微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在Woocommerce购物车中获取每个产品的作者ID

在woocommerce中,我需要获取购物车中每个产品的用户ID,用户ID不是来自客户,而是创建和发布当前位于购物车中的产品的用户(作者帖子).

我有这个代码,我在这里WooCommerce Get Order Product Details Before Payment in Plugin

代码共享:@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
    }
}

这是完美的,但我还需要获得帖子的作者ID

任何帮助表示赞赏.

解决方法:

请尝试以下操作,从产品中获取帖子作者(购物车项目):

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

我有一个关于你的问题中的链接答案的更新(因为它与Woocommerce版本3不兼容)

对于版本3之前的Woocommerce(例如2.6.x),您可以使用:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐