我要做的是创建一个foreach循环以获取所有产品,并从这些产品中获取其类别ID。这样一来,我以后便可以检查某些类别。
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_catagory_id .= $item['catagory_id']; //in between these brackets I need a value that gives me the catagory id.
}
//Some validation here if one of the catagorie_id's was found.
//I can build this. It's not part of the question
经过一番评论,这是我受困的地方:
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
var_dump($product_id);
$terms = get_the_term_list( $product_id, 'term_id' );
var_dump($terms);
}
结果如下:
string(2) "79"
object(WP_Error)#9148 (2) {
["errors"]=>
array(1) {
["invalid_taxonomy"]=>
array(1) {
[0]=>
string(19) "Ongeldige taxonomie" //invalid taxonomy
}
}
["error_data"]=>
array(0) {
}
}
因此,可以很好地读取产品ID,我可以使用它,但是不能正确返回类别。
尝试
$terms = get_the_terms ( $product_id, 'product_cat' );
foreach ( $terms as $term ) {
$cat_id = $term->id;
}
尝试var_dump($terms);
正确分析如何获取类别ID。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句