我正在尝试更新a' product_themes' WooCommerce / WP中的用户字段,其功能是收集订单中商品的term_ids,并将其添加到产品主题下的用户个人资料中。下订单时的字段。当我在代码中指定ID数组但似乎没有从每个项目中收集术语ID以用于自定义主题时,这是有效的。 taonomy。
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文档并搜索了任何类似问题的解决方案,但我似乎无法使其工作。我错过了什么吗?
非常感谢。
答案 0 :(得分:0)
我自己找到了解决方案,更改了$ itemID = $ item [' order_item_id']; $ itemID = $ item [' product_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 ) {
$itemID = $item['product_id'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
update_field('product_themes', $push, 'user_' . $userID);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}