phpwordpresswoocommerceproduct-variationssku

Generate the product variations SKUs from Woocommerce parent variable SKU


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

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.


Solution

  • 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