php - 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.
解决方案
尝试以下操作,从产品(购物车项目)中获取帖子作者:
// 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
}
推荐阅读
- javascript - 使用过滤器计算对象值
- windows - Windows 2019 上堆外持久性存储的 Apache Ignite OOM 错误
- python - django save() 在 sqlite 中不起作用
- google-sheets - 使用谷歌表抓取只是拨号网站
- python - 将 ntScanDirInterator 过滤为仅文件
- javafx - Gluon Client Maven 插件:应在“reflectionList”配置下指定哪种类型的类
- python - APPIUM:无法绑定监听器:无法绑定到 127.0.0.1:8200
- c++ - C++ - 如何比使用 unordered_map 更快地缓冲计算结果
- python - Plotly - 如何在悬停时突出显示节点的路径?
- type-inference - Protege 中的推断不会基于 `subClassOf` 推断类型