我正在尝试使用一个功能来更新WooCommerce / WP中的'product_themes'用户字段,该功能收集订单中商品的term_ids,并在下订单时将它们添加到'product_themes'字段下的用户配置文件中 . 当我在代码中指定ID数组但似乎没有从每个项目中收集术语ID以用于自定义“主题”分类时,这是有效的 .
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$item_id = $item['order_item_id'];
$product_name = $item['name'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}
我已经查阅了WooCommerce文档并搜索了任何类似问题的解决方案,但我似乎无法使其工作 . 我错过了什么吗?
非常感谢 .
1 回答
我自己找到了解决方案,更改了$ itemID = $ item ['order_item_id']; to $ itemID = $ item ['product_id'];