WooCommerce 管理订单列表中自定义订单状态的操作按钮问题

时间:2021-06-16 15:27:13

标签: php wordpress woocommerce backend orders

我还创建了自定义订单状态“准备发货”和自定义操作按钮。

问题和疑问是:当我点击自定义操作按钮时,订单状态发生了变化,但默认操作按钮“完成”消失了。

如何在单击自定义操作按钮后保留“完成”操作按钮?

任何建议将不胜感激

我当前的代码:

// Register new status
function register_awaiting_shipment_order_status() {
    register_post_status( 'wc-ready-to-dispatch', array(
        'label'                     => 'Ready to dispatch',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Ready to dispatch (%s)', 'Ready to dispatch (%s)' )
    ) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );

// Add your 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' status
    if ( $order->has_status( array( 'processing' ) ) ) {

        $action_slug = 'ready-to-dispatch';

        // Get Order ID (compatibility all WC versions)
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
        // Set the action button
        $actions['ready-to-dispatch'] = array(
            'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=ready-to-dispatch&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
            'name'      => __( 'Ready to dispatch', 'woocommerce' ),
            'action'    => 'ready-to-dispatch', // keep "view" class for a clean button CSS
        );
    }
    return $actions;
}

// Set Here the WooCommerce icon for your action button
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
    $action_slug = 'ready-to-dispatch';
    echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\f344" !important; }</style>';
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_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-ready-to-dispatch'] = 'Ready to dispatch';
       }
   }

   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', 1, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_ready-to-dispatch'] = __( 'Ready to dispatch', 'woocommerce' );
    return $actions;
}

1 个答案:

答案 0 :(得分:0)

这是因为在 woocommerce_admin_order_actions 钩子中缺少 if 条件,以便在订单包含状态 ready-to-dispatch 时显示按钮。

我已经用这个更新后的代码重写了你的代码。例如,init 挂钩已替换为 woocommerce_register_shop_order_post_statuses 以注册自定义订单状态。

所以你得到:

/**
 * Register order status
 */
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
    // Status must start with "wc-"
    $order_statuses['wc-ready-to-dispatch'] = array(
        'label'                     => _x( 'Ready to dispatch', 'Order status', 'woocommerce' ),
        'public'                    => false,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        /* translators: %s: number of orders */
        'label_count'               => _n_noop( 'Ready to dispatch <span class="count">(%s)</span>', 'Abonnement <span class="count">(%s)</span>', 'woocommerce' ),       
    );
    
    return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );

/**
 * Show order status in the dropdown @ single order
 */
function filter_wc_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 ) {
            // Status must start with "wc-"
            $new_order_statuses['wc-ready-to-dispatch'] = _x( 'Ready to dispatch', 'Order status', 'woocommerce' );
        }
    }

    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );

/**
 * Show order status in the dropdown @ bulk actions
 */
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
    // Note: "mark_" must be there instead of "wc"
    $bulk_actions['mark_ready-to-dispatch'] = __( 'Ready to dispatch', 'woocommerce' );
    return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );

/**
 * Add quick action button @ admin orders list
 */
function filter_order_actions( $actions, $order ) {
    // Get Order ID (compatibility all WC versions)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    
    // Display the button for all orders that have a 'processing' status
    if ( $order->has_status( array( 'processing' ) ) ) {

        $action_slug = 'ready-to-dispatch';

        // Set the action button
        $actions['ready-to-dispatch'] = array(
            'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=ready-to-dispatch&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
            'name'      => __( 'Ready to dispatch', 'woocommerce' ),
            'action'    => $action_slug, // keep "view" class for a clean button CSS
        );
    }
    
    // Display the button for all orders that have a 'ready-to-dispatch' status
    if ( $order->has_status( array( 'ready-to-dispatch' ) ) ) {
        
        $action_slug = 'complete';
    
        // Set the action button
        $actions['complete'] = array(
            'url'    => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
            'name'   => __( 'Complete', 'woocommerce' ),
            'action' => $action_slug,
        );
    }
    
    return $actions;
}
add_filter( 'woocommerce_admin_order_actions', 'filter_order_actions', 10, 2 );

/**
 *  Add WooCommerce icon for your action button @ admin orders list
 */
function action_admin_head() {
    $action_slug = 'ready-to-dispatch';
    echo '<style>.wc-action-button-' . $action_slug . '::after { content: "\f344" !important; }</style>';
}
add_action( 'admin_head', 'action_admin_head' );