2

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

7
  • Inside variations => attributes it should be option instead of options Commented Nov 24, 2017 at 7:11
  • Are you able to set categories by this code? I have figured out most of it but still stuck on categories Commented Nov 24, 2017 at 7:15
  • @PiyushRawat no not yet still working on setting categories too but for the variations, i've done some research and i think there is no way of doing it in the same request because variations have a different dedicated end point as according to the API docs. but i'll keep working if i get and answer i'll update you Commented Nov 25, 2017 at 12:42
  • I got it fixed bro...look here wordpress.stackexchange.com/questions/286732/… Commented Nov 25, 2017 at 12:49
  • yea this is great, i've also tried adding product with php and its possible to do in one request but the categories won't work i don't know why Commented Nov 25, 2017 at 15:12

2 Answers 2

1

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

You can use variations/batch and used your existing product attributes /wp-json/wc/v3/products//variations/batch enter image description here

2
  • 4
    Please don't use images to show your code examples. Commented Feb 4, 2020 at 14:36
  • in rest api json/wc/v3/products//variations/batch check here example use this you its same as added image example Commented Feb 4, 2020 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.