【发布时间】:2018-06-26 12:42:35
【问题描述】:
在 Woocommerce 中,如果产品具有特定的自定义字段值(使用高级自定义字段插件生成此字段),我会尝试自动将给定的产品类别分配给产品。
在我的functions.php 我有:
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
$post_type = get_post_type($post_id);
if ( "product" != $post_type ) return;
$field = get_field("city");
if($field == "Cassis"){
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
if($product_cat_id != 93){
wp_set_post_terms( $product_id, 93, 'product_cat', true );
}
break;
}
}
}
add_action('save_post','auto_add_category');
但这不起作用。请问有什么办法吗?
【问题讨论】:
标签: php wordpress woocommerce categories product