Website Building » WooCommerce » How Do I Create a Variable Product in WooCommerce Programmatically?

How Do I Create a Variable Product in WooCommerce Programmatically?

Last updated on October 1, 2022 @ 2:34 pm

When it comes to eCommerce, product variation is key. Customers want to be able to choose from a range of options when they purchase an item, and this is where variable products come in.

If you’re running a WooCommerce store, you may be wondering how you can create variable products programmatically. In this article, we’ll show you how to do just that.

A variable product in WooCommerce is a product with multiple variations, each of which has its own SKU, price, and stock options. For example, a t-shirt might come in multiple sizes and colors, each of which would be considered a variation.

PRO TIP: If you are not a developer or comfortable with code, we recommend you hire a developer to help you create a variable product in WooCommerce.

In order to create a variable product programmatically, you’ll need to use the WC_Product_Variable class. This class contains methods for managing variable products, including creating new variations and managing stock levels.

Here’s an example of how you might use the WC_Product_Variable class to create a new variable product:

set_name( ‘T-Shirt’ );
$product->set_slug( ‘tshirt’ );
$product->set_price( 19.99 );
$product->set_stock_status( ‘instock’ );
$product->set_manage_stock( true );
$product->set_stock_quantity( 10 );
$product->set_weight( ” ); // empty weight so product can have variations with weights

// Get the attributes for the product – size and color for our example t-shirt product
$attribute1 = wc_get_attribute( ‘pa_size’ ); // size attribute slug is ‘pa_size’ – set in product attributes area of admin panel
$attribute2 = wc_get_attribute( ‘pa_color’ ); // color attribute slug is ‘pa_color’ – set in product attributes area of admin panel

// Create the first variation – T-Shirt Size S – Red (Red has a value or “slug” of ‘red’) $variation1 = new WC_Product(); $variation1->set_name( ‘T-Shirt Size S – Red’ ); $variation1->set_slug( ‘tshirt-size-s-red’ ); $variation1->set_price( 19.99 ); $variation1->set__stock( 10 ); // set stock status and level – this variation has 10 in stock (you can use 0 or false for out of stock) $variation1->set__weight(); // empty weight so product can have variations with weights $variation1->setparentid($product); // Set the parent id of the product as the original variable product we created $variation1->save(); // Save the data – Important! update post meta data for size and color add size pa value wpupdatepostmeta($variation1id,’attribute__color’,array(‘color’)); add color pa value wpupdatepostmeta($variation1id,’attribute__size’,array(‘S’)); Update price meta data (this will update the price display on front end) update post meta data for regular price wpupdatepostmeta($variation1id,’price’,19.99); Update sale price meta data if needed (this will update the sale price display on front end) update post meta data for sale price wpuupdatepostmetadate($variation11d,’saleprice’,15); Create second variation – T-Shirt Size M – Blue $variaiton2 = new WC Product(); $variaiton2->setname(‘T Shirt Size M Blue’); $variaiton2->setslug(‘tshirt size m blue’); $variaiton2->setprice(19.99); $variaiton2->set__stock(10); // set stock status and level – this variation has 10 in stock (you can use 0 or fasle for out of stock) $variaiton2->save(); // Save the data – Important!

Update post meta data for size and color add size pa value wpuupdatepostmeta($variaiton2id,’attributesize’,array(‘M’)); add color pa value wpuupdatepostmeta($variaton2id,’attributecolor’,array(‘blue’)); Update price meta date (this will update the price display on front end) update post meta date for regular price wpuupdatepostmedate($variaton21d,’price’,19.99); Update sale price meta date if needed (this will update th esale prcie display on front end) update post meta date for sale prcie wpuupdatepostmedate($variaton21d,’saleprice’,15); Create third variation T Shirt Size L Green $variaiton3 = new WC Product(); $variaiton3 -> setname(‘T Shirt Size L Green’); $variaiton3 -> setslug(‘tshirt size l green’); $variaiton3 -> setprice(19.99); variaton3 -> set__stock(10); // set stock status and level – this variation has 10 in stock (you can use 0 or false for out of stok) variaton3 -> save(); // Save th e data – Important! Upate post meta data for size and color add size pa value wpuupdatepostmeta($variaiton31d,’attributesize’,array(‘L’)); add color pa value wpuupdatemetapostdate($variaton31d,’attributecolor’,array(‘green’)); Updat epric emeta date (this will update teh price display on front end) updat epos tme ta date fo rregular pr ice wpuupdatemetapostdate ($variaton31d ,’price ‘,19 .99 ) ; Updatesalepricemeta date if needed (this wil lupdaet th esalepricedisplay on front end ) updat epostmetadataforsaleprice wpuupdatemetapostdate ($variaton31d ,’saleprice ‘,15 ) ;

Dale Leydon

Dale Leydon

Sysadmin turned Javascript developer. Owner of 20+ apps graveyard, and a couple of successful ones.