php 批量结算订单管理,php-在Woocommerce中处理管理订单列表上的自定义批量操作...

在WooCommerce中,通过自定义代码添加了一个批量操作,目的是当选择多个订单并使用此操作时,每个订单的“饭菜数量”字段减少1。已有的代码实现了在订单列表中显示这个新的批量操作,但未能成功更新选中订单的餐数。解决方案提供了修正后的代码,包括添加批量操作到订单列表、处理批量操作以减少餐数,并在操作完成后显示通知。
摘要由CSDN通过智能技术生成

我在woocommerce订单页面中添加了一个自定义操作,如下所示,并且我还有一个自定义订单字段“饭菜数量”.现在我想要的是,当我批量选择订单并使用该自定义操作时,用餐次数应减少1.

例如,如果订单ID为1& 2有15&分别使用12餐,则使用该操作后应变为14餐; 11 …

我的订单页面,自定义钩子和我创建的自定义订单字段的屏幕截图:

pU3JF.png

我的代码:

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

}

}

我被困在这里,不知道如何进一步做.

请指导我如何实现这一目标.

解决方法:

您没有使用正确的方法和挂钩.使用WooCommerce管理员自定义订单字段插件时,正确的自定义字段元键也应为_wc_acof_3.

因此,请尝试以下操作:

// 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( '

' .

_n( 'Decreased meals for %s Order.',

'Decreased meals for %s Orders.',

$count,

'woocommerce'

) . '

', $count );

}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

KBDPQ.png

wp8hW.png

标签:orders,php,wordpress,woocommerce,hook-woocommerce

来源: https://codeday.me/bug/20191009/1879849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值