我有一个函数,它添加了标记"新的";到基于CCD_ 1钩子新发布的产品。
// Add New Tag to new Products
function jsd_tag_on_product_save( $product_id ) {
//ADD THE PRODUCT TAG
wp_add_object_terms($product_id, "New", 'product_tag');
}
add_action( 'woocommerce_new_product', 'jsd_tag_on_product_save', 10, 1 );
这很好,但我也在尝试在以后删除标签。我首先得到";新的";在阵列中标记产品并按日期排序:
//Get products by tag
$new_product_ids = get_posts( array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'New',
'operator' => 'IN',
)
),
));
然后根据CCD_ 2&CCD_ 3-";新产品最小值";被手动设置为"0";10〃;以确保总是有至少10个标有";新":
//Set the minimum number of new products to leave
$min_new_products = 10;
$total_new = count($new_product_ids);
if( $total_new > $min_new_products){
// No. to remove
$remove_qty = abs($total_new - $min_new_products);
最后,我启动一个计数器并打开一个foreach
循环以获得一个产品标签数组;新的";标记,当计数器>=时使用break
退出循环$remove_qty
:
// For each product remove New tag
foreach ( $new_product_ids as $product_id ) {
$all_current_tags = array();
// get an array of the WP_Term objects for a defined product ID
$terms = wp_get_post_terms( $product_id, 'product_tag' );
// Loop through each product tag for the current product
if( count($terms) > 0 ){
foreach($terms as $term){
$term_name = str_replace('"', "", $term->name);
// Set the product tag names in an array
$all_current_tags[] = $term_name;
}
}
$key = array_search('New', $all_current_tags);
unset($all_current_tags[$key]);
$i++;
if ($i >= $remove_qty){
break;
}
}
不幸的是,我的代码不起作用。。。我是一个PHP新手,不知道为什么,甚至不知道如何调试它。有人能建议我如何修复它吗?
以下完整代码:
// Untag old "New" products weekly
function mlabs_untag(){
//Get products by tag
$new_product_ids = get_posts( array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'New',
'operator' => 'IN',
)
),
));
echo "<script>console.log('New Products Array: " . json_encode($new_product_ids) . "' );</script>";
//Set the minimum number of new products to leave
$min_new_products = 10;
$total_new = count($new_product_ids);
if( $total_new > $min_new_products){
echo "<script>console.log('Total New: " . $total_new . "' );</script>";
echo "<script>console.log('Min to Leave: " . $min_new_products . "' );</script>";
echo "<script>console.log('Total is greater than min.');</script>";
// No. to remove
$remove_qty = abs($total_new - $min_new_products);
echo "<script>console.log('No. to remove: " . $remove_qty . "' );</script>";
// Start counter
$i = 0;
// For each product remove New tag
foreach ( $new_product_ids as $product_id ) {
$all_current_tags = array();
// get an array of the WP_Term objects for a defined product ID
$terms = wp_get_post_terms( $product_id, 'product_tag' );
// Loop through each product tag for the current product
if( count($terms) > 0 ){
foreach($terms as $term){
$term_name = str_replace('"', "", $term->name);
// Set the product tag names in an array
$all_current_tags[] = $term_name;
}
}
echo "<script>console.log('Product Terms Array: " . json_encode($all_current_tags) . "' );</script>";
$key = array_search('New', $all_current_tags);
unset($all_current_tags[$key]);
echo "<script>console.log('Product Terms Array Edited: " . json_encode($all_current_tags) . "' );</script>";
$i++;
if ($i >= $remove_qty){
break;
}
}
}
}
原来我很接近。。。我忘了在最后用wp_set_object_terms($product_id, $all_current_tags, 'product_tag');
设置产品标签😅
我还将'order' => 'DESC',
更改为'order' => 'ASC',