通过Woocommerce API V2更新库存 [英] Updating stock via Woocommerce API V2

查看:77
本文介绍了通过Woocommerce API V2更新库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过API接口更新woocommerce商店的库存水平.我正在使用 kloon/WooCommerce-REST-API-Client-Library 但不幸的是,它似乎只能读取产品信息,而不允许我输入库存信息.

I'm trying to update my woocommerce shop's stock levels via the API interface. I'm using the kloon/WooCommerce-REST-API-Client-Library but unfortunately it only seems to read product information and doesn't allow me to PUT stock info.

阅读API文档,我可以使用以下内容更新产品:

Reading the API docs I can see I can update a product using the following:

curl -X PUT https://example.com/wc-api/v2/products/546 \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "product": {
    "regular_price": "24.54"
  }
}'

但API客户端库中的所有功能都无法访问产品的PUT功能,我发现了针对V2 api的修改后的客户端库

But non of the function in the API client library access the PUT features of product, I have found a modified client library for the V2 api rodolfojnn/WooCommerce-REST-API-Client-Library which has updated PUT functions:

/**
* Update a product by id
* @param int $product_id
* @param array $data
* @param string $method
* @return mixed|json string
*/
    public function update_product($product_id, $data, $method = "PUT") {

        return $this->_make_api_call('products/' . $product_id, ['product' => $data], $method);
}

但是现在这会引发一个解析错误:语法错误,由于 ['product'=> $ data] 位,出现了意外的'['错误-我尝试了转换它到一个数组,但也会出错,任何人都知道为什么(我在codeigniter btw中运行它)

But this now throws an Parse error: syntax error, unexpected '[' error because of the ['product' => $data] bit - I tried converting it to an array but that also errors, anyone any idea why (I'm running this in codeigniter btw)

推荐答案

尝试修改

public function update_product($product_id, $data, $method = "PUT") {

    return $this->_make_api_call('products/' . $product_id, $data, $method);
}

仅将$ data作为第二个参数传递给update_product

Pass only $data as 2nd parameter to update_product

$data=  json_encode(

    array( 'product' =>

       array( 
          'regular_price'        => "10.26",
          'managing_stock'   => true,
          'in_stock'         => true,
          'stock_quantity'   => 45
       )
    )
);

这篇关于通过Woocommerce API V2更新库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆