[Resolved] Sort by Price isn't working

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.

This topic contains 17 replies, has 2 voices.

Last updated by Ido Angel 1 month, 3 weeks ago.

Assigned support staff: Waqar.

Author
Posts
#1609771

Hey,

I have this to sort woocommerce products by price:

[wpv-sort-orderby type="radio" options="field-views_woo_price" label_for_field-views_woo_price="Field - views_woo_price" orderby_ascending_for="field-views_woo_price"][wpv-sort-order type="select" options="asc,desc" label_for_asc="Ascending" label_for_desc="Descending" label_asc_for_field-views_woo_price="מחיר: מהגבוה לנמוך" label_desc_for_field-views_woo_price="מחיר: מהנמוך לגבוה"]

but it simply doesn't work.

I tried the initial order to be numeric, string, native field - nothing works.

Any ideas?

Thanks
Ido

#1610353

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thank you for contacting us and I'd be happy to assist.

I've performed some tests on my website and this sorting shortcode works:


[wpv-sort-orderby type="radio" options="field-views_woo_price" label_for_field-views_woo_price="Field - views_woo_price" orderby_as_numeric_for="field-views_woo_price" orderby_ascending_for="field-views_woo_price"]
[wpv-sort-order type="select" options="asc,desc" label_for_asc="Ascending" label_for_desc="Descending" label_asc_for_field-views_woo_price="Ascending" label_desc_for_field-views_woo_price="Descending"]

Comparing this with the shortcode that you've shared, your "wpv-sort-orderby" shortcode seems to be missing the orderby_as_numeric_for="field-views_woo_price" attribute, which is added when "numeric" comparison type is selected.
( screenshot: hidden link )

Note: To check if the correct price is saved in the "views_woo_price" field with your products, you can include the following shortcode in your view's loop output:


[wpv-post-field name="views_woo_price"]

You can go to WP Admin -> Toolset -> WooCommerce Views and use the "Calculate Now" button at the bottom to update this price and other static product fields.

I hope this helps and please let me know how it goes.

regards,
Waqar

#1610359

thanks Waqar!
It looks better, but the calculation is still off. when I sort initial in "ascending" - it's fine. but when I turn to "descending", the calculation is not according to actual price, but to regular price.
you can see here:

hidden link

cheers
ido

#1610693

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thanks for writing back.

Please note that Toolset WooCommerce Views plugin stores "Sale price" ( or current actual price ) in the field "views_woo_price" and not the "Regular price". This means that this filter will also use the same sale or actual price.

When the sorting by price is enabled, the default ordering is automatically set to use this price field too.
( screenshot: hidden link )

Can you please confirm if you have the same settings for the "Ordering" section?

In case the ordering settings are the same, but the ordering issue persists, I'll recommend to:

1. Make sure that WordPress, active theme, and plugins are all updated to the latest versions.

2. It would be interesting to test this with all non-Toolset plugins disabled and a default theme like Twenty Twenty.

If it's fixed, you can start adding the disabled items, one-by-one, to narrow down to a possible conflicting one.

regards,
Waqar

#1610735

thanks Waqar!

Still not working. Before I try to debug with a different theme - two questions please:
1) maybe since there's a ₪ sign (new shekel) attached it should be string and not number?
2) maybe instead of using views_woo_price i can use the _sale_price field in both initial ordering and the sorting shortcode? wouldn't that solve the issue?

#1612489

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

1) maybe since there's a ₪ sign (new shekel) attached it should be string and not number?

- The price fields don't store the currency sign in the database value which is used for the query and sorting operations as that would bring in unexpected results.

And using string comparison is a bad idea for numerical values since 100 will be considered smaller value than 11, which is not what we want.

2) maybe instead of using views_woo_price i can use the _sale_price field in both initial ordering and the sorting shortcode? wouldn't that solve the issue?

- You can try using the "_sale_price" field too, but using the "views_woo_price" is more reliable because:

a). for a simple product where the sale price is set, the "views_woo_price" field will also have the same value

b). for a simple product where the sale price is not set, "_sale_price" will return empty value, whereas the "views_woo_price" field will return the actual price value.

c). for a variable product, the "_sale_price" field will return empty value, whereas the "views_woo_price" field will return the lowest sale price set from all the available variations.

Please continue the troubleshooting with all extra plugins disabled and a default theme and let me know how it goes.

I'm also setting the next reply as private, in case you need to share temporary admin login details.

regards,
Waqar

#1612795

Wait - still doesn't work. I think it's because of infinite scrolling. would you mind checking?

#1616609

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thank you for sharing the admin access.

I noticed that the sorting controls have been removed from the view "Products Slider Home", but the order by "views_woo_price" field is working as expected.

To confirm, I've created a new test view "Temp view for products" which can be seen on the page yourwebsite.com/view-temp-view-for-products/

You'll see that the ordering and the sorting controls both are working and the post order is the same as the ones shown on the homepage.

Please let me know if I'm missing something.

regards,
Waqar

#1616619

Thx Waqar!
Inky difference you dont use "load more" with infinite scroll, which i think scrambled everything...

#1619621

And another thing... if you check this view, you'll see that the infinite scroll (i'm using a button for this) loads duplicate results. There's another view in the page with infinite scroll, for articles (and not products) which works fine

#1619701

take a look in your view, after changing to "ascending" and loading more results (image attached)

#1623727

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thank you for sharing these details and I apologize for the delay in getting back on this.

I'm currently performing some tests with a similar view on my own website and will share my findings, shortly.

Thank you for your patience.

regards,
Waqar

#1625405

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

Thank you for waiting, while I performed some tests and troubleshooting around sorting and infinite scroll feature.

On my test website, when the infinite scrolling is enabled, the results keep loading as the page is scrolled downwards and the "Pagination controls" button is disabled from the view's "Search and Pagination" and "Loop Editor" sections. This suggests that this infinite scroll feature is not designed to work with the regular pagination feature.

On your website, I don't see automatic results being loaded when the page is scrolled with infinite scrolling and it is possible that some custom code has been used to prevent that. And since pagination isn't supported with infinite scrolling "wpv-pager-next-page" shortcode brings in incorrect results.

Note: I tried some workarounds to make pagination and infinite scrolling work together, but unfortunately that didn't work.

Another issue is that results are not shown in the correct order on your website with respect to "views_woo_price" field, when the sorting controls are changed, even with infinite scrolling disabled.

That issue is the result of "Post Types Order" plugin and disabling the "Admin Sort" option from its settings fixes it.

I hope these findings will help.

regards,
Waqar

#1625589

Thx Waqar!
The code to prevent infinite scroll (and actually apply it to clicking a button) was something I caught from this forum 🙂

#1626217

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ido,

I can look into that custom code if you could share the link to that forum thread or the code that you've used directly.

regards,
Waqar