Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add Custom Input Field in WooCommerce Admin for Product Variation SKU Field ?

If you own an online store that sells multiple product variations, you might need distinct identifiers for each variation. You can use the built-in SKU field for this purpose, but there are situations where additional fields are necessary for more effective product variation management.

With WooCommerce customization, we can add a special field called ‘CustomSKU’ on the admin side for each variation of your products. This allows store owners to assign unique identifiers (CustomSKUs) to each variation of a product in addition to the standard SKU field. Let’s dive into the implementation steps! 

Solution: Add Custom Input Field in WooCommerce Admin for Product Variation SKU Field

The code adds a custom SKU field to each product variation in WooCommerce so that you can set a unique SKU to track product variations effectively.

add_action('woocommerce_variation_options', 'ts_field_variable', 10, 3);

function ts_field_variable($loop, $variation_data, $variation){
    woocommerce_wp_text_input(
        array(
            'id' => '_customsku', // Corrected field ID to match the saving function
            'name' => '_customsku[' . $loop . ']', // Add name attribute 
            'value' => get_post_meta($variation->ID, '_customsku', true),
            'label' => esc_html__('CustomSKU', 'my_field'),
            'desc_tip' => true,
            'description' => __('Enter the CustomSKU', 'my_field'),
        )
    );
}

// Save the field inputted value
add_action('woocommerce_save_product_variation', 'ts_save_variation_custom_sku_input_field_value', 10, 2 );

function ts_save_variation_custom_sku_input_field_value($variation_id, $i) {
    if(isset($_POST['_customsku'][$i])) {
        $custom_sku = sanitize_text_field($_POST['_customsku'][$i]);
        update_post_meta($variation_id, '_customsku', $custom_sku);
    }
}

Output

When an admin edits a variable product and selects a variation, a CustomSKU field will be displayed. Any value entered and saved in this field will be retained, allowing the admin to see and manage custom SKUs for each variation.

How to Add Custom Input Field in WooCommerce Admin for Product Variation SKU Field ? - Tyche Softwares

This customization adds a special input field for product variations, making it easier for admins to track and manage inventory. Additionally, similar customizations can add input fields in the admin area and also display the admin entered values on the product page. This helps customers to access more detailed information about the product.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x