如何在WooCommerce中使用自定义订单状态更改订单状态

时间:2019-04-05 08:14:48

标签: php wordpress methods woocommerce orders

您好,我有这段代码,它在订单状态中添加了称为退款的自定义状态

    function register_awaiting_shipment_order_status() {
    register_post_status( 'wc-awaiting-shipment', array(
        'label'                     => 'Return',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Return <span class="count">(%s)</span>', 'Return <span class="count">(%s)</span>' )

        ) );
        register_post_status( 'wc-refund-issued', array(
            'label'                     => 'Refund Issued',
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'Refund Issued<span class="count">(%s)</span>', 'Refund Issued <span class="count">(%s)</span>' )

            ) );

}
add_action( 'init', 'register_awaiting_shipment_order_status' );
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
    $new_order_statuses = array();
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-awaiting-shipment'] = 'Return';
            $new_order_statuses['wc-refund-issued'] = 'Refund Issued';
        }
    }
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );

我希望能够通过以下方式更改自定义订单状态

$order->status_update('refund issued');

但是它不起作用,仅当我运行代码时,它才适用于woocommerce已有的状态,例如处理和待处理付款,它将状态更改为待处理而不是退款,这是我做错了吗?最后,我想做的是在查看订单区域中创建一个退款按钮,以便用户可以向当前订单发出退款请求

1 个答案:

答案 0 :(得分:0)

问题解决了,我称它应该是错误的

$order->status_update('wc-refund-issued');  

代替

$order->status_update('refund issued');

非常感谢您