首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为最新的WooCommerce产品设置对象术语,并删除旧产品的对象术语

为最新的WooCommerce产品设置对象术语,并删除旧产品的对象术语
EN

Stack Overflow用户
提问于 2021-11-10 11:38:28
回答 1查看 146关注 0票数 1

我想把最后32个上传的产品放在一个特定的类别中。

该函数可以工作,但在上传新产品时,它不会从旧产品中删除该类别。

以下是代码:

代码语言:javascript
复制
function add_category_to_product()
{

    $args = array(
        'post_type'   => 'product',
        'posts_per_page' => 32
    );

    $products = new WP_Query($args);
    $foundposts = $products->post_count;

    if ($products->have_posts()) {
        while ($products->have_posts()) {
            $products->the_post();

            
            if ($foundposts <= 32 && has_term(array(86, 87, 88, 89, 90, 91, 92, 93, 83, 94, 95, 96, 84, 98, 99, 100, 101, 102, 103, 80), 'product_cat', get_the_ID())) {
                wp_set_object_terms(get_the_ID(), 79, 'product_cat', true);
            } elseif ($foundposts <= 32 && has_term(array(51, 52, 53, 54, 55, 56, 57, 58, 43, 60, 61, 62, 63, 45, 64, 65, 66, 67, 68, 69, 70, 48, 71, 72, 73, 74, 75, 76, 77, 78, 44, 49), 'product_cat', get_the_ID())) { 
                wp_set_object_terms(get_the_ID(), 40, 'product_cat', true);
            } elseif ($foundposts <= 32 && has_term(array(128, 129, 130, 131, 132, 114, 115, 133, 134, 135, 136, 117, 118, 119, 137, 138, 139, 140), 'product_cat', get_the_ID())) {
                wp_set_object_terms(get_the_ID(), 112, 'product_cat', true);
            } elseif ($foundposts <= 32 && has_term(array(141, 142, 143, 144, 145, 146, 125, 147, 148, 149, 150), 'product_cat', get_the_ID())) {
                wp_set_object_terms(get_the_ID(), 122, 'product_cat', true);
            } else {
                wp_remove_object_terms(get_the_ID(), 112, 'product_cat');
                wp_remove_object_terms(get_the_ID(), 122, 'product_cat');
                wp_remove_object_terms(get_the_ID(), 40, 'product_cat');
                wp_remove_object_terms(get_the_ID(), 79, 'product_cat');
            }
        }
        wp_reset_postdata();
    }
}

也许有人能看到我的错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-10 14:45:59

您的错误:

您可以使用'posts_per_page' => 32并提到‘和其他产品’,而其他条件在这32个当前产品的循环中.

解决方案:

  • 使用查询
  • 由于您实际上希望对所有产品应用更改(除非您定期执行此功能),仅应用于最后32个产品是不够的。所以使用limit,它接受一个整数:要检索的结果的最大数量,或无限的-1
  • orderby -接受一个字符串:'none','ID','name','type','rand','date','modified‘。
  • order -接受一个字符串:'DESC‘或'ASC’。与orderby一起使用。
  • return -接受一个字符串:idsobjects.,因为我们只需要针对产品对象的productID,ids就足够了。
  • 注意:当不涉及很多产品时,对产品进行更改的方法是有效的。如果不是,那么sql查询是一个更有效的解决方案。

使用的WordPress函数:

所以你得到了:

代码语言:javascript
复制
// For x number of last products
$last_products = 32;

// Args
$args = array(
    'limit'     => -1,
    'orderby'   => 'date',
    'order'     => 'DESC',
    'return'    => 'ids',
);

// Get product IDs
$product_ids = wc_get_products( $args );

// Loop through
foreach( $product_ids as $key => $product_id ) {
    if ( ( $key + 1 ) <= $last_products ) {
        if ( has_term( array( 80, 86, 87, 88, 89, 90, 91, 92, 93, 83, 94, 95, 96, 84, 98, 99, 100, 101, 102, 103 ), 'product_cat', $product_id ) ) {
            wp_set_object_terms( $product_id, 79, 'product_cat', true );
        } elseif ( has_term( array( 44, 49, 51, 52, 53, 54, 55, 56, 57, 58, 43, 60, 61, 62, 63, 45, 64, 65, 66, 67, 68, 69, 70, 48, 71, 72, 73, 74, 75, 76, 77, 78 ), 'product_cat', $product_id ) ) {
            wp_set_object_terms( $product_id, 40, 'product_cat', true );                
        } // etc..
    } else {
        if ( has_term( array ( 40, 79, 112, 122 ), 'product_cat', $product_id ) ) {
            wp_remove_object_terms( $product_id, 40, 'product_cat', true );
            wp_remove_object_terms( $product_id, 79, 'product_cat', true );
            wp_remove_object_terms( $product_id, 112, 'product_cat', true );
            wp_remove_object_terms( $product_id, 122, 'product_cat', true );
        }           
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69912627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档