Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19466

    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)) {

    switch ($_SESSION) :

    case ‘date_asc’ :

    $args = ‘date';

    $args = ‘asc';

    $args = ”;

    break;

    case ‘price_desc’ :

    $args = ‘meta_value_num';

    $args = ‘desc';

    $args = ‘_price';

    break;

    case ‘title_desc’ :

    $args = ‘title';

    $args = ‘desc';

    $args = ”;

    break;

    case ‘price_desc’ :

    $args = ‘meta_value_num';

    $args = ‘asc';

    $args = ‘_sku';

    break;

    endswitch;

    }

    return $args;

    }

    add_filter(‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’);

    function custom_woocommerce_catalog_orderby( $sortby ) {

    $sortby = ‘Reverse-Alphabetically';

    $sortby = ‘Price (highest to lowest)';

    $sortby = ‘Oldest to newest';

    $sortby = ‘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

    #103810

    1) My php code linter doesn’t show any errors – the code syntax itself seems to be correct. Not sure if it really works though. It probably won’t work with WC2.0 because it uses a SESSION variable at the top and afaik they switch from sessions to a cookie/transient based system with WC2.0). However it should work with WC1.6.x.

    2) Yes just use the sku option field on the product editor page.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Sort products by SKU’ is closed to new replies.