Custom sort order over-rides shortcode sorting
-
I have found some odd behaviour relating to a custom sort order I have implemented and some shortcode I use on my front page. I have followed the many examples online of how to add a custom sort order to the shop pages, and my code is this:
// Add "Sort by discount" to sorting options. Defaults to biggest to smallest discount. add_filter('woocommerce_get_catalog_ordering_args' ,'mycode_woocommerce_add_salediscount_to_catalog_ordering_args' ); function mycode_woocommerce_add_salediscount_to_catalog_ordering_args( $args ) { $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters('woocommerce_default_catalog_orderby' ,get_option('woocommerce_default_catalog_orderby' ) ); if ( 'discount' == $orderby_value ) { $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; $args['meta_key'] = 'discount_amount'; } return $args; } add_filter('woocommerce_default_catalog_orderby_options' ,'mycode_woocommerce_add_salediscount_to_catalog_orderby' ); add_filter('woocommerce_catalog_orderby' ,'mycode_woocommerce_add_salediscount_to_catalog_orderby' ); function mycode_woocommerce_add_salediscount_to_catalog_orderby( $sortby ) { $sortby['discount'] = __( 'Sort by discount', 'woocommerce' ); return $sortby; }It sorts based on a meta value (discount amount) that every product has. This is added to a simple plugin I use for my own code snippets. It works perfectly, in that I can sort by discount in my shop and I can also select ‘Sort by discount’ as the default sort order in the customize panel.
However. I have some shortcode on my frontpage, using the Elementor shortcode widget, that looks like this:
[products limit="4" columns="4" orderby="date" order="DESC" category="Locking carabiners"]This also works fine, EXCEPT when I set the default sort order for my site to be “Discount amount”, the shortcode no longer displays the products in order of date but by discount amount. It’s like the default sort order is over-riding the shortcode ordering but only when my custom sort order is the default. This doesn’t happen with the built in sorting options like price or average rating. For example, if I set the default sort order of my shop to be “price: low to high”, the shortcode will still sort by date. As it should.
Just to further throw a spanner in the works, I’ve noticed that if my default sort order is “discount amount” then search results sort by relevance (despite saying “discount amount” in the dropdown box). But if I set the default sort order to one of the built in ones, then it works fine on the search results page. This might be a different issue but I thought I’d mention it.
Does anyone know why any of this this happens?
The page I need help with: [log in to see the link]
The topic ‘Custom sort order over-rides shortcode sorting’ is closed to new replies.