【问题标题】:Create a product with custom fields via Woocommerce Rest Api通过 Woocommerce Rest Api 创建具有自定义字段的产品
【发布时间】:2017-12-24 17:56:28
【问题描述】:

我正在通过 Woocommerce Rest Api v2 添加产品。但我无法通过 Api 添加自定义字段。

请给我建议最好的方法。

json 响应:

$data = array(
   // 'product' => array(
        'id' => 8555,
        'title' => 'Notebook',
        'type' => 'simple',
        'product_id' => 8555,
        'regular_price' => '21.99',
        '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.',
        'categories' => array("General"),
        'product_meta' => array(
            'custom_products_key' => 'custom_product_value',

            )
);

product_meta 不工作...

【问题讨论】:

    标签: php wordpress woocommerce custom-fields woocommerce-rest-api


    【解决方案1】:

    您通常应该将'product_meta' 替换为'meta_data' 并将键与值分开。

    例如,这将为您的产品添加 2 个自定义字段:

        'meta_data' => [
            [
                'key' => 'custom_key'
                'value' => 'custom_value'
            ]
            [
                'key' => 'custom_key2'
                'value' => 'custom_value2'
            ]
        ]
    

    要使用最新的 Woocommerce Rest API v2(使用 PHP)创建产品,请在下面设置好的类别 ID:

    $data = [
        'name' => 'Notebook',
        'type' => 'simple',
        'regular_price' => '21.99',
        '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.',
        'categories' => [
            [
                'id' => 9
            ]
        ],
        'meta_data' => [
            [
                'key' => 'custom_key'
                'value' => 'custom_value'
            ]
            [
                'key' => 'custom_key2'
                'value' => 'custom_value2'
            ]
        ]
    ];
    
    print_r($woocommerce->post('products', $data));
    

    这应该可行。

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 2022-01-19
      • 2016-07-22
      • 2018-02-05
      相关资源
      最近更新 更多