【问题标题】:add a category to all products that already have a common category将类别添加到所有已具有共同类别的产品
【发布时间】:2022-01-07 01:39:15
【问题描述】:

我想用 jquery 添加一个类别到 woocommerce 所有具有“tax_qeeboo”类的产品,从特定的一天开始,但我不明白我错在哪里

var startpromozioneuno = new Date("November 30, 2021 00:00:00")
  if(today.getTime()>startpromozioneuno.getTime()){
    $('body.tax_qeeboo').addClass('tax_occasioni');
  }

类别出现在代码中,但不出现在前端。 我想获得的最终结果就像我从 woocommerce 面板中添加了一个类别

谢谢

【问题讨论】:

  • 不清楚你想在这里实现什么。如果存在特定类,您想在正文中添加其他类吗?或者您想更新所有具有特定类别的产品以在数据库中保存另一个产品?
  • 对不起!第二个:我想更新所有具有特定类别的“.tax_qeeboo”类的产品,以便在数据库中保存另一个产品
  • 好的,然后转到您的管理仪表板转到产品 -> 类别。然后在每个类别旁边,您会看到一个数字,显示该类别有多少产品。单击它,将列出该类别的所有产品。然后选择所有产品并进行批量编辑并选择您要添加的类别。如果它的多个页面只是进入每个页面或增加每个页面的产品,但可能需要更多时间来更新取决于您的服务器等。在此之前备份 ofc :)
  • 我尝试以这种方式进行批量编辑,但不起作用:后端有东西阻止了该功能。我尝试使用 BEAR 插件,但对于我需要的更改,我必须购买高级版本。出于这个原因,我正在尝试用 jquery 做一些事情:(
  • 你不能只用 jquery 更新数据库。

标签: jquery wordpress woocommerce categories


【解决方案1】:

将以下函数放在你的主题functions.php中

使用前做好备份。 使用您的参数更改 $taxonomy 、 $post_type 、 $filter_term_id 、 $target_term_id 。

add_action('init','update_post_taxonomy_terms');
function update_post_taxonomy_terms() {

    $taxonomy = 'product_cat'; // Taxonomy we are looking for
    $post_type = 'product'; // Post type we are looking for
    $filter_term_id = 199; // The taxonomy term id we are looking for 
    $target_term_id = 263; // Term id we want to add

    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'term_id',
                'terms' => $filter_term_id
            )
        )
    );

    $results = new WP_Query($args); // We need all posts that we will update
    $post_ids = wp_list_pluck( $results->posts, 'ID' ); // We need only post ids
    
    error_log(print_r($post_ids,true)); // Check the post ids that will be updated if all good uncomment rest of the code

    //Uncomment when ready
    // foreach($post_ids as $post_id):
    //     wp_set_post_terms( $post_id, $target_term_id, $taxonomy );
    // endforeach;
}

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2018-09-09
  • 1970-01-01
相关资源
最近更新 更多