首页 > 解决方案 > woocommerce 自定义订单状态和 REST API

问题描述

我在 function.php 中使用 php 向 woocommerce 添加了自定义订单状态。

我可以在管理界面上查看/设置此状态,但我想使用 REST API(v2 或 v3)并将订单更新为设置此新状态。

API 返回此错误:错误:在:状态 [rest_invalid_param]

我可以为订单设置预建状态,但不能为新订单设置。我怎样才能做到这一点?

WordPress 5.2.2,WooCommerce 3.6.4

function wpblog_wc_register_post_statuses() {
register_post_status( 'wc-invoicing', array(
    'label'                     => 'XXXX',
    'public'                    => true,
    'exclude_from_search'       => false,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop( 'XXXX <span class="count"> 
    (%s)</span>', 'XXXX alatt <span class="count">(%s)</span>' )
));
}
add_filter( 'init', 'wpblog_wc_register_post_statuses' );

function wpblog_wc_add_order_statuses( $order_statuses ) {
   $order_statuses['wc-invoicing'] = 
   _x( 'XXXX alatt', 'Order Status', '' );
    return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wpblog_wc_add_order_statuses', 10, 1 );

标签: phpwordpresswoocommercewoocommerce-rest-api

解决方案


不要在自定义订单状态前使用“wc-”。

即在 WooCommerce 中,我创建了一个名为“wc-new-quote”的订单状态。现在,当我尝试发布时,REST API 给了我一个错误:

$data['status'] = 'wc-new-quote';

正确的方法是:

$data['status'] = 'new-quote';

推荐阅读