【问题标题】:woocommerce api create new product with variationswoocommerce api 创建具有变化的新产品
【发布时间】:2023-03-12 02:20:01
【问题描述】:

我想用 woocommerce api 创建一个新产品。我有一个颜色属性,我想从 sku 中识别产品,例如 codeblack。

我的问题是如何做到这一点。

在创建带有倾斜、描述、sku 等的产品时,没关系。为了创建具有属性的产品,我添加了'type' => 'variable'

现在我有了属性,我启用了变体,但我无法为我的变体赋予价值。

这是我的代码

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

    ) 

);

所以我有我的属性

但我无法将它们与变体匹配

你能帮帮我吗?

【问题讨论】:

    标签: api attributes woocommerce variations


    【解决方案1】:

    即使我通过 NodeJS 使用 API,我也遇到了完全相同的问题。 我发现了在来自 API 的调用中处理 positionis_taxonomy 的方式存在问题,这会阻止变量正确链接到属性: https://github.com/woothemes/woocommerce/issues/11200

    【讨论】:

    • 将 id 放入变量的属性中。这对我有用
    【解决方案2】:

    我找到了解决办法

    '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'
                            )
                        ) 
                    )
                )
    

    如果存在颜色属性和红色、黑色值,则适用于我。

    你必须使用id

    【讨论】:

      【解决方案3】:

      您可以使用变体/批次并使用您现有的产品属性,它对我有用...

      【讨论】:

        猜你喜欢
        • 2020-08-08
        • 2019-05-24
        • 1970-01-01
        • 2021-11-25
        • 2018-05-11
        • 1970-01-01
        • 2021-06-19
        • 2015-11-02
        • 1970-01-01
        相关资源
        最近更新 更多