0

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

    }

Mohammad Arshi 来自这个 stackoverflow 的答案(在 Woocommerce 中的父级下显示产品的子类别)的方法/功能,可以回答问题 2。

4

0 回答 0