在wocommerce REST API中更新产品meta_data

时间:2017-07-25 14:31:32

标签: wordpress rest api woocommerce

我使用一个小插件脚本添加了一个新字段...它在woocommerce界面中工作正常,并且还显示使用REST API来获取产品

这里使用$ woocommerce-> get()

的结果摘录
'meta_data' => 
    array (size=1)
      0 => 
        array (size=3)
          'id' => int 3293
          'key' => string 'kambusa' (length=7)
          'value' => string '123' (length=3)

使用:

$data = [
    'regular_price' => '21.00',
    'meta_data' => [
        ['kambusa' => '456']
    ]
];

print_r($woocommerce->put('products/530', $data));

仅更新价格,但忽略(没有任何错误)meta_data

我整个上午搜索了网络,但没有找到任何明确的解决方案 (有人建议我必须注册_meta(),做了一些测试,但没有任何改变(元数据也在注册前显示))

按照用于在我的插件“admin”部分创建字段的代码

$args = array(
    'id'            => $this->textfield_id,
    'label'         => sanitize_text_field( 'Kambusa ID' ),
    'placeholder'   => $placeholder,
    'desc_tip'      => true,
   'description'    => $description,
);
woocommerce_wp_text_input( $args );

任何线索?

3 个答案:

答案 0 :(得分:0)

你试过这个吗?

$data = [
    'regular_price' => '21.00',
    'meta_data' => [
        ['key' => 'kambusa'],
        ['value' => '456']
    ]
];

答案 1 :(得分:0)

{
  "name": "Test Product",
  "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
    },
    {
      "id": 14
    }
  ],
  "images": [
    {
      "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
    },
    {
      "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
    }
  ],
  "meta_data": [
    {
        "key": "test_meta1", 
        "value": "test_meta_val1"
    }
  ]
}

这是我如何在POST wp-json / wc / v3 / products(创建产品)上传递meta_data

答案 2 :(得分:0)

试试这个代码:

$product_id = 123;

update_products_meta($product_id);    

function update_products_meta($product_id)
{

$connection = woo_connection();

$data = [
    'meta_data' => [
        [
            'key' => 'KNK',
            'value' => 'KIDZ N KIDZ'
        ],
    ],
];

$connection = woo_connection();

print_r($connection->put('products/' . $product_id, $data));
}