2

In WooCommerce I would like to generate the product variations SKUs from the parent variable product SKU like:

  • Variable Product sku: 1200
  • 1st Product Variation SKU: 1200-01
  • 2nd Product Variation SKU: 1200-02
  • 3rd Product Variation SKU: 1200-03
  • … and so on …

So I need an SKU in the order (and shown on the product page when choosing the variation). For example, 1200-01

I can not figure out how to achieve that in WooCommerce. I worked with other shop systems before and this was a very basic setting to get the variations running.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Neil Fender
  • 75
  • 1
  • 10

1 Answers1

4

You can use multiple ways to generate a combined SKU for product variations from the parent variable product. Here are two of them:

1st way:

The following code will just generate for front end display ("view") a combined sku when the product uses the WC_Product method get_sku() (but will not generate and save combined variation SKUs from the variable product like in the other way):

add_filter( 'woocommerce_product_variation_get_sku', 'custom_variation_sku', 10, 3 );
function custom_variation_sku( $sku, $product ) {

    if( $main_sku = get_post_meta( $product->get_parent_id(), '_sku', true ) ){
        $sku = $main_sku . '-' . sprintf( '%02d', ( get_post($product->get_id())->menu_order ) );
    }

    return $sku;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


2nd way:

The following will auto generate the product variations SKUs from the variable product SKU.

The first function will add a checkbox to the variable product settings in "Advanced" tab. Only when you will check this checkbox, the 2nd function will generate all the product variations SKUs from the variable product SKU.

// Custom checkbox for auto generation of the variations SKUs
add_action('woocommerce_product_options_advanced', 'additional_product_options_advanced_custom_checkbox');
function additional_product_options_advanced_custom_checkbox()
{
    global $post;

    echo '<div class="options_group show_if_variable hidden">';

    // Custom checkbox located on Advanced tab settings
    woocommerce_wp_checkbox( array(
        'id'          => 'generate_variations_skus',
        'label'       => __('Generate variations SKUs', 'woocommerce'),
        'description' => __('Auto Generation of the variations SKUs from variable sku', 'woocommerce'),
        'desc_tip'    => 'true'
    ));

    echo '</div>';
}

// Auto generate the variations SKUs
add_action( 'woocommerce_admin_process_product_object', 'wc_auto_generate_variations_skus', 10, 1 );
function wc_auto_generate_variations_skus( $product ) {

    if( $product->is_type('variable') && isset($_POST['generate_variations_skus']) ) {
        $parent_sku   = $product->get_sku();
        $children_ids = $product->get_children();
        $count        = 0;
        
        // Loop through the variations Ids
        foreach( $children_ids as $child_id ){
            $count++;

            // Get an instance of the WC_Product_Variation object
            $variation = wc_get_product($child_id);
            
            // Set the prefix length based on variations count
            $prefix = sizeof($children_ids) < 100 ? sprintf('%02d', $count) : sprintf('%03d', $count);

            // Generate and set the sku
            $variation->set_sku( $parent_sku . '-' . $prefix );

            // Save variation
            $variation->save();
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

a) On Variable products "Advanced" Tab settings: Check the checkbox

enter image description here

b) Update the Variable product:

enter image description here

c) Now the SKUs numbers of All variations have been generated (keeping the order):

enter image description here

enter image description here

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • WOW, thank you very much. Both solutions are very helpful. Perfect! – Neil Fender Dec 28 '18 at 11:14
  • Hey LoicTheAztec, can I ask you anoher question? now we have Parent SKU for exaple: 1111 Variation SKU is for example: 89 (changes from Variaton to variotan) . We want in front end: 1111-89 – Neil Fender Jan 02 '19 at 09:53
  • Sorry for bad formatting, it does not do linebreaks here.

    So basically a combination ( and add a "-" in beetween): parentSKU-varSKU

    Thank you for help
    – Neil Fender Jan 02 '19 at 10:06