Need Help With WooCommerce Custom Sort

sharingsunshine used Ask the Experts™
I need a sort that uses the parent id which is called jk_sku.  I am on 5.1.1 WordPress and 3.5.7 for WooCommerce.  I only need this as an option for sort.  So following two articles on the net.  I put together this code

function jk_add_custom_sku() {
$args = array(
'label' => __( 'Parent ID', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
woocommerce_wp_text_input( $args );
add_action( "woocommerce_product_options_sku", "jk_add_custom_sku" );

function jk_save_custom_sku( $post_id ) {
// grab the custom SKU from $_POST
$custom_sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';

// grab the product
$product = wc_get_product( $post_id );

// save the custom SKU using WooCommerce built-in functions
$product->update_meta_data( 'jk_sku', $custom_sku );

add_action( "woocommerce_process_product_meta", "jk_save_custom_sku" );
//add_action( "save_post_product", "jk_save_custom_sku" );

####### Custom Sort Option ######################

// 1. Create new product filter
add_filter( 'woocommerce_get_catalog_ordering_args', 'bbloomer_sort_by_name_woocommerce_shop' );
function bbloomer_sort_by_name_woocommerce_shop( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) wp_unslash( $_GET['orderby'] ) ) : wc_clean( get_query_var( 'orderby' ) );
    if ( 'jk_sku' == $orderby_value ) {
        $args['orderby'] = 'jk_sku';
        $args['order'] = 'asc';
        $args['meta_key'] = '';
    return $args;
// 2. Add new product filter to Sorting dropdown
add_filter( 'woocommerce_catalog_orderby', 'bbloomer_load_custom_woocommerce_catalog_sorting' );
function bbloomer_load_custom_woocommerce_catalog_sorting( $options ) {
$options['jk_sku'] = 'Sort by Parent ID (asc)';
return $options;

Open in new window

However, it doesn't seem to be honoring the parent id as the sort parameter.  You can see it in action at

Open in new window

just use the sort dropdown to change it to sorting on parent id.

The premise is that each of the products on sale have a parent product and I am trying to get the sort to group them as such.  The company I distribute for requires a separate sku for each sale offered.  

The default sorting is working only if the titles are exactly the same.  This is very tedious especially when there are a lot of products to put on sale.  Using the parent id I hoped to group them without having to be tedious with the titles of each sale product.

This is a look at all of the products on that page and the parent id is the last column.  Bulk Edit of Products
Please help me to get the sort to group according to the parent id only.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I believe you need a value here that indicated the parent id? : $args['meta_key'] = '';
place var_dump($args);

before the return ($args) to see if that looks right


Thanks for the help.  I put in jk_sku as the meta key and that fixed it.
@sharingsunshine, I'm glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial