Skip to content

Instantly share code, notes, and snippets.

@kloon
Created January 23, 2013 10:23
Show Gist options
  • Save kloon/4604097 to your computer and use it in GitHub Desktop.
Save kloon/4604097 to your computer and use it in GitHub Desktop.
WooCommerce Custom Orderby
<?php
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
if( isset ( $_SESSION['orderby'] ) ) {
switch ( $_SESSION['orderby'] ) {
case 'sku_asc' :
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
$args['meta_key'] = '_sku';
break;
}
}
return $args;
}
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['sku_asc'] = 'SKU';
return $sortby;
}
?>
@aliasjimbo
Copy link

Good morning, thanks for the input, I'm new to this and am learning, my question is if this code snippet is used to sort the products in my shop WooCommerce according sku, and if so what should I do? Where shall I put it? Thank you very much in advance and sorry for my English.

@bentasm1
Copy link

alias: it goes in your themes functions.php. Google "theme functions.php" and you'll see walkthroughs of how to paste code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment