0

Currently in Woocommerce, when handling variable products, the SKU needs to be defined for each variant in full.

What I would like to do, is define a parent SKU, and then define suffix to SKU for each variant, and the final SKU is produced by concatenating the parent plus the variant suffix.

So lets say I have a parent product of base SKU PRODUCT_0001, the SKU PRODUCT_0001 I would like to define here:

BASE

Then for variants, say VARIANT_A, I would like to define the suffixes here:

VARIANT

Now the final SKU for the variant of the product (ie what is transmitted during order workflow), I would like to represent as:

PRODUCT_0001/VARIANT_A

This way, this prevents double-handling of the same base product code when setting-up/defining many variants.

Is there an easy way to accomplish this?

Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88

1 Answers1

0

I have not tried this on email, but this works on dashboard area and frontend..

add_filter( 'woocommerce_get_sku', 'reigel_woocommerce_get_sku', 10, 2 );
function reigel_woocommerce_get_sku( $sku, $product ){
    // remove current filter to prevent unli loop on get_sku()
    remove_filter( current_filter(), __FUNCTION__ ); 
    if ( $product->get_type() === 'variation' ) {
        $parent = $product->parent->id?$product->parent->id:$product->get_parent();
        $sku = wc_get_product($parent)->get_sku() . '/' . $sku;
    }
    add_filter( 'woocommerce_get_sku', 'reigel_woocommerce_get_sku', 10, 2 );
    return $sku;
}

disable sku uniqueness on variation...

add_filter( 'wc_product_has_unique_sku', 'reigel_wc_product_has_unique_sku', 10, 3 );
function reigel_wc_product_has_unique_sku( $sku_found, $product_id, $sku ) {
    $product = wc_get_product($product_id);
    if ( $product->get_type() === 'variation' ) {
        $sku_found = false;
    }
    return $sku_found;
}
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Sorry, spoke too soon, getting error "Product Variant SKU's Must be Unique". In the above case, the "Uniqueness" is achieved when the variant SKU is concatenated to the base SKU, – Nicholas Hamilton Mar 17 '16 at 23:44
  • this is only for displaying it... checking for uniqueness is different story and might be impossible in your case unless you will enter the sku of the parent too... – Reigel Gallarde Mar 18 '16 at 00:13
  • Well the uniqueness check is conducted each time a new variant product is created, so the above is useless unless the uniqueness check can be suppressed or modified to check uniqueness on the value generated by: `wc_get_product($parent)->get_sku() . '/' . $sku;` – Nicholas Hamilton Mar 18 '16 at 01:32
  • you can disable that function on variations level... I updated the code.. but this has very simple logic applied... you can expand it... example, check it's siblings if sku is found... – Reigel Gallarde Mar 18 '16 at 03:17