Skip Navigation

[Resolved] Order archive products by price not working

This thread is resolved. Here is a description of the problem and solution.

Problem:
Order archive products by price not working

Solution:
You can use WordPress action hook "pre_get_posts" to change the archive order.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/order-archive-products-by-price-not-working/#post-1150419

Relevant Documentation:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by ivanJ 6 years ago.

Assisted by: Minesh.

Author
Posts
#1149437

Hello,

I'm trying to order product by regular price with the field _regular_price but it's not affecting the order.
There is also a custom filter on the left side which is also not working properly. The filter is set as the custom fields of the products with all OR relationships. Because multiple laptops (in example) can have 3GB of RAM and INTEL I3 as a processor.
hidden link -> that's the archive, this is the name of that WordPress archive "Proizvodi, Kategorije, Oznake, Kategorije proizvoda, Proizvod oznake, Kategorije"

Best regards,
Ivan

#1149813

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - the access details you shared with us is not working. Could you please send me working access details so I can check your issue further.

I have set the next reply to private which means only you and I have access to it.

#1149874
#1150419

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - The default shop page of Woocommerce plugin is archive page of post type "Products", and the query of WordPress archive page is setup by wordpress, Views cannot change it, but you can try with WordPress action hook "pre_get_posts":

So, I've added the following code at "Custom Code" section:
=> hidden link


function sort_by_regular_price($query) { 
    if ($query->is_post_type_archive('product') && $query->is_main_query()) {
    $query->set('order', 'ASC');
    $query->set('meta_key', '_regular_price');
    $query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'sort_by_regular_price');

More info:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Could you please confirm:
=> hidden link

#1150832

Hello Minesh,

Yes, it works perfectly now, awesome, thanks.

But another issue is still present. Please, when you click on multiple filter choices, it doesn't show good. It only shows good when I have 1 filter checked.

When I choose more of them, it throws "No items found" (on my Croatian language of course).
hidden link

I hope you understand what I'm trying to describe, if not, I'll make a video, no problem 🙂

Best,
Ivan

#1151110

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As your original issue is resolved. I have split the ticket for your another issue. Please feel free to resolve this ticket.

This will help us to write correct resolution summery as well as help other users searching on the forum for the specific issue.

We will continue with your new issue with the following ticket:
=> https://toolset.com/forums/topic/split-order-archive-products-by-price-not-working-multiple-filters-applied-not-working/

#1155694

Hello Minesh,

I've ran into another problem. You see, I've modified you code by adding one of the categories into your custom code like this:


<?php
/**
 * New custom code snippet.
 */

function sort_by_regular_price($query) { 
    if ( $query->is_post_type_archive('product') || $query->is_product_category() ) {
    	$query->set('order', 'ASC');
    	$query->set('meta_key', '_regular_price');
    	$query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'sort_by_regular_price');

And now my navigation "Max Mega Menu Pro" is not working/showing up on those category pages - hidden link

Can you please check it out?

Thanks

#1156193

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - could you please share few screenshots pointing out whats not working?

#1157289

Hello Minesh,

Sorry it took me this long. I've made you a video showing what's wrong 🙂

Here's the link: hidden link

Best regards,
Ivan

#1157771

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the wp-admin access details you shared before is not working now at this end.

Could you please resend me working access details so I can look at it whats going wrong there.

I have set the next reply to private which means only you and I have access to it.

#1161709
#1162686

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've changed the code as given under:

function sort_by_regular_price($query) { 
    if ( $query->is_post_type_archive('product') or  $query->is_tax('product_cat') ) {
    	$query->set('order', 'ASC');
    	$query->set('meta_key', '_regular_price');
    	$query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'sort_by_regular_price');

I can see the order works now:
hidden link

#1163109

My issue is resolved now. Thank you!