在 Woocommerce wc_get_loop_prop 产品循环中仅包含某些类别

Include only certain categories in the Woocommerce wc_get_loop_prop product loop

我正在使用 archive-product.php 作为基础构建自定义类别存档模板。我需要做的是使用产品类别的 ID(在 Woocommerce 中为 product_cat)仅显示某些类别的产品。但我想使用 wc_get_products 而不是 WP_Query post 循环,回复:https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

"wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance."

我已经将 archive-product.php 复制到我的 child 主题中的 woocommerce 文件夹并重命名为 custom-category-archive.php,它有一个 header 的

/*
Template Name: Custom Category Archive Template
*/

所以我可以 select 它作为页面编辑器中的页面模板。

这是新模板中的原始 Woocommerce 产品循环 custom-category-archive.php:

if ( woocommerce_product_loop() ) {

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked woocommerce_output_all_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action( 'woocommerce_before_shop_loop' );

    woocommerce_product_loop_start();

    if ( wc_get_loop_prop( 'total' ) ) {
        while ( have_posts() ) {
            the_post();

            /**
             * Hook: woocommerce_shop_loop.
             */
            do_action( 'woocommerce_shop_loop' );

            wc_get_template_part( 'content', 'product' );
        }
    }

    woocommerce_product_loop_end();

如何使用类别 ID 修改循环以仅包含产品类别 (product_cat) 中的产品?这就是新的 WP_Query post 循环仅包含产品类别 12, 345, 7899:

中的产品的方式
$args = array(
   'post_type' => 'product',
   'post_status' => 'publish',
   'posts_per_page' => '-1',
   'tax_query' => array(
        array(
            'taxonomy'  => 'product_cat',
            'terms'     => array('12,345,7899'),   // including categories by ID
            'operator'  => 'IN',
        )
 

如何在我的 custom-category-archive.php 模板的 wc_get_loop_prop('total') post 产品循环中包含产品类别,例如 12, 345, 7899,以仅显示这三个产品中的产品类别?

你应该试试 -

$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'tax_query' => array(
         array(
             'taxonomy'  => 'product_cat',
             'terms'     => array(12,345,7899),   // including categories by ID
             'operator'  => 'IN',
         )
    )
);
$loop = new WP_Query( $args ); 
    
while ( $loop->have_posts() ) : $loop->the_post(); 
    print the_title(); 
    the_excerpt(); 
endwhile;

wp_reset_postdata(); 

引用此大纲 https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/,这是使用 wc_get_products:

分类存档的基本循环
defined( 'ABSPATH' ) || exit;

get_header( 'shop' );

do_action( 'woocommerce_before_main_content' );

?>

<?php


  if(!function_exists('wc_get_products')) {
    return;
  }


  $cat_terms = get_field('add_categories_custom_template', $term->term_id);


  $ordering              = WC()->query->get_catalog_ordering_args();
  $ordering['orderby']   = array_shift(explode(' ', $ordering['orderby']));
  $ordering['orderby']   = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
 
  $cat_products         = wc_get_products(array(
    'stock_status'      => 'instock',
    'visibility'        => 'visible',
    'status'            => 'publish',
    'limit'             => -1,
    'paginate'          => true,
    'return'            => 'ids',
    'orderby'           => $ordering['orderby'],
    'order'             => $ordering['order'],

    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'field'         => 'term_id',
            'terms'         => array('123,45,6789'),
            'operator'      => 'IN',
        )
   )

  ));


 wc_set_loop_prop('total', $cat_products->total);

  if($cat_products) {
    do_action('woocommerce_before_shop_loop');

    echo '<div id="container">';

      foreach($cat_products->products as $cat_product) {

        $post_object = get_post($cat_product);
        setup_postdata($GLOBALS['post'] =& $post_object);

        echo '<div '; wc_product_class( ' ', $product ); echo '>'; 

            do_action( 'woocommerce_before_shop_loop_item' );
            do_action( 'woocommerce_before_shop_loop_item_title' );
            do_action( 'woocommerce_shop_loop_item_title' );
            do_action( 'woocommerce_after_shop_loop_item_title' );
            do_action( 'woocommerce_after_shop_loop_item' );

        echo '</div>';

      }
      wp_reset_postdata();

echo '</div>';

    do_action('woocommerce_after_shop_loop');
  } else {
    do_action('woocommerce_no_products_found');
  }


do_action( 'woocommerce_after_main_content' );