pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我在我的 woocommerce 訂單頁(yè)面中添加了一個(gè)自定義操作,如下所示,我還有一個(gè)自定義訂單字段餐數(shù)".現(xiàn)在我想要的是當(dāng)我批量選擇訂單并使用該自定義操作時(shí),用餐次數(shù)應(yīng)減少 1.
例如,如果訂單 ID 1 &2 有 15 &分別是 12 餐,然后在使用該動(dòng)作后應(yīng)變?yōu)?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…

我的訂單頁(yè)面截圖,我創(chuàng)建的自定義鉤子和自定義訂單字段:

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

我被困在這里,不知道如何進(jìn)一步做.
請(qǐng)指導(dǎo)我如何實(shí)現(xiàn)這一目標(biāo).

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

推薦答案

您沒(méi)有使用正確的方法和鉤子.當(dāng)使用 WooCommerce 管理自定義訂單字段插件時(shí),正確的自定義字段元鍵應(yīng)該是 _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.

因此請(qǐng)嘗試以下操作:

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

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.經(jīng)測(cè)試有效.

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

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

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

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問(wèn)題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 伺服电机_直流伺服_交流伺服_DD马达_拓达官方网站 | 单柱拉力机-橡胶冲片机-哑铃裁刀-江都轩宇试验机械厂 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 茶叶百科网-茶叶知识与茶文化探讨分享平台 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 合肥弱电工程_安徽安防工程_智能化工程公司-合肥雷润 | 上海办公室设计_办公楼,写字楼装修_办公室装修公司-匠御设计 | 武汉创亿电气设备有限公司_电力检测设备生产厂家| 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 苏州西朗门业-欧盟CE|莱茵UL双认证的快速卷帘门品牌厂家 | 磨煤机配件-高铬辊套-高铬衬板-立磨辊套-盐山县宏润电力设备有限公司 | 喷砂机厂家_自动除锈抛丸机价格-成都泰盛吉自动化喷砂设备 | 申江储气罐厂家,储气罐批发价格,储气罐规格-上海申江压力容器有限公司(厂) | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 | 德州万泰装饰 - 万泰装饰装修设计软装家居馆 | CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 金属波纹补偿器厂家_不锈钢膨胀节价格_非金属伸缩节定制-庆达补偿器 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 北京环球北美考试院【官方网站】|北京托福培训班|北京托福培训 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 智慧养老_居家养老_社区养老_杰佳通 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 沈阳缠绕包装机厂家直销-沈阳海鹞托盘缠绕包装机价格 | 氮化镓芯片-碳化硅二极管 - 华燊泰半导体| 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 天命文免费算命堂_自助算命_自由算命系统_长文周易 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 |