最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

creating product with variations using woocommerce REST API

programmeradmin1浏览0评论

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 them

$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' => '.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

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 them

$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/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

Share Improve this question edited Nov 24, 2017 at 6:26 rudtek 6,3535 gold badges30 silver badges52 bronze badges asked Nov 23, 2017 at 21:37 kabuye denniskabuye dennis 211 silver badge3 bronze badges 7
  • Inside variations => attributes it should be option instead of options – Piyush Rawat 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 – Piyush Rawat 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 – kabuye dennis Commented Nov 25, 2017 at 12:42
  • I got it fixed bro...look here wordpress.stackexchange/questions/286732/… – Piyush Rawat 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 – kabuye dennis Commented Nov 25, 2017 at 15:12
 |  Show 2 more comments

2 Answers 2

Reset to default 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 );

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

发布评论

评论列表(0)

  1. 暂无评论