Hi, I'm still working on the topic "sort by sku"
If I edit a product (grouped - quick edit) there is the field SKU. After studying forums in woothemes and other sources, I come out with a code like this
===============================================
/**
* This code should be added to functions.php of your theme
**/
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 'date_asc' :
$args['orderby'] = 'date';
$args['order'] = 'asc';
$args['meta_key'] = '';
break;
case 'price_desc' :
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
$args['meta_key'] = '_price';
break;
case 'title_desc' :
$args['orderby'] = 'title';
$args['order'] = 'desc';
$args['meta_key'] = '';
break;
case 'price_desc' :
$args['orderby'] = 'meta_value_num';
$args['order'] = 'asc';
$args['meta_key'] = '_sku';
break;
endswitch;
}
return $args;
}
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['title_desc'] = 'Reverse-Alphabetically';
$sortby['price_desc'] = 'Price (highest to lowest)';
$sortby['date_asc'] = 'Oldest to newest';
$sortby['sku_asc'] = 'Sort by SKU';
return $sortby;
}
===============================================
Before actually using the code, I would like to know:
- is my coding correct?
- how can I finally address/access the _sku?
In the single product page there is the field SKU - is it enough to use this or do I have to add a custom field?
Thanks for your assistance.
Best regards, Katharina














