Holen Sie sich die Autoren-ID jedes Produkts im Woocommerce-Warenkorb

Lesezeit: 3 Minuten

Benutzer-Avatar
Noe_Mares

In Woocommerce muss ich die Benutzer-ID jedes Produkts im Warenkorb abrufen. Die Benutzer-ID stammt nicht vom Kunden, sondern von dem Benutzer, der das Produkt erstellt und veröffentlicht hat, das sich derzeit im Warenkorb befindet (der Autorenbeitrag).

Ich habe diesen Code, den ich hier bekommen habe: WooCommerce Get Order Product Details Before Payment in Plugin

Code geteilt von: @LoicTheAztec

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $product_id = $cart_item['product_id']; // Product ID
    $product_obj = wc_get_product($product_id); // Product Object
    $product_qty = $cart_item['quantity']; // Product quantity
    $product_price = $cart_item['data']->price; // Product price
    $product_total_stock = $cart_item['data']->total_stock; // Product stock
    $product_type = $cart_item['data']->product_type; // Product type
    $product_name = $cart_item['data']->post->post_title; // Product Title (Name)
    $product_slug = $cart_item['data']->post->post_name; // Product Slug
    $product_description = $cart_item['data']->post->post_content; // Product description
    $product_excerpt = $cart_item['data']->post->post_excerpt; // Product short description
    $product_post_type = $cart_item['data']->post->post_type; // Product post type

    $cart_line_total = $cart_item['line_total']; // Cart item line total
    $cart_line_tax = $cart_item['line_tax']; // Cart item line tax total
    $cart_line_subtotal = $cart_item['line_subtotal']; // Cart item line subtotal
    $cart_line_subtotal_tax = $cart_item['line_subtotal_tax']; // Cart item line tax subtotal

    // variable products
    $variation_id = $cart_item['variation_id']; // Product Variation ID
    if($variation_id != 0){
        $product_variation_obj = wc_get_product($variation_id); // Product variation Object
        $variation_array = $cart_item['variation']; // variation attributes + values
    }
}

Es ist perfekt, aber ich brauche auch die Post-Autoren-ID

Jede Hilfe ist willkommen.

Benutzer-Avatar
LoicTheAztec

Versuchen Sie Folgendes, um den Autor des Beitrags aus dem Produkt (Warenkorbartikel) zu erhalten:

// For all Woocommerce versions
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $post_obj    = get_post( $cart_item['product_id'] ); // The WP_Post object
    $post_author = $post_obj->post_author; // <=== The post author ID
}

Ich habe die verknüpfte Antwort in Ihrer Frage aktualisiert (da sie nicht mit den Woocommerce-Versionen 3+ kompatibel war).

Für Woocommerce vor Version 3 (also zum Beispiel 2.6.x) können Sie Folgendes verwenden:

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $authror_id = $cart_item['data']->post->post_author; // <=== The post author ID
}

  • Woow, du bist großartig! Ich suchte nach WordPress-Dokumentation und konnte nichts finden, was gut funktionieren würde. Es funktioniert super, danke für die Hilfe. Wissen Sie, wo ich eine Dokumentation finden kann, die für Anfänger von WordPress-Entwicklern leichter verständlich ist?

    – Noe_Mares

    22. Mai 2018 um 17:14 Uhr


  • @Noe_Mares For WordPress ist etwas sehr Großes und etwas weniger für WooCommerce. Es ist nicht leicht zu verstehen, es ist etwas sehr Fortschrittliches. Es gibt viele Websites, auf denen Sie Tutorials und Informationen finden können. Für WordPress vielleicht: wpbeginner.com … Denn Woocommerce ist etwas anderes, schwieriger und sinnvoller

    – LoicTheAztec

    22. Mai 2018 um 17:33 Uhr


  • Ich verstehe, ich werde erstaunt sein, den WordPress- und Woocommerce-Code nach und nach zu verstehen. Nochmals vielen Dank, ich schicke dir eine große Umarmung 🙂

    – Noe_Mares

    22. Mai 2018 um 18:13 Uhr

  • @Noe_Mares Wir haben alle wie du angefangen.

    – LoicTheAztec

    22. Mai 2018 um 18:15 Uhr

1381860cookie-checkHolen Sie sich die Autoren-ID jedes Produkts im Woocommerce-Warenkorb

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy