【问题标题】:WooCommerce REST API - Retrieve Order Attributes (Size / Color)WooCommerce REST API - 检索订单属性(尺寸/颜色)
【发布时间】:2014-11-28 04:33:01
【问题描述】:

使用WooCommerce REST Client Library,我可以轻松拉取正在处理的订单,如下所示:

$response = $wc_api->get_orders( array( 'status' => 'processing' ) );

但结果不包括属性(颜色、尺寸等),即使购买的产品设置了与产品变体相关的尺寸/颜色属性。那部分一切正常。客户可以选择产品的尺寸和颜色,但 get_orders 查询不会显示该信息。

以下是显示的内容:

<line_items>
     <XML_Serializer_Tag>
         <id>18</id>
         <subtotal>19.99</subtotal>
         <total>19.99</total>
         <total_tax>0.00</total_tax>
         <price>19.99</price>
         <quantity>1</quantity>
         <tax_class />
         <name>Cool T-Shirt You Just Bought!</name>
         <product_id>351</product_id>
         <sku>194953</sku>
     </XML_Serializer_Tag>
 </line_items>

如您所见,即使客户为变体选择了“大号/黑色”,它也不会显示在 get_orders 数据中。

我可以使用相同的库为 product 提取可用属性,但我需要为 order 提取客户选择的属性。

【问题讨论】:

  • 我说的是 Magento 经验,同样可能适用于 woocommerce:您需要获取订单的产品,并从该集合/数组中获取产品的详细信息,例如 weightsize
  • 可以从 REST API 访问产品详细信息;例如,我可以让它为我提供特定产品的可用color 选项。但这并不能告诉我color 客户在下订单时选择了什么。 REST API 似乎没有提供该数据,也没有提供选定的 size 或任何其他“变量”数据字段。

标签: php xml wordpress api woocommerce


【解决方案1】:

我讨厌回答自己的问题,但事实证明答案很简单:

WooCommerce REST Client Library 尚未针对 API 的 V2 进行更新,尽管 WooCommerce 将其链接为 V2 资源。解决方法非常简单:

导航到 ~/class-wc-api-client.php 并将第 17 行更改为:

const API_ENDPOINT = 'wc-api/v2/';

当我执行 get_orders() 查询时,API 立即返回了正确的数据。

【讨论】:

    【解决方案2】:

    我又是在 magento 的背景下说话 who would like to get that bounty :P 我认为您必须从上面生成的订单列表中分别处理每个订单

    引用此link

    您可以通过以下方式获取订单的订单商品

    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    

    那么如果你遍历它们,你可以得到所有相关的数据:

    foreach ( $items as $item ) {
        $product_name = $item['name'];
        $product_id = $item['product_id'];
        //// you had the product id now you can load the product and get all information  you might need 
    }
    

    【讨论】:

    • 是的,如果您是在 Wordpress / WooCommerce 框架内执行此操作,这将是执行此操作的方法——也许作为一个插件。但是 REST API 不提供对 WC_Order 的直接访问,而我正在尝试使用它。
    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 2021-07-31
    • 2017-04-09
    • 2015-12-20
    • 2017-09-22
    • 2018-05-12
    • 1970-01-01
    • 2020-03-22
    相关资源
    最近更新 更多