Customizing WooCommerce

Customizing WooCommerce

Another great feature to add is the ability to sort products by SKU number. THis is something left out on the standard WooCommmerce set up.

<?php


/**

 * 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/

 *

 * @param array $args the sorting args

 * @return array updated args

 https://gist.github.com/bekarice/1883b7e678ec89cc8f4d

 */

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

	}

		///added added } also

		if ( 'skudesc' == $orderby_value ) {

		$args['orderby'] = 'meta_value';

		$args['order'] = 'desc'; // lists SKUs alphabetically desc for reverse alphabetical

		$args['meta_key'] = '_sku';

			////end 

	}



	return $args;

	

	

	///

	

}

add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );


?>