在 Woocommerce 中的管理订单列表上处理自定义批量操作

问题描述 投票:0回答:2

我在我的 woocommerce 订单页面中添加了一个自定义操作,如下所示,并且我还有一个自定义订单字段“餐数”。现在我想要的是,当我批量选择订单并使用该自定义操作时,餐食数量应该减少 1。
例如,如果订单 ID 1 和 2 的餐数分别为 15 和 12,那么在使用该操作后,它应该变成 14 和 11…

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

screenshot of my orders page

我的代码:

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

我被困在这里,不知道如何进一步做。
请指导我如何实现这一目标。

php wordpress woocommerce hook-woocommerce orders
2个回答
11
投票

您没有使用正确的方法和钩子。此外,使用 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( '<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 文件中。已测试并有效。

enter image description here

enter image description here


2
投票

如果您已切换到 HPOS,则屏幕 ID 现在为

woocommerce_page_wc-orders
。 所以,你需要使用这些钩子:

bulk_actions-woocommerce_page_wc-orders

handle_bulk_actions-woocommerce_page_wc-orders

这些钩子是动态的,使用像

bulk_actions-{screen_id}
这样的屏幕ID,并且是核心的一部分,在此处解释

对于 HPOS 订单列表页面,Woo 通过

this commit
实现了 handle 功能。

© www.soinside.com 2019 - 2024. All rights reserved.