在Woocommerce上将子类别批量重新链接到父类别

时间:2018-07-30 18:45:07

标签: php wordpress plugins woocommerce categories

我们最近从Big commerce向woocommerce导入了一家大型产品商店,但在类别方面遇到了问题。

产品仅是子类别的链接,这意味着如果您访问父类别,则不会显示任何产品。

Product linked to child category but not parent

是否有一种方法可以使所有子类别大规模重新链接到父类别?该商店有6000多种产品和数百个类别,因此即使使用批量编辑工具,也需要很长时间才能完成此任务。

预先感谢您,如果已经提出要求,我们深表歉意,很难找到适合这种情况的措词。

**我遇到了这段代码**

add_action('save_post', 'assign_parent_terms', 10, 2);

function assign_parent_terms($post_id, $post){

    if($post->post_type != 'product')
        return $post_id;

    // get all assigned terms   
    $terms = wp_get_post_terms($post_id, 'product_cat' );
    foreach($terms as $term){
        while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
            // move upward until we get to 0 level terms
            wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
            $term = get_term($term->parent, 'product_cat');
        }
    }

}

有人可以指出调整代码的正确方向,这样我就不必单独保存每个产品,而只需运行一次即可更新所有产品?

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请勿执行add_action()部分。使用

$products = get_posts(['post_type' => 'product', 'posts_per_page' => 100, 'offset' => 0]);
var_dump(count($products));
foreach ($products as $product) {
    assign_parent_terms($oroduct->ID, $product);
}

刷新页面。每次刷新后手动将偏移量增加100,直到转储显示0。

相关问题