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

Posted

技术标签:

【中文标题】在 Woocommerce wc_get_loop_prop 产品循环中仅包含某些类别【英文标题】:Include only certain categories in the Woocommerce wc_get_loop_prop product loop 【发布时间】:2021-12-24 04:40:36 【问题描述】:

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

"wc_get_products 和 WC_Product_Query 提供了一个标准的方法 检索可安全使用且不会因以下原因而损坏的产品 未来 WooCommerce 版本中的数据库更改。建筑定制 WP_Queries 或数据库查询将来可能会破坏您的代码 WooCommerce 版本随着数据向自定义表格移动以获得更好的效果 性能。”

我已将archive-product.php 复制到我的子主题中的woocommerce 文件夹中,并将其重命名为custom-category-archive.php,它的标题为

/*
Template Name: Custom Category Archive Template
*/

所以我可以在页面编辑器中选择它作为页面模板。

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

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 帖子循环仅包含产品类别 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') 发布产品循环中包含产品类别,例如12, 345, 7899,以仅显示这三个产品类别中的产品?

【问题讨论】:

澄清一下,您有一个WP_Query,它返回某些类别的产品。现在您希望相同的查询只返回ids 而不是整个数据?你问的是这个吗?能解释清楚吗? 谢谢,澄清一下,我想使用类别 ID 退回这些类别中的产品。但如果可能的话,我想使用 wc_get_loop_prop 循环而不是新的 WP_Query。也许使用 WP_Query 更简单更容易? 【参考方案1】:

引用此大纲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' );

【讨论】:

【参考方案2】:

你应该试试是 -

$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(); 

【讨论】:

我的问题是我想使用 wc_get_loop_prop,而不是 WP_Query。 在get(wc_get_loop_prop)之前你必须设置(wc_set_loop_prop)。请参阅参考(自定义 WooCommerce 产品循环)网址:cfxdesign.com/…

以上是关于在 Woocommerce wc_get_loop_prop 产品循环中仅包含某些类别的主要内容,如果未能解决你的问题,请参考以下文章

php [WooCommerce Core]在主题激活时设置WooCommerce图像尺寸

php [WooCommerce Core]在主题激活时设置WooCommerce图像尺寸

Woocommerce:woocommerce 查询库存数量简单和可变产品

如何在 WooCommerce 3+ 中进行调试

在 WooCommerce 中禁用原始图像裁剪

WooCommerce 插件模板覆盖