Creating a WooCommerce product variation with the REST API

Like adding a product via the REST API is creating a product variation done relatively quickly – I use the PHP library for that, like in my last week’s post.

Requirements for adding a variation

To make adding a variation work, we need to add the wanted attributes to the product first. This can look something like that while adding a product (my post from last week was about that):

$prod_data = [ 'name' => 'A great product', 'type' => 'variable', 'description' => 'A very meaningful product description', 'images' => [ [ 'src' => 'https://shop.local/path/to/image.jpg', 'position' => 0, ], ], 'categories' => [ [ 'id' => 1, ], ], 'attributes' => [ [ 'id' => 5, 'variation' => true, 'visible' => true, 'options' => [ 'S', 'M', 'L' ], ], ], ]; $product = $this->woocommerce->post( 'products', $prod_data );

We set an array of attributes for the attributes key (just the size in the example) and can specify if this attributes should be available for variations via variation. With options, we define the available options for the variations – those attribute terms must exist before adding the product, like the attribute.

Besides that, we set the type of the product to variable.

Adding a product variation

After we meet the requirements, we can create a variation with the following code:

$variation_data = [ 'regular_price' => '15.00', 'image' => [ 'src' => 'https://shop.local/path/to/image_size_l.jpg', ], 'attributes' => [ [ 'id' => 5, 'option' => 'L', ], ], ]; $this->woocommerce->post( "products/$product->id/variations", $variation_data );

We define the price, set an image and the size. For specifying the size, we pass the id of the attribute and the label of the option as an array inside the array of attributes. We use the REST API endpoint products/$product->id/variations – we can get the product’s ID via $product->id here, because we saved the call of the products endpoint for creating the product in the $product variable.

And that is it – we should have created a variable product with a variation.

Related posts

7 comments on »Creating a WooCommerce product variation with the REST API«

  1. Edu

    WOnderful!!
    It was super useful for me!! It is not so easy to find how to change the attributes and variations by API REST

    Reply
  2. JAMES

    Thank you for this in-depth tutorial. I am trying to work on product variations but not getting the actual results as I have already gone through several resources and following this resource https://wpitech.com/woocommerce-product-variation/ to implement the product variation process. Can you please describe me further how to do this? This is the code that I think is somehow wrong. Is there any other way to set product variations?

    $variation_id = woo_insert_post( $variation_post );
    $variation = new WooCommerce_Product_Variation( $variation_id );
    foreach ($variation_data['attributes'] as $attribute => $term_name )

    Reply
    1. Florian Brinkmann

      Hi,

      I guess it would be better to ask that question under the article where the code is from – I cannot help you with that, because I do not know the code (and do not have the time to dive into it).

      Best,
      Florian

      Reply
  3. NorthW

    Hey Florian,

    Thanks for the article! I think this is closest I got trying to achieve the following..

    I've already loaded all products to WooCommmerce, and all attributes (size, colour etc.) are entered. Further, checkbox for the "used for variations" is selected for the relevant ones (there are a few that are not used for variations, they are there just for info).

    So, can I use above code to generate all variations for all products? There are 1000+ products, and some variations would exceed 50, so doing this manually is a massive job. I'm trying to find a way to automate that part, and above article sounds like it does exactly that, although I'm not quite sure how to implement it.

    I would appreciate your comment and guidance on how this code could be applied.

    Many thanks in advance!

    Reply
  4. Matias Castro

    Hello,
    Which language is this? PHP?
    I'am looking for this example but in JSON (I'm newbie).

    Thanks

    Reply

Leave a Reply to Matias Castro Cancel reply

Your email address will not be published. Required fields are marked *