这个问题在这里已有答案:
我正在尝试创建所有这些if语句:
-
如果选中产品类别A,将自动不检查类别B,产品状态变为"out of stock" .
-
如果未选中产品类别A,将自动选中类别B.
到目前为止,除了"out of stock"部分之外,这两个陈述都有效 . 任何想法 how to call and set the stock status ?
add_action( 'save_post', 'auto_add_product_category', 50, 3 );
function auto_add_product_category( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
$term_id_A = 116; //category A
$term_id_B = 1459; //category B
$terms = get_the_terms( $post_id, 'product_cat' );
$term_ids = wp_list_pluck( $terms, 'term_id' );
if(in_array($term_id_A, $term_ids) && ($key = array_search($term_id_B, $term_ids)) !== false) {
unset($term_ids[$key]);
} elseif(!in_array($term_id_A, $term_ids) && !in_array($term_id_B, $term_ids)) {
$term_ids[] = $term_id_B;
}
wp_set_post_terms($post_id, $term_ids, 'product_cat');
}
1 回答
您需要用以下代码替换代码的最后一位:
有关额外代码的进一步说明,请访问:Updating product stock programmatically in Woocommerce 3