微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我还想在父类别中显示子类别的产品

如何解决我还想在父类别中显示子类别的产品

这是一个 Woocommerce 网上商店,但当我处于父类别时,它不会显示子类别的产品。一个解决方案是遍历所有产品并将它们添加父类别,但当我有 5000 个产品时,这需要做很多工作。

  1. 是否可以使用 fx.用于将所有产品添加到现有父类别的 sql 查询
  2. 然后将方法/函数添加到 function.PHP 文件中,这样当我保存新产品时,它会自动添加到我选择的类别中的所有父类别中。

我找到了这个例子:

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),true);
                $term = get_term($term->parent,'product_cat');
            }
        }

    }

来自 Mohammad Arshi 来自此 stackoverflow 答案 (Show Sub categories of products under parent in Woocommerce) 的方法/函数,可以回答问题 2。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。