簡體   English   中英

"WooCommerce 管理訂單列表中用於自定義訂單狀態的操作按鈕問題"

[英]Issue with action button in WooCommerce admin order list for custom order status

我還創建了自定義訂單狀態“准備發貨”和自定義操作按鈕。

問題和問題是:當我單擊自定義操作按鈕時,訂單狀態已更改,但默認操作按鈕“完成”消失了。

單擊自定義操作按鈕后,如何使“完成”操作按鈕保留?

任何意見,將不勝感激

我當前的代碼:

// 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;
}

這是因為在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' );

是否可以在移動應用程序上顯示此按鈕?

只有當我們選擇“處理”狀態時才會出現,有沒有辦法在手機上顯示?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM