0

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.

  • Sorry, but your code makes no sense at all... There is no point in this part: $wp_query = NULL; $wp_query = $loop;. Why assign a NULL if you want to assign another value right after that. And why assign anything to wp_query if right after that you use $loop to loop your posts... – Krzysiek Dróżdż May 23 '18 at 22:18
  • I did those things following the guide. In my understanding zero'ing the main query has to eliminate potentional errors during overriding. Then the purpouse of the assigning my $loop into wp_query is for making pagination works. My loop is outputing posts but it doesn't impact original query which is resposible for pagination and result count. I saw this kind of code working in guide (link provided in question above), but my case in slightly different than that because im using woocommerce instead plain wordpress. – Bartosz Was May 24 '18 at 12:17
  • it makes no sense at all. If you assign value to variable, then you just assign it - there's no point in doing it twice. And if you don't modify global $wp_query then there's no point in storing it some temp variables and so on. Change the tutorial, because this one is really bad - author doesn't understand basics of PHP nor WP. PS. WooCommerce and other third-party plugins are off-topic on this site. – Krzysiek Dróżdż May 24 '18 at 12:26
  • What object? You create this loop on your own in your code... – Krzysiek Dróżdż May 24 '18 at 12:30
  • Simplyfying my statement : $woocommerce_loop is fetching it results from somewhere. I thought that it's from wp_query (like in wordpress) but it seems to me that it's taking its results from other object and im trying to figure out this exact object. – Bartosz Was May 24 '18 at 12:41
1

You're complicating things quite a bit with some of your query syntax, and WooCommerce actually discourages using WP_Query() to retrieve products. They even claim it can end up breaking down the road.

I recently wrote an article on how to do what you want, complete with an example here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/

  • Yeah, that's exactly what i was looking for. Thank you! Now i have to transform all WP_Queries into wc_get_products. So i have a question for you: did you used complex queries in your project? I mean the ones with specified meta_key, meta_value, price range between given values etc.. ? – Bartosz Was May 25 '18 at 13:58
  • I haven't attempted that but it may work. Give it a shot and I'll do my best to help if you run into a problem. – cfx May 25 '18 at 17:45
  • I updated the post. Can you look into #EDIT1 and tell me if those arguments are working for you? – Bartosz Was May 29 '18 at 11:38
  • They're not working, but if I format them like you would format a WP_Query() then the taxonomy query works just fine. I'm not sure what you're trying to do with the price values, but you may need a custom meta_query key to accomplish what you want. Check the WC_Product_Query() class for valid arguments. – cfx May 29 '18 at 17:14
  • That's strange, core arguments aren't working. Seems like i need to inform woocommerce support. Yes i solved pricing with meta_query. FYI I'm using pricing with my custom filters - user is passing 'price_min' and 'price_max' and then im displaying products between given price range. Now im just wondering how to solve category problem. For categories with given id i used tax_query, but what with categories with given slug? For them category argument is broken.. – Bartosz Was May 29 '18 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.