使用WooCommerce REST API创建带有变体的产品

时间:2017-11-23 作者:kabuye dennis

我创建了一个带有变体的产品,但它们没有链接到后端,除非手动设置,否则不会显示在产品页面上enter 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);
非常感谢您的帮助

2 个回复
SO网友:Ramuchit Kumar

您必须创建另一个变更请求。

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

SO网友:Maulik Makwana

您可以使用变体/批次并使用现有的产品属性/wp-json/wc/v3/products//variances/batchenter image description here

结束

相关推荐