Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the author ID of each product in Woocommerce cart

In woocommerce, I need get the user ID of each product within the cart, The user ID isn't from the customer, but is the user who created and published the product that is currently in the shopping cart (the author post).

I have this code that I got here: WooCommerce Get Order Product Details Before Payment in Plugin

code shared by: @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
    }
}

It is perfect, but I also need to get the post author ID

Any help is appreciated.

like image 502
Noe_Mares Avatar asked May 22 '18 07:05

Noe_Mares


People also ask

How do I get the product ID from the cart in WooCommerce?

You can use directly $product_id variable of the first item in cart. 2) Using an array of product IDs (one for each item in cart). To get the 1st item product ID: $products_ids_array[0]; To get the 2nd item product ID: $products_ids_array[1]; etc…

How do I find my WooCommerce product ID?

A WooCommerce Product ID is sometimes needed when using shortcodes, widgets, and links. To find the ID, go to Products and hover over the product you need the ID for. The product ID number is displayed.

How do I find the author ID in WordPress?

To get the author ID by post ID, you can use the get_post_field() function, along with the post_author and post_id as parameters. Below is an example of getting author id by post id. $author_id = get_post_field( 'post_author' , $post ->ID); // display the author ID.

How do I get product Meta in WooCommerce?

$product->get_tags(); // Returns the product tags. $product->get_upsell_ids(); // Get upsell IDs. $product->get_upsells(); // Returns the upsell product ids. $product->get_meta(); // Get Meta Data by Key.


1 Answers

Try the following, to get the post author from the product (cart item):

// 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
}

I have made an update on the linked answer in your question (as it was not compatible with Woocommerce versions 3+)

For Woocommerce before version 3 (so 2.6.x for example), you can use:

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $authror_id = $cart_item['data']->post->post_author; // <=== The post author ID
}
like image 160
LoicTheAztec Avatar answered Sep 22 '22 23:09

LoicTheAztec