Im trying to get proper display of pagination and result count after running my custom query.
Right now results of query are displayed properly but seems to me that result count and pagination is displayed for all product in shop.
After digging into problem i found out that i created separated query object instead of replacing main query. I tried to fix this following the guide here : How to fix pagination for custom loops?
.. but this is a wordpress way and i need to transform this :
global $loop ;
$args = array(
'post_type' => 'product',
'posts_per_page' =>$per_page,
'meta_query' => array(
array(
'key' => '_product_brand',
'value' => $val,
)
)
);
$loop = new WP_Query( $args );
// pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $loop;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
$wp_query = NULL;
$wp_query = $temp_query;
..into woocommerce function. So far i've tried changing global variable $loop for $woocommerce_loop but it's producing errors.
I also tried to pass $paged attribute like :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' =>$per_page,
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_product_brand',
'value' => $val,
)
)
);
and also like :
$args['paged'] = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
Both of methods failed, they properly displaying products but not pagination neither result count.
What's the issue here? I would really appreciate for any help, thank you.
#EDIT 1 After following CFX guide : https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
Pagination and result count started to finally work. My issue here was using WP_Query instead of wc_get_products.
Following the woocommerce documentation for wc_get_products function : https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
i achieved proper display of products with my custom key. But not all of the arguments from documentation are working for me. For instance i can't display products from category nor products with given price.
$all_products = wc_get_products(array(
'category' => array( 'suspension' ),
'price' => 59.99,
'regular_price' => 59,
'sale_price' =>159,
));
I tried to pass those arguments one by one, everything according to documentation so it should work but it isn't. It's producing this error :
Warning: array_map(): Argument #2 should be an array in E:\xampp\apps\wordpress\htdocs\wp-content\plugins\woocommerce\includes\data-stores\class-wc-product-data-store-cpt.php on line 1673
Warning: array_filter() expects parameter 1 to be array, null given in E:\xampp\apps\wordpress\htdocs\wp-content\plugins\woocommerce\includes\data-stores\class-wc-product-data-store-cpt.php on line 1673
#EDIT2
arguments :
$all_products = wc_get_products(array(
'product_category' => 'suspension',
));
handler :
if ( ! empty( $query_vars['product_category']) ) {
$query['tax_query'][] = array(
'taxonomy' => 'product_cat',
'terms' => esc_attr( $query_vars['product_category'] ),
);
return $query;
}
I want to display products from parent category with given slug. This code is working only if i pass category id. If i pass slug as argument (like in code above) loop is displaying all products in the shop.
$wp_query = NULL; $wp_query = $loop;
. Why assign a NULL if you want to assign another value right after that. And why assign anything towp_query
if right after that you use$loop
to loop your posts...