Order woocommerce products alphabetically by second word in title?

I’ve achieved this in a slightly different way.

In the single product edit page, Woocommerce (WC) has the ability to create custom fields. (if you can’t see the ‘custom fields’ area in the edit product page, select ‘screen options’ at the top right of the wordpress admin page and check ‘custom fields’)

So, I created a ‘surname’ custom field on one product, exported all products via the built in WC export functionality, so I could see where the one new ‘surname’ field was on a csv spreadsheet.

Then, within the spreadsheet, I copied all of the second words into the new custom field and then imported it back in, which populated every product with the new custom field and their corresponding value.

Once that was done, I found a couple of snippets of code that I add to my functions.php. This added the ‘surname’ field to the ‘orderby’ sorting.

/**
 * Adds WooCommerce catalog sorting options using postmeta, such as custom fields
 * Tutorial: http://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/
**/
function skyverge_add_postmeta_ordering_args( $sort_args ) {

$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {

    // Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
    case 'surname':
        $sort_args['orderby']  = 'meta_value';
        // Sort by meta_value because we're using alphabetic sorting
        $sort_args['order']    = 'asc';
        $sort_args['meta_key'] = 'surname';
        // use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
        break;  
}

return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );
// Add these new sorting arguments to the sortby options on the frontend
function skyverge_add_new_postmeta_orderby( $sortby ) {

// Adjust the text as desired
$sortby['surname'] = __( 'Sort by surname', 'woocommerce' );

return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );

/**
 * This code should be added to functions.php of your theme
 **/
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby'); function am_woocommerce_catalog_orderby( $args ) { $args['meta_key'] = 'surname'; $args['orderby'] = 'meta_value'; $args['order'] = 'asc'; return $args; }

If anyone else uses this, you can name your custom field whatever you want, within the edit product page of WC. Just make sure that you change all instances of ‘surname’ in the code above to whatever you do use.

It then occurred to me that I could do the same thing with the sku code:make this the second word then sort by it.

However, I can’t get this to work. I’m using the WOOF product filter plugin which also has to be modified to make the custom field sorting work. I think this is the problem.

I’m sure I’ll work it out though and the above will work with regular WC stores. Hope that helps someone else