Reputation: 939
I am having difficulty creating my own custom WooCommerce product loop.
Along the way I have encountered a few questions that would be really helpful if they could be answered.
I am currently working with this block of code:
ARCHIVE-PRODUCT.PHP (Moved into child theme -> woocommerce/archive-product.php):
<?php
/**
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
get_header();
$display_count = $atts['per_page'];
echo $display_count; // NO value - How do I access this?
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo $page; // returns 1 - CORRECT;
$offset = ( $page - 1 ) * $display_count;
echo $offset; // returns 0 - CORRECT;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $atts['per_page'],
'page' => $page,
'offset' => $offset,
'orderby' => $atts['orderby'],
'order' => $atts['order']
); ?>
<?php
$loop = new WC_Product_Query( $args );
if ( $loop->have_posts() ) :
// do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
?>
<div id="product-list">
<?php
while ( $loop->have_posts() ) :
$loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
?>
</div>
<?php
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
else :
do_action( 'woocommerce_no_products_found' );
endif;
wp_reset_postdata();
do_action( 'woocommerce_after_main_content' );
do_action( 'woocommerce_sidebar' );
get_footer(); ?>
Perhaps my error is immediately obvious.
As I walk through my code I will highlight the questions I have ran into that I cannot find answers to.
Starting with the line:
$display_count = $atts['per_page'];
echo $display_count; // NO value - How do I access this?
It was my understanding that these variables ($atts['per_page']
, $atts['order_by']
, $atts['order']
) were included here: /includes/class-wc-shortcodes.php
.
However, I can only see this attribute set up for individual product pages unless I am mistaken?
Am I allowed to use the variables set up within $atts[]? This also goes for the uses of these variables further down the page in the $args array
How do I set these up so I can use them?
Where are these values stored and changed?
When writing a custom AJAX pagination script into the functions.php file, are these variables readily available here also? If not, how can I set these up?
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo $page; // returns 1 - CORRECT;
Setting up a page variable to help me keep track of the page I am on.
$offset = ( $page - 1 ) * $display_count;
echo $offset; // returns 0 - CORRECT;
Creating the offset variable to put into the args so it can be told how many products will be offset, will come in super handy in the pagination.
$loop = new WC_Product_Query( $args );
Building the loop of products associated with the $args provided.
Is this the best query handler to use? I have seen a host of others such as:
$loop = new WP_Query( $args );
$loop = wc_get_products( $args );
The tail end of the code I am just trying to output the products found using as much of the WooCommerce base template structuring as possible.
EDIT: Ideally it would also show me how to make use of the WooCommerce loop as I have used a plug-in add on to the WooCommerce plug-in that helps me re-order the products into a custom order. It is this custom ordered product list I wish to pull through.
Upvotes: 4
Views: 12948
Reputation: 136
Basic query to loop through WooCommerce products
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, // -1 will get all the product. Specify positive integer value to get the number given number of product
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) : $the_query->the_post();
// Get default product template
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul>
Upvotes: 7