I’m building a custom category archive template using archive-product.php
as a base. What I need to do is show only products from some categories by using the ID of the product category (which is product_cat
in Woocommerce). But I want to use wc_get_products
rather than a WP_Query
post loop, re: 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."
I’ve copied archive-product.php
to the woocommerce folder in my child theme and renamed it custom-category-archive.php
, and it has a header of
/*
Template Name: Custom Category Archive Template
*/
so I can select it as a page template in the page editor.
This is the original Woocommerce product loop in the new template 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();
How do I modify the loop to include only the products in the product category (product_cat
) by using the category ID? This is how a new WP_Query post loop includes only the products which are in the product categories 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',
)
How do I include product categories, i.e. like 12, 345, 7899
, in the wc_get_loop_prop('total')
post product loop in my custom-category-archive.php
template to show only products in those three product categories?
2
Answers
Referencing this outline https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/, this is a basic loop for a category archive using
wc_get_products
:you should try like is –