久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Woocommerce 的管理訂單列表上處理自定義批量操

Process custom bulk action on admin Orders list in Woocommerce(在 Woocommerce 的管理訂單列表上處理自定義批量操作)
本文介紹了在 Woocommerce 的管理訂單列表上處理自定義批量操作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在我的 woocommerce 訂單頁面中添加了一個自定義操作,如下所示,我還有一個自定義訂單字段餐數".現在我想要的是當我批量選擇訂單并使用該自定義操作時,用餐次數應減少 1.
例如,如果訂單 ID 1 &2 有 15 &分別是 12 餐,然后在使用該動作后應變為 14 &11…

I have added a custom action in my woocommerce orders page like shown below, and I also have a custom order field "number of meals". Now what I want is when I select the orders in bulk and use that custom action then count of number of meals should get decreased by 1.
For example if order id 1 & 2 had 15 & 12 number of meals respectively then after using the action it should become 14 & 11…

我的訂單頁面截圖,我創建的自定義鉤子和自定義訂單字段:

The screenshot of my orders page, the custom hook and the custom order field I created:

我的代碼:

add_filter( 'bulk_actions-edit-shop_order', 'decrease_number_of_meals_by_1' );
function decrease_number_of_meals_by_1( $bulk_actions ) {
    $bulk_actions['decrease_number_of_meals'] = 'Decrease Number of Meals by 1';
    return $bulk_actions;
}

add_action( 'admin_action_decrease_number_of_meals', 'fire_my_hook' );
function fire_my_hook() {
    if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
        return;

    foreach( $_REQUEST['post'] as $order_id ) {

        $order = new WC_Order( $order_id );
        $no_of_meals = $order->get_post_meta( $order_id, '_wc_acof_{3}', true );
    }
}

我被困在這里,不知道如何進一步做.
請指導我如何實現這一目標.

I am stuck here and have no idea on how to do it further.
Please guide me on how can I achieve this.

推薦答案

您沒有使用正確的方法和鉤子.當使用 WooCommerce 管理自定義訂單字段插件時,正確的自定義字段元鍵應該是 _wc_acof_3.

You are not using the right way and hooks. Also the right custom field meta key should be _wc_acof_3 when using WooCommerce Admin Custom Order Fields plugin.

因此請嘗試以下操作:

// Add a bulk action to Orders bulk actions dropdown
add_filter( 'bulk_actions-edit-shop_order', 'decrease_meals_orders_bulk_actions' );
function decrease_meals_orders_bulk_actions( $bulk_actions ) {
    $bulk_actions['decrease_meals'] = 'Decrease Number of Meals by 1';
    return $bulk_actions;
}

// Process the bulk action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'decrease_meals_bulk_action_edit_shop_order', 10, 3 );
function decrease_meals_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
    if ( $action === 'decrease_meals' ){
        $processed_ids = array(); // Initializing

        foreach ( $post_ids as $post_id ) {
            // Get number of meals
            $nb_meal = (int) get_post_meta( $post_id, '_wc_acof_3', true );

            // Save the decreased number of meals ($meals - 1)
            update_post_meta( $post_id, '_wc_acof_3', $nb_meal - 1 );

            $processed_ids[] = $post_id; // Adding processed order IDs to an array
        }

        // Adding the right query vars to the returned URL
        $redirect_to = add_query_arg( array(
            'decrease_meals' => '1',
            'processed_count' => count( $processed_ids ),
            'processed_ids' => implode( ',', $processed_ids ),
        ), $redirect_to );
    }
    return $redirect_to;
}

// Display the results notice from bulk action on orders
add_action( 'admin_notices', 'decrease_meals_bulk_action_admin_notice' );
function decrease_meals_bulk_action_admin_notice() {
    global $pagenow;

    if ( 'edit.php' === $pagenow && isset($_GET['post_type']) 
    && 'shop_order' === $_GET['post_type'] && isset($_GET['decrease_meals']) {

        $count = intval( $_REQUEST['processed_count'] );

        printf( '<div class="notice notice-success fade is-dismissible"><p>' .
            _n( 'Decreased meals for %s Order.',
            'Decreased meals for %s Orders.',
            $count,
            'woocommerce'
        ) . '</p></div>', $count );
    }

代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.

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

這篇關于在 Woocommerce 的管理訂單列表上處理自定義批量操作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 在线观看中文字幕 | 91亚洲精 | 91玖玖| 日日操日日舔 | 综合久 | 亚洲在线一区二区 | 美国一级黄色片 | 丁香婷婷综合激情五月色 | 日韩欧美成人精品 | 91av在线电影 | 国产视频在线一区二区 | 国产一区二区免费 | av黄色网| 亚洲精品二区 | 国产精品一级在线观看 | 免费av毛片 | 久久蜜桃av | 色综合美女 | 色在线免费 | 日韩在线观看一区二区三区 | 婷婷综合网 | 毛片免费观看 | 久久久高清 | 久久这里有精品 | 日韩精品久久一区二区三区 | 亚洲91精品 | 一区二区三区四区在线视频 | 91精品国产综合久久久久蜜臀 | 亚洲欧洲在线观看视频 | 日韩a在线观看 | av片网站 | 国产在线观看一区二区 | 一级午夜aaa免费看三区 | 成人在线视频观看 | 伊人精品国产 | 久久久久久国产 | 青青草这里只有精品 | 久久91| 狠狠ri | 一区二区三区亚洲 | 五月激情六月婷婷 |