Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我还希望在父类别中显示子类别中的产品_Php_Wordpress_Woocommerce_Webshop_Product Catalog - Fatal编程技术网

Php 我还希望在父类别中显示子类别中的产品

Php 我还希望在父类别中显示子类别中的产品,php,wordpress,woocommerce,webshop,product-catalog,Php,Wordpress,Woocommerce,Webshop,Product Catalog,这是一个Woocommerce网站商店,但当我在父类别中时,它不显示子类别的产品。一个解决方案是遍历所有产品,并将它们添加到父类别中,但当我有5000个产品时,这需要大量的工作 有没有外汇。将所有产品添加到现有父类别的Sql查询 然后将method/function添加到function.php文件中,这样当我保存新产品时,它会自动添加到我选择的类别中的所有父类别中 我发现这个例子: add_action('save_post', 'assign_parent_terms', 10, 2);

这是一个Woocommerce网站商店,但当我在父类别中时,它不显示子类别的产品。一个解决方案是遍历所有产品,并将它们添加到父类别中,但当我有5000个产品时,这需要大量的工作

  • 有没有外汇。将所有产品添加到现有父类别的Sql查询
  • 然后将method/function添加到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中得到了一个方法/函数的答案(),它可能是问题2的答案