【问题标题】:Woocommerce REST API Update Product CategoriesWoocommerce REST API 更新产品类别
【发布时间】:2018-10-22 07:26:09
【问题描述】:

我正在使用 Wordpress 4.9.5 和 Woocommerce 3.3.5。当网站上的产品在单独的产品管理系统中发生更改时,我使用WooCommerce REST API PHP Client Library 来更新它们。该库使用的是 REST API v2。

使用以下代码,我成功更新了基本产品数据(标题、描述、sku、价格等),但我无法从未分类中获取要更新的类别。如果网站上尚不存在产品,则在使用类似代码创建产品时也不会设置类别。

$client = new WC_API_Client( $domain, $consumerKey, $consumerSecret, $options );
$client->products->update( $id, array( 
'sku' => $product->sku,
'title' => $product->title, 
'type' => $product->type, 
'status' => $product->status,
'regular_price' => $product->regular_price, 
'description' => $product->description,
'categories' => array(
    array( 
        'id' => 343
    ),
    array(
        'id' => 347
    )
)
));

正如我所说,其他字段按预期更新。我已经确认 ID 为 343 和 347 的类别肯定存在,所以我认为我的语法一定有问题。随着其他字段的更新,身份验证肯定是有效的。

我已阅读官方 Woocommerce API 文档并基于 this 教程编写代码。基于这两个,我不确定我做错了什么。

感谢您的任何帮助或建议。

【问题讨论】:

    标签: wordpress woocommerce woocommerce-rest-api


    【解决方案1】:

    我最终解决了这个问题。这是我的疏忽。

    我使用的客户端库连接到 Woocommerce 文档中称为“Legacy v2”版本而不是“v2”版本的 API。旧版本不支持类别、图像 alt 标签、元数据等。

    我从 using 库切换到使用 https://sitename/wp-json/wc/v2/endpoint 直接连接到“v2”版本,现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2014-11-06
      • 2021-08-08
      • 2017-12-31
      • 2020-08-08
      • 1970-01-01
      • 2016-11-19
      相关资源
      最近更新 更多