【问题标题】:Bulk relink child categories to parent categories on Woocommerce在 Woocommerce 上将子类别批量重新链接到父类别
【发布时间】:2019-01-07 01:59:06
【问题描述】:

我们最近将一家大型产品商店从 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');
        }
    }

}

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

再次感谢您的帮助。

【问题讨论】:

    标签: php wordpress plugins woocommerce categories


    【解决方案1】:

    不要做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。

    【讨论】:

    • 感谢您的帮助!我刚刚尝试了代码,但出现错误 Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)
    • 如果有效,您能否将我的回答标记为有用/正确?
    • 如果服务器不能一次处理 6000 个,请使用 posts_per_page 和 limit 参数分步执行。
    • 对不起,我们互相评论了,我现在试试看
    • 如果限制为 100 个帖子,请分步尝试。每次刷新时将偏移量增加 100。您将需要刷新 60 次,但这比保存 6000 个帖子要好。
    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多