首页 > 解决方案 > 如何在 WooCommerce 中使用自定义订单状态更改订单状态

问题描述

你好,我有这个代码,它添加了一个名为退款的自定义状态到订单状态

    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 已经附带的状态,例如处理和待付款,当我运行代码时它会将状态更改为待付款而不是退款我做错了什么?最后,我试图完成的是在查看订单区域中制作退款按钮,以便用户可以向当前订单发出退款请求

标签: phpwordpressmethodswoocommerceorders

解决方案


问题解决了我说错了它应该是

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

代替

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

非常感谢


推荐阅读