creating product with variations using woocommerce REST API

I’ve created a product with variations but they are not linked at the back end and won’t show up on the product page unless i manually set thementer image description here

$data = [
'title'=> 'ship your idea5',

'type' => 'variable',
'description' => 'Trying it out for real',
'short_description' => 'Pellentesque habitant.',
'categories' => [
    [
        'name' => 'Movies',
        'slug' => 'movies'
    ],
    [
        'name' => 'Romance',
        'slug' => 'romance'
    ]
],
'images' => [
    [
        'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg',
        'position' => 0
    ]

],
'attributes' => [
    [
        'name' => 'Color',
        'position' => 0,
        'visible' => true,
        'variation' => true,
        'options' => [
            'Black',
            'Green'
        ]
    ],
    [
        'name' => 'Size',
        'position' => 0,
        'visible' => true,
        'variation' => true,
        'options' => [
            'S',
            'M'
        ]
    ]
],
'default_attributes' => [
    [
        'name' => 'Color',
        'option' => 'Black'
    ],
    [
        'name' => 'Size',
        'option' => 'S'
    ]
    ],
    'variations' => array( 
        array(  'regular_price' => '29.98', 
        'attributes' => array( 
            array( 'name'=>'Color', 'options'=>'Black' )
             ) 
            ),
             array(  'regular_price' => '29.98',
              'attributes' => array( 
                  array( 'name'=>'color', 'options'=>'Green' ) 
                  ) 
                  ) 
                  )


];


$client->products->create($data);

any help is highly appreciated

2 Answers
2

you have to create another request for variation.

$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 );

Leave a Comment