Support » Plugin: WooCommerce » Custom sort order over-rides shortcode sorting

  • Resolved jimmcwibb

    (@jimmcwibb)


    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]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Hannah S.

    (@fernashes)

    Automattic Happiness Engineer

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    jimmcwibb

    (@jimmcwibb)

    Thanks Hannah.

    I’ve been going through the sort code but haven’t quite got my head around the flow of it all yet. I can’t immediately see a way in which the default options are treated differently to those added with hooks.

    Warren

    (@rabbitwordpress)

    Hi @jimmcwibb

    I have an issue with the new ‘Relevance‘ option in search results that you mentioned above.

    In WC version 3.2.6 search results would default to ‘Sort by newness‘ which I renamed to ‘Sort by date put in stock‘ for my client.

    However since WC 3.3+ the new ‘Relevance‘ option has been added.

    I can see it is actually possible on the Storefront demo site:

    https://themes.woocommerce.com/storefront/?s=Kettle&post_type=product

    However here on my site it defaults to ‘Relevance‘:

    https://testsite.miltonsjewellers.com/?s=ring&post_type=product

    Is there a way to set the default back to ‘Sort by date put in stock‘ (newness) in the latest version of WooCommerce?

    Many thanks
    Warren

    • This reply was modified 4 months ago by  Warren.
    jimmcwibb

    (@jimmcwibb)

    OK I think I’ve got to the bottom of this. It’s not a problem with WC or wordpress. The code I’ve used is too simplistic and doesn’t account for all eventualities.

    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;
    }

    The function first checks to see if the user has selected sorting by looking in the GET. If it’s not there then it retrieves the default sort order set in WC.

    If either the GET or the default sort order are ‘discount’, then the discount sort order arguments are returned.

    The problem is that this isn’t always the desired outcome. In search results, the desired sorting could be ‘relevance’ – indeed this is the one wordpress puts in the dropdown box. But the above code doesn’t care. If the default sort order is ‘discount’ then it sorts by that.

    It seems to do the same for shortcode. For example the sorting could be “rand”, but the above code doesn’t check for that. It returns the discount arguments.

    The above code is repeated in almost exactly the same form on dozens of blogs/tutorials. It’s bad code. I will find a better way to do it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.