Sort by discount percentage rather than discount price
-
Hello!
I would like to configure the ‘sort by discount’ feature a little bit more.
As far as I can notice, the feature sorts by descending discount price.
Is it possible to sort by:
Descending discount percentage
Ascending discount percentageThank you
-
Hi @pickme,
Thanks for reaching out. Happy to help you with this!
When a discount/sale price is applied to WooCommerce products, it is saved as the sale price in the database and not stored as a percentage value. For this reason, it is not currently possible to sort by percentage values, as these are not stored values in the database, I’m afraid.
I’m happy to share this with our product team, however, to see if it’s something we might be able to add in a future release. Would you please tell us a little bit more about how this would benefit your business? I want to be sure to pass along as many details as possible!
Thanks!
MarcusHi!
I want to promote product categories, for example: Summer Sale up to 60%. Therefore, I can set a category, an attribute with archives enabled and with the sorting query, a URL is created!
All products on the shop have a sale price. Therefore, setting the sorting order by sale price does not make sense, since sorting order ‘Price:Low to High orders the Sale price and not the regular price.
Anyhow, I am using the snippet codes below to:
1) Calculate the discount percentage.
2) Place the sorting option in the sorting list.It works, the problem is that the discount calculation is not carried out when editing product price by WP’s ‘quick edit’ feature, but only with opening the product edit page and setting the product price with in it and saving the page.
Could you please help me out to accomplish calculating the discount percentage by editing a product price by WP’s ‘quick edit’?
Thank you!
// calculate and save discount amount on product save/edit add_action('woocommerce_process_product_meta', 'woo_calc_my_discount'); function woo_calc_my_discount( $product_id ) { $_product = wc_get_product( $product_id ); $regular = (float) $_product->get_regular_price(); $sale = (float) $_product->get_sale_price(); $discount = round( 100 - ( $sale / $regular * 100), 2 ); update_post_meta( $product_id, '_discount_amount', $discount ); }
Then this snippet:
add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' ); function misha_add_custom_sorting_options( $options ){ $options['_discount_amount'] = 'Sale: High to Low'; return $options; }
and this snippet:
add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' ); function misha_custom_product_sorting( $args ) { // Discount percentage: High to Low if( isset( $_GET['orderby'] ) && '_discount_amount' === $_GET['orderby'] ) { $args['meta_key'] = '_discount_amount'; $args['order'] = 'desc'; } return $args; }
Hi @pickme,
Nice work! Thanks for following up here with all that detail!
In the very first snippet you shared, it looks like you’re only targeting the
woocommerce_process_product_meta
hook. This hook is not run when you are doing a quick edit, so you’ll need to run this as well using thewoocommerce_product_quick_edit_save
hook.To be clear, you should be able to just add the following new “add_action” line to the first snippet, right below the existing “add_action” line:
add_action('woocommerce_product_quick_edit_save', 'woo_calc_my_discount');
Would you please give that a try and see if it works for you?
Thanks,
MarcusHi Marcus, I added
add_action('woocommerce_product_quick_edit_save', 'woo_calc_my_discount');
belowadd_action('woocommerce_process_product_meta', 'woo_calc_my_discount');
.I changed sale price of a product with the quick edit feature to alter position in the descending percentage order, but unfortunately it did not change position. It should be first since I increased it to 90% sale price, but remained in same place.
Changing the priority hook
add_action('woocommerce_product_quick_edit_save', 'woo_calc_my_discount');
is executed, would that fix it?I would be grateful for further help!!
Thank youHey @pickme,
Thank you for letting me know. I’m so sorry that this didn’t work correctly!
I believe it has to do with what information is being passed into the function. In your original function, the
$product_id
is being passed in. However, in the “Quick Edit” version, the$post
object is passed in.So… to get this to work, you will, unfortunately, need to add a second function that does basically the same thing, but using the $post object.
You would remove the “add_action” line that I provided before and add all of this to your code:
add_action('woocommerce_product_quick_edit_save', 'sv_woo_calc_my_discount_quickedit'); function sv_woo_calc_my_discount_quickedit( $post ) { $_product = wc_get_product( $post ); $regular = (float) $_product->get_regular_price(); $sale = (float) $_product->get_sale_price(); $discount = round( 100 - ( $sale / $regular * 100), 2 ); update_post_meta( $post->ID, '_discount_amount', $discount ); }
Would you please give that a try and see if it works for you?
Thanks,
MarcusHello Marcus!
I tested it and did not work! ??
I did the following tests:
1) I edited the sale price and hit quick save. > Product on shop page remained on same position even though its discount percentage was increased.
2) I edited the sale price within product page, saved it and product changed position. Then on same product, changed sale price again, I used quick save, but product did not change position.I am using plugin: Code Snippets and added the following 2 snippets on separate files:
// calculate discount percentage on product edit page edit/save add_action('woocommerce_process_product_meta', 'woo_calc_my_discount'); function woo_calc_my_discount( $product_id ) { $_product = wc_get_product( $product_id ); $regular = (float) $_product->get_regular_price(); $sale = (float) $_product->get_sale_price(); $discount = round( 100 - ( $sale / $regular * 100), 2 ); update_post_meta( $product_id, '_discount_amount', $discount ); }
// calculate discount percentage on product quick edit/save add_action('woocommerce_product_quick_edit_save', 'sv_woo_calc_my_discount_quickedit'); function sv_woo_calc_my_discount_quickedit( $post ) { $_product = wc_get_product( $post ); $regular = (float) $_product->get_regular_price(); $sale = (float) $_product->get_sale_price(); $discount = round( 100 - ( $sale / $regular * 100), 2 ); update_post_meta( $post->ID, '_discount_amount', $discount ); }
What would be a solution that would work?
I am grateful if you could help on this.Thank you very much!
-
This reply was modified 4 years, 7 months ago by
pickme.
Hi @pickme,
Thanks for following up. So sorry that this still isn’t working as expected!
I think I’ve tracked down the problem, though! It sounds like WordPress/WooCommerce doesn’t like the product being referenced by
$post->ID
directly. In the second snippet about, you’ll need to replace$post->ID
with$_product->get_id()
on the last line.Would you please give that a try? It should work this time!
Thanks,
MarcusMarcus!
It worked! ??
I don’t know how I can thank you for your help.
https://www.skyverge.com/ Simply Rocks!
I will contact you via your website.
Thank you Very Much!
Hi @pickme,
Excellent – thank you for confirming this for me! Happy to hear that we were able to get it working for you! We appreciate the kind words and the review you left here, too.
I’m going to mark this one as “resolved”, but if you have any other questions, please don’t hesitate to let me know.
Cheers!
MarcusHello Marcus! How are you?
I’ve noticed that ‘variable’ products are being included within the discount percentage sorting array of products even without inserting a sale price to the product, and moreover, if a sale price is inserted, the variable product is not sorted according to its discount percentage.
I would be grateful if you could possibly provide any help on this?
Thank you!
-
This reply was modified 4 years, 6 months ago by
pickme.
Hi @pickme,
Thanks for reaching back out here with this question. We appreciate your feedback and knowing what is important to you.
Since the code provided above is not covered under our support policy, I’m afraid I won’t be able to further modify it or implement it directly.
If you are interested in continuing to take this code further, I’m happy to recommend a developer or two that can look at this with you.
Would you please let me know if you’d like me to make a couple of recommendations?
Thanks,
MarcusHello Marcus!
This is fine, I will manage it. Thank you very much for your response!
Regards!
@pickme -
This reply was modified 4 years, 7 months ago by
- The topic ‘Sort by discount percentage rather than discount price’ is closed to new replies.