• devyadav

    (@devyadav)


    I would like to sort the products by their SKUs in ascending order. I had manually arranged them in descending order. But now I want them all in ascending order. By default WordPress sort them by Product IDs. I tried with the below code, but it did not work properly either. Please suggest.

    /**
     * Adds the ability to sort products in the shop based on the SKU
     * Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
     */
    
    function sv_add_sku_sorting( $args ) {
    
    	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    
    	if ( 'sku' == $orderby_value ) {
    		$args['orderby'] = 'meta_value';
        		$args['order'] = 'asc'; 
        		// ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
    		$args['meta_key'] = '_sku';
    	}
    
    	return $args;
    }
    add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );
    
    function sv_sku_sorting_orderby( $sortby ) {
    	$sortby['sku'] = 'Sort by SKU';
    	// Change text above as desired; this shows in the sorting dropdown
    	return $sortby;
    }
    add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
    add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator t-p

    (@t-p)

    I recommend asking at your ecommerce plugin’s support so the plugin’s developers and support community can help you with this.

    Thread Starter devyadav

    (@devyadav)

    Thank you for looking into this! I will ask them. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Products Sorting by SKU in Ascending Order’ is closed to new replies.