sorting by name and sku number in woo commerce


However, I wanted to add some sorting options that use the WooCommerce’s custom attributes. According to this thread, the product attributes are saved in a serialized format in the database and therefore it’s not feasible to do it directly.

To use the provided WooCommerce filters the attributes should be saved as meta data.

When a post that has some custom attributes set is updated, the submitted data ($_REQUEST) contains the following:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'attribute_names' =>
 array (
 0 => 'pa_pub-year',
 1 => 'pa_pub-author',
...
 ),
 'attribute_position' =>
 array (
 0 => '1',
 1 => '1',
......
'attribute_is_taxonomy' =>
 array (
 0 => '1',
......
 ),
 'attribute_values' =>
 array (
 0 => '1990',
 1 => '',
 2 => '',
......
 ),

The approach I followed is to get the submitted attributes and save them as post meta data. Afterwards, the code provided by WooCommerce to sort using attributes, with a tiny modification, worked successfully.

The code is as follows

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/************* Add sorting by attributes **************/
/**
 *  Defines the criteria for sorting with options defined in the method below
 */
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
function custom_woocommerce_get_catalog_ordering_args( $args ) {
    global $wp_query;
        // Changed the $_SESSION to $_GET
    if (isset($_GET['orderby'])) {
        switch ($_GET['orderby']) :
            case 'pa_pub-year' :
                $args['order'] = 'ASC';
                $args['meta_key'] = 'pa_pub-year';
                $args['orderby'] = 'meta_value_num';
            break;
        endswitch;
    }
    return $args;
}
/**
 *  Adds the sorting options to dropdown list .. The logic/criteria is in the method above
 */
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['pa_pub-year'] = 'Sort by Year: Older to Newer';
    return $sortby;
}
/**
 *  Save custom attributes as post's meta data as well so that we can use in sorting and searching
 */
add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
function save_woocommerce_attr_to_meta( $post_id ) {
        // Get the attribute_names .. For each element get the index and the name of the attribute
        // Then use the index to get the corresponding submitted value from the attribute_values array.
    foreach( $_REQUEST['attribute_names'] as $index => $value ) {
        update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
    }
}
/************ End of Sorting ***************************/

This should be placed in the functions.php file of your theme.

Advertisements

2 thoughts on “sorting by name and sku number in woo commerce

  1. Hi there,
    Would it possible to use this work around to sort by SKU?

    I’m hoping to be able to show products within a category by their SKU using shortcode on a specific page for example:

    [product_category category=”x-men-vol-4″ per_page=”99″ columns=”5″ orderby=”meta_value” order=”desc” meta_key=”_sku”]

    But am having no luck at all.

    You can see where I’m trying to apply this here:
    http://www.comicbookshop.co.nz/comic-books/marvel-comics/x-men/x-men-vol-4/

    I’ve requested help via wordpress.org forum here:
    http://wordpress.org/support/topic/category-order-by-sku-shortcode?replies=1

    But saw your post and thought I’d see if you might have some ideas.

    I hope you don’t mind me asking.

    Kind regards,
    JP

  2. Hi there,
    Would it possible to use this work around to sort by SKU?

    I’m hoping to be able to show products within a category by their SKU using shortcode on a specific page for example:

    [product_category category=”x-men-vol-4″ per_page=”99″ columns=”5″ orderby=”meta_value” order=”desc” meta_key=”_sku”]

    But am having no luck at all.

    You can see where I’m trying to apply this here:

    http://www.comicbookshop.co.nz/comic-books/marvel-comics/x-men/x-men-vol-4/

    I’ve requested help via wordpress.org forum here:

    http://wordpress.org/support/topic/category-order-by-sku-shortcode?replies=1

    But saw your post and thought I’d see if you might have some ideas.

    I hope you don’t mind me asking.

    Kind regards,
    JP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s