WooCommerce is one of the best eCommerce solutions in the market. It helps people to sell various kinds of products easily. WooCommerce is a perfect fit for both developers and store owners.
What is Product Variation in WooCommerce
WooCommerce Product variation is considered as different variations of the same product. For example product in different sizes and colors etc.
When a product has different variations then the product from which the variations are created known as the main product. Each product variation has its own price and product information.
You can easily display similar products on your store using WooCommerce product variations.
In this tutorial, I will show you a brief about WooCommerce product variations using an example of a WooCommerce store that sells shirts. All the shirts are the same but they differ in color, design, and attributes.
Now each product variation has different prices, images, videos, and SKU numbers. However, each WooCommerce product variation is a different entity in the online store.
How to Create WooCommerce Variable Product with Attributes
This code defines the WooCommerce variable product ID using a custom function that actually creating a product variation. The parent product of the variable product is required for creating attributes such as the array of value, prices, SKU and stock.
All the data is stored in a formatted multidimensional array. This function checks whether the value of the attributes already exists and if not then it creates an entry for the WooCommerce product attribute and then it will set in the variable parent product. Add the following code snippet in functions.php file.
function create_WooCommerce_product_variation( $product_id, $variation_data ){ $product = woo_get_product($product_id); $variation_post = array( 'post_title' => $product->get_title(), 'post_name' => 'product-'.$product_id.'-variation', 'post_status' => 'publish', 'post_parent' => $product_id, 'post_type' => 'product_variation', 'guid' => $product->get_permalink() ); $variation_id = woo_insert_post( $variation_post ); $variation = new WooCommerce_Product_Variation( $variation_id ); foreach ($variation_data['attributes'] as $attribute => $term_name ) { $taxonomy = 'pa_'.$attribute; if( ! taxonomy_exists( $taxonomy ) ){ register_taxonomy( $taxonomy, 'product_variation', array( 'hierarchical' => false, 'label' => ucfirst( $attribute ), 'query_var' => true, 'rewrite' => array( 'slug' => sanitize_title($attribute) ), ) ); } if( ! term_exists( $term_name, $taxonomy ) ) woo_insert_term( $term_name, $taxonomy ); $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; $post_term_names = wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') ); if( ! in_array( $term_name, $post_term_names ) ) woo_set_post_terms( $product_id, $term_name, $taxonomy, true ); update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug ); } if( ! empty( $variation_data['sku'] ) ) $variation->set_sku( $variation_data['sku'] ); if( empty( $variation_data['sale_price'] ) ){ $variation->set_price( $variation_data['regular_price'] ); } else { $variation->set_price( $variation_data['sale_price'] ); $variation->set_sale_price( $variation_data['sale_price'] ); } $variation->set_regular_price( $variation_data['regular_price'] ); if( ! empty($variation_data['stock_qty']) ){ $variation->set_stock_quantity( $variation_data['stock_qty'] ); $variation->set_manage_stock(true); $variation->set_stock_status(''); } else { $variation->set_manage_stock(false); } $variation->set_weight(''); $variation->save(); }
woo_get_product($product_id) Gets the WooCommerce variable product object.
woo_insert_post( $variation_post ) create the WooCommerce product variation.
WooCommerce_Product_Variation( $variation_id ) create an instance of the WooCommerce_Product_Variation object.
register_taxonomy will create taxonomy if doesn’t exist.
woo_insert_term check if the term name exists and If not it will create a new term.
$post_term_names get the post Terms names from the parent variable WooCommerce product.
woo_set_post_terms checks if the post-term exists and, if not it will create in the parent variable product.
update_post_meta saves the attribute data in the WooCommerce product variation
$variation->set_sku saves all other data in SKU
$variation_data[‘sale_price’] consist prices values in product variations
$variation_data[‘stock_qty’] consist stock values in product variations
$variation->save() saves attribute values of WooCommerce product variations