3
votes

I want to create a new product with woocommerce api. I have a color attribute and i'd like to recognize product from sku for example codeblack.

My problem is how to do this.

While creating a product with tilte,description,sku etc it's ok. To create a product with attributes, i added 'type' => 'variable'.

Now i have attributes, i enabled variations but i cannot give value to my variation.

Here's my code

    $client->products->create( 
    array( 
        'title' => 'Premium Quality', 
        'type' => 'variable', 
        'sku' => 'code',
        'regular_price' => '29.98',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
        'images' => array(
                array(
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',
                    'position' => 0

                ),
                array(
                    'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
                    'position' => 1
                )
         ),
         'attributes' => array( 
            array( 
                'name'=>'color',
                'slug'=>'color',
                'position'=>'0',
                'visible'=>'true',
                'variation'=>'true',
                'options'=>array('red','black')
            ) 
         ),
         'variations' => array(
            array(
                'sku' => 'codered',
                'regular_price' => '29.98',
                'attributes' => array( 
                    array( 
                        'name'=>'color',
                        'options'=>'red'
                    )

                ) 
            ),
            array(
                'sku' => 'codeblack',
                'regular_price' => '29.98',
                'attributes' => array( 
                    array( 
                        'name'=>'color',
                        'options'=>'black'
                    )
                ) 
            )
        )

    ) 

);

So i have my attributes enter image description here

but i can't to matching them with variationsenter image description here

Could you please help me?

3

3 Answers

0
votes

I have the exact same issue even though I use the API via NodeJS. I have identified an issue in the way position & is_taxonomy are handled in calls from the API which prevents variations to be properly linked to the attribute: https://github.com/woothemes/woocommerce/issues/11200

0
votes

I found a solution

'variations' => array(
                array(
                    'sku' => date('His').'_1',
                    'regular_price' => '29.98',
                    'attributes' => array( 
                        array( 
                            'id' => date('YmdHis', strtotime('+2 seconds')),
                            'slug'=>'color',
                            'name'=>'color',
                            'option'=>'red' 
                        )
                    ) 
                ),
                array(
                    'sku' => date('His').'_2',
                    'regular_price' => '29.98',
                    'attributes' => array( 
                        array( 
                            'id' => date('YmdHis'),
                            'slug'=>'color',
                            'name'=>'color',
                            'option'=>'black'
                        )
                    ) 
                )
            )

works for me if color attribute and red,black values exist.

You have to use id

0
votes

You can use variations/batch and used your existing product attributes , its works for me... enter image description here