我正在尝试使用一个函数来更新 WooCommerce/WP 中的“product_themes”用户字段,该函数收集订单中项目的 term_id,并在下订单时将它们添加到“product_themes”字段下的用户个人资料中. 当我在代码中指定一组 ID 但似乎没有从每个项目中为自定义“主题”taonomy 收集术语 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 文档并搜索了任何类似问题的解决方案,但我似乎无法让它发挥作用。我错过了什么吗?
非常感谢。