在Woocommerce中添加多个自定义订单状态

时间:2018-11-09 14:16:18

标签: php wordpress woocommerce hook-woocommerce orders

我尝试了下面编写的这段代码,并且有效。唯一的问题是我需要再添加一个名为“ Verification”的状态。如果这是你们可以做的,请帮助我。

function register_awaiting_shipment_order_status() {
    register_post_status( 'wc-awaiting-shipment', array(
        'label'                     => 'Awaiting shipment',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <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'] = 'Awaiting shipment';
        }
    }
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );

1 个答案:

答案 0 :(得分:0)

以下代码将添加“等待发货” “验证” 自定义订单状态,并将替换您问题中的代码:

add_action( 'init', 'register_custom_statuses_as_order_status' );
function register_custom_statuses_as_order_status() {

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

    register_post_status( 'wc-verification', array(
        'label'                     => __('Verification'),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Verification <span class="count">(%s)</span>', 'Verification <span class="count">(%s)</span>' )
    ) );
}

// Add to list of WC Order statuses
add_filter( 'wc_order_statuses', 'add_additional_custom_statuses_to_order_statuses' );
function add_additional_custom_statuses_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'] = __('Awaiting shipment');
            $new_order_statuses['wc-verification'] = __('Verification');
        }
    }
    return $new_order_statuses;
}

代码进入您的活动子主题(活动主题)的function.php文件中。经过测试,可以正常工作。