添加自定义订单状态“已发货"在Woocommerce中 [英] Add a custom order status "Shipped" in Woocommerce

查看:634
本文介绍了添加自定义订单状态“已发货"在Woocommerce中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce开发一个电子商务站点,我希望将自定义订单状态设置为已发货".

I am developing an e-commerce site using Woocommerce and I would like to a custom order status "Shipped".

流程如下:客户下订单时会收到一封电子邮件,指出订单已确认,当前状态为处理中,管理员从其快递员那里获取跟踪ID并将其粘贴到一个新的部分,并将订单状态更改为已发货.这会向客户发送另一封带有跟踪ID的电子邮件.

So this is how the flow would go: Customer places an order gets an email saying order has been confirmed, current status is processing, admin gets the tracking ID from its courier and can paste it in a new section and change the order status to shipped. That would fire another email with tracking id to the customer.

是否有任何自定义代码可以帮助我实现此功能?

Is there any custom code which will help me achieve this functionality?

任何帮助将不胜感激.

推荐答案

以下代码将添加一个新的自定义已发货"商品, Woocommerce订单的订单状态:

The following code will add a new custom "shipped" order status to Woocommerce orders:

// Register a custom order status
add_action('init', 'register_custom_order_statuses');
function register_custom_order_statuses() {
    register_post_status('wc-shipped ', array(
        'label' => __( 'Shipped', 'woocommerce' ),
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')
    ));
}


// Add a custom order status to list of WC Order statuses
add_filter('wc_order_statuses', 'add_custom_order_statuses');
function add_custom_order_statuses($order_statuses) {
    $new_order_statuses = array();

    // add new order status before processing
    foreach ($order_statuses as $key => $status) {
        $new_order_statuses[$key] = $status;
        if ('wc-processing' === $key) {
            $new_order_statuses['wc-shipped'] = __('Shipped', 'woocommerce' );
        }
    }
    return $new_order_statuses;
}


// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 50, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $new_actions = array();

    // add new order status before processing
    foreach ($actions as $key => $action) {
        if ('mark_processing' === $key)
            $new_actions['mark_shipped'] = __( 'Change status to shipped', 'woocommerce' );

        $new_actions[$key] = $action;
    }
    return $new_actions;
}

// Add a custom order status action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
    // Display the button for all orders that have a 'processing', 'pending' or 'on-hold' status
    if ( $order->has_status( array( 'on-hold', 'processing', 'pending' ) ) ) {

        // The key slug defined for your action button
        $action_slug = 'shipped';

        // Set the action button
        $actions[$action_slug] = array(
            'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status='.$action_slug.'&order_id='.$order->get_id() ), 'woocommerce-mark-order-status' ),
            'name'      => __( 'Shipped', 'woocommerce' ),
            'action'    => $action_slug,
        );
    }
    return $actions;
}

// Set styling for custom order status action button icon and List icon
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
    $action_slug = "shipped"; // The key slug defined for your action button
    ?>
    <style>
        .wc-action-button-<?php echo $action_slug; ?>::after {
            font-family: woocommerce !important; content: "\e029" !important;
        }
    </style>
    <?php
}

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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

您将获得以下内容:

在管理员订单列表中:

在管理员订单编辑页面中:

In admin order edit pages:

例如,我们要删除保留" 状态更改:

For example we want to remove "On hold" status change:

add_filter( 'bulk_actions-edit-shop_order', 'remove_a_bulk_order_action', 20, 1 );
function remove_a_bulk_order_action( $actions ) {
    unset($actions['mark_on-hold']);

    return $actions;
}

所有状态更改键均以mark_ +状态标记(无wc-)开头.

All statuses change keys start with mark_ + the status slug (without wc-).

这篇关于添加自定义订单状态“已发货"在Woocommerce中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆