如何获取当前订单商品的产品类别?
我正在尝试通过woocommerce_checkout_create_order_line_item挂钩获取woocommerce订单商品的产品类别.
I am trying to get the product categories for the woocommerce order item at woocommerce_checkout_create_order_line_item hook.
我能够成功获得product_id(感谢帮助,我来到这里),但是现在尝试获取产品的类别,尝试$ product-> get_categories()并尝试wc_get_product_category_list($ product-> get_id时,数组返回为空().
I am able to successfully get the product_id (thanks to help I got here) but now trying to get the product's categories the array comes back empty trying $product->get_categories() and alternatively trying wc_get_product_category_list($product->get_id().
我不知道自己在做什么错.
I can't figure out what I am doing wrong.
add_action('woocommerce_checkout_create_order_line_item',
'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta( $item, $cart_item_key, $cart_item, $order ) {
$product = $item->get_product(); // The WC_Product instance Object
$cat_names = $product->get_categories(); // one attempt
$cat_names = wc_get_product_category_list($product->get_id()); // another attempt
$arrlength = count($cat_names);
for($x = 0; $x<$arrlength; $x++) {
$cat_name = $cat_names[$x];
}
$item->update_meta_data( '_raw_product_id', $product->get_id() );
$item->update_meta_data( '_raw_product_name', $cat_name );
}
因此add_action和函数起作用."$ product = ..."可以正常工作,我可以在下面的代码的第二到第二行中将其用作$ product-> get_id(),并根据需要将正确的值存储为元数据.
So the add_action and the function work. The "$product=..." works and I can use it below in the 2nd to the last line of code as $product->get_id() and the correct value is stored as metadata as desired.
因此,由于$ product-> get_id()有效,因此我认为$ product-> get_categories()将起作用是合乎逻辑的.但是它返回一个空数组.
So since $product->get_id() works I thought it logical that $product->get_categories() would work. But it returns a null array.
然后我在某处读取了已弃用的内容,应该使用wc_get_product_category_list.所以我尝试了,也没有运气.
I then read somewhere that it was deprecated and I should use wc_get_product_category_list. So I tried that and also no luck.
所以我现在陷入困境,无法弄清楚我的代码出了什么问题.感谢您的帮助.
So I am now stuck and can't figure out what is wrong with my code. Thanks for any help.
不建议使用 WC_Product
类的 get_categories()
方法.相反,您可以使用 get_category_ids()
方法.但是,此方法返回产品类别ID,并且似乎需要类别名称,以便我们可以从WP_Term对象获取名称.
As you mentioned get_categories()
method of WC_Product
class is deprecated. Instead you can use get_category_ids()
method. However this method returns product category IDs, and It seems that you need category names so we can get the names from WP_Term objects.
最终代码如下:
add_action('woocommerce_checkout_create_order_line_item', 'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta($item, $cart_item_key, $cart_item, $order)
{
$product = $item->get_product(); // The WC_Product instance Object
$cat_ids = $product->get_category_ids(); // returns an array of cat IDs
$cat_names = [];
foreach ( (array) $cat_ids as $cat_id) {
$cat_term = get_term_by('id', (int)$cat_id, 'product_cat');
if($cat_term){
$cat_names[] = $cat_term->name; // You may want to get slugs by $cat_term->slug
}
}
$item->update_meta_data( '_raw_product_id', $product->get_id() );
$item->update_meta_data( '_raw_product_name', $cat_names );
}
注意:: foreach
循环是执行此类操作的首选方式(而不是使用 for
循环).
Note: foreach
loop is the preferred way of doing this kind of stuff (instead of using a for
loop).