Support » Plugin: WooCommerce » Custom sorting options (asc/desc)

  • Resolved pbraun

    (@pbraun)


    Hi,

    I’ve added random ordering option in Appearence < Customize < WC < Product Catalog < Default Product sorting for my product lists using this code

    /**
     * Add custom sorting options (asc/desc)
     */
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    function custom_woocommerce_get_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 ( 'random_list' == $orderby_value ) {
    		$args['orderby'] = 'rand';
    		$args['order'] = '';
    		$args['meta_key'] = '';
    	}
    	return $args;
    }
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    function custom_woocommerce_catalog_orderby( $sortby ) {
    	$sortby['random_list'] = 'Random';
    	return $sortby;
    }

    It does add the options but choosing this options does not change product order to random 🙁
    Any thoughts on this ? Thanks a lot

    • This topic was modified 1 month ago by pbraun.
    • This topic was modified 1 month ago by pbraun.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Stuart Duff

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @pbraun,

    This documentation below describes how to add some customer sorting options to WooCommerce and seems like the same code you’ve referenced.

    https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

    When testing this code on my installations using the free Storefront theme the order of the products changes for me.

    Image Link: https://cloudup.com/ihw4EZOY956

    pbraun

    (@pbraun)

    Hey @stuartduff

    But it adds the option in the customizer
    Appearence < Customize < WC < Product Catalog < Default Product < Random

    When choosing Random it does not behave as expected. I’ve implemented it on 2 versions of my site.

    – it works here. If you refresh products changes.
    – but it does not on this one : staging site

    even though I got the exact same code :-/

    • This reply was modified 1 month ago by pbraun.
    • This reply was modified 1 month ago by pbraun.
    Plugin Support Stuart Duff

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @pbraun,

    When you find that code functions on one site but not another that behaviour indicates that something on the site the code is not working on is having a negative effect and preventing that from functioning as expected.

    The only way to try and find out the cause would be to debug and conflict test the site which the code is not working as expected on to try and locate the culprit.

    https://docs.woocommerce.com/document/how-to-test-for-conflicts/

    I hope this helps.

    Plugin Support Amanda

    (@amandasjackson)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

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