在Woocommerce管理员订单列表上处理多个自定义批量操作

时间:2019-05-15 17:13:49

标签: php wordpress woocommerce hook-woocommerce orders

在使用插件Admin Custom Order Fields的WooCommerce中,我在WooCommerce管理订单列表上添加了一个自定义字段“返利状态”,其中包含3个值“无返利”,“未付款”和“已付费”。

enter image description here

我还在查看顺序屏幕上显示了该内容,就像下面的屏幕截图一样:

enter image description here

现在,我想批量更新所选订单的回扣状态,就像wooCommerce允许批量更改订单状态时一样。

基于Process custom bulk action on admin Orders list in Woocommerce回答线程,我在批量编辑下拉菜单中成功添加了3个返利状态(如您在第一张屏幕截图中所见)

const s = 'My ID is 112243 and my phone number is 0987654321. Thanks you.'

console.log(s.match(/\d+(?=\D*$)/))

但是当我尝试批量更新所选订单的返利状态时,没有应用任何更改。

回扣状态的元密钥为add_filter( 'bulk_actions-edit-shop_order', 'decrease_meals_orders_bulk_actions' ); function decrease_meals_orders_bulk_actions( $bulk_actions ) { $bulk_actions['mr_norebates'] = 'Mark Transactions as No Rebates'; $bulk_actions['mr_unpaid'] = 'Mark Transactions as Unpaid'; $bulk_actions['mr_paid'] = 'Mark Transactions as Paid'; return $bulk_actions; }

我也被困住了,不知道如何解决问题。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

实际上我是在您的代码的帮助下使它起作用的,但是它太长了,如果您能使其变得简单,我将不胜感激。

add_filter( 'bulk_actions-edit-shop_order', 'decrease_meals_orders_bulk_actions' );
function decrease_meals_orders_bulk_actions( $bulk_actions ) {
    $bulk_actions['mr_norebates'] = 'Mark Transactions as No Rebates';
    $bulk_actions['mr_unpaid'] = 'Mark Transactions as Unpaid';
    $bulk_actions['mr_paid'] = 'Mark Transactions as Paid';
    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 === 'mr_norebates' ){
        $processed_ids = array(); // Initializing

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

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

            $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(
            'mr_norebates' => 'No Rebates',
            'processed_count' => count( $processed_ids ),
            'processed_ids' => implode( ',', $processed_ids ),
        ), $redirect_to );
    }

    elseif ( $action === 'mr_unpaid' ){
        $processed_ids = array(); // Initializing

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

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

            $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(
            'mr_unpaid' => 'Unpaid',
            'processed_count' => count( $processed_ids ),
            'processed_ids' => implode( ',', $processed_ids ),
        ), $redirect_to );
    }
    elseif ( $action === 'mr_paid' ){
        $processed_ids = array(); // Initializing

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

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

            $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(
            'mr_paid' => 'Paid',
            '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() {
    if ( empty( $_REQUEST['mr_norebates'] ) ) return; // Exit

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

    printf( '<div id="message" class="updated fade"><p>' .
        _n( 'Selected %s transaction updated.',
        'Selected %s transactions updated.',
        $count,
        'mr_norebates'
    ) . '</p></div>', $count );

答案 1 :(得分:1)

这是一种完整,紧凑和优化的方法,可使其适用于3个操作中的每一个,以批量更新显示摘要通知的自定义“回扣状态”:

// Your settings in a function
function custom_admin_orders_bulk_actions( $labels = false ){
    $domain = 'woocommerce';
    return array(
        'mr_norebates'  => $labels ? __('No Rebates', $domain) : 'norebates',
        'mr_unpaid'     => $labels ? __('Unpaid', $domain)     : 'unpaid',
        'mr_paid'       => $labels ? __('Paid', $domain)       : 'paid',
    );
}

// Display the custom actions on admin Orders bulk action dropdown
add_filter( 'bulk_actions-edit-shop_order', 'set_transactions_orders_bulk_actions' );
function set_transactions_orders_bulk_actions( $bulk_actions ) {
    foreach( custom_admin_orders_bulk_actions(true) as $key => $label ) {
        $bulk_actions[$key] = sprintf( __('Mark Transactions as %s', 'woocommerce'), $label );
    }
    return $bulk_actions;
}

// Process the bulk action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'set_transactions_bulk_action_edit_shop_order', 10, 3 );
function set_transactions_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
    $actions = custom_admin_orders_bulk_actions();

    if ( in_array( $action, array_keys($actions) ) ) {
        $processed_ids = array(); // Initializing

        foreach ( $post_ids as $post_id ) {
            // Save the new value
            update_post_meta( $post_id, '_wc_acof_2', $actions[$action] );

            $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(
            'rebate_action'   => $action,
            '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', 'set_transactions_bulk_action_admin_notice' );
function set_transactions_bulk_action_admin_notice() {
    global $pagenow;

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

        foreach( custom_admin_orders_bulk_actions(true) as $key => $label ) {
            if (  $_GET['rebate_action'] === $key ) {

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

                printf( '<div class="notice notice-success fade is-dismissible"><p>' .
                    _n( '%s selected order updated to "%s" rebate status.',
                    '%s selected orders updated to "%s" rebate status.',
                    $count, 'woocommerce' )
                 . '</p></div>', $count, $label );
            }
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

基于:Process custom bulk action on admin Orders list in Woocommerce