将来宾客户的订单分配给特定用户 [英] Assign orders from guest customers to particular user

查看:23
本文介绍了将来宾客户的订单分配给特定用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来解决这个问题,但这不适用于来宾客户的订单.但是,这适用于属于某些注册用户/客户的订单但不适用于属于来宾客户的订单.

I am using the following code to solve this but this is not working for orders from guest customers . However this is working fine for the orders belonging to some registered user/customer but not for the orders belonging to guest customers.

解决方案归功于 LoicTheAztec答案

Solution credit to LoicTheAztec for answer

function cristmas_bulk_editing_orders(){

    if(!is_admin()) return; // Will work only from Admin Backed.
    else {

        $order_id = 9458;
        $new_customer_id = 479;

            // Getting the postmeta customer ID for 'order' post-type
            $customer_id = get_post_meta( $order_id, '_customer_user', true );
            var_dump($customer_id);
            // If it's an existing order and doesn't have already this user ID
            // It update the customer ID
            if( !empty($customer_id) && $new_customer_id != $customer_id )
                update_post_meta($order_id, '_customer_user', $new_customer_id,0);
                echo 'order updated';

    }

}
cristmas_bulk_editing_orders();

原始问题

我们通过 woocommerce order export & 导入订单从 woocommerce 团队导入插件..

We imported the orders via woocommerce order export & import plugin from woocommerce team ..

但是在这个过程中出了点问题..大部分订单都没有分配给任何客户..

But in the process something went wrong.. Most of the orders were not assigned any customer ..

因此,现在每当新客户注册时,他/她都会自动分配到这些订单中的 1 个..

So now when ever a new customer registers he/she is assigned 1 of these orders automatically ..

所以基本上他们所有人都在他们最近的订单中看到 1 个属于其他客户客户的订单,然后他们就拥有了其他客户的所有信息.他们的电子邮件等.

So basicallly all of them see 1 order in their recent orders which belongs to some other guest cusotmer , then they have all the information about other customer . their email etc..

所以一种选择是我找出所有订单(没有客户协助他们的问题)并将它们分配给管理员......但这也有一些问题......

So one option is I find out all the orders(with issues that is no customer assisned to them ) and I assign them to admin ..but this also have some issuses.....

所以有没有其他选择可以让这些新注册用户不分配旧订单..

SO is there any other option that these new registered users don't get old orders assigned..

请帮忙

推荐答案

也许您可以尝试基于 SQL 查询的其他类似功能,这应该可以解决问题.在开始之前,进行数据库备份.我添加了控件(或限制),例如:

May be you could try this other similar function based on SQL queries, that should do the trick. Before starting, make a database backup. I have add controls (or limitations) like :

  • 最大日期(YYYY-MM-DD 00:00:00 日期时间格式)
  • 订单号范围

代码如下:

function easter_bulk_editing_orders(){

    if( ! is_admin() ) return; // Will work only from Admin Backed.
    else {
        $processed_orders = array();

        // Define the replacement user ID
        $replacement_user_id = '2500';

        // Define the DATE LIMIT for guest orders max date limit
        $max_date = '2017-12-09 00:00:00';

        // Define an order range
        $order_id_min = '0';
        $order_id_max = '100000';

        global $wpdb;

        // Get all guest orders below a defined max date
        $old_guest_orders = $wpdb->get_results( "
            SELECT pm.*, p.post_date
            FROM {$wpdb->prefix}postmeta AS pm
            LEFT JOIN {$wpdb->prefix}posts AS p ON pm.post_id = p.ID
            WHERE pm.post_id BETWEEN $order_id_min AND $order_id_max
            AND pm.meta_key LIKE '_customer_user'
            AND ( pm.meta_value LIKE '0' OR pm.meta_value LIKE '' )
            AND p.post_date <= '$max_date'
        " );

        foreach( $old_guest_orders as $guest_order ){
            $meta_id = $guest_order->meta_id;

            $wpdb->query( "
                UPDATE {$wpdb->prefix}postmeta as pm
                SET pm.meta_value = '$replacement_user_id'
                WHERE pm.meta_id = '$meta_id'
            " );
            // Set each order ID in an array
            $processed_orders[] = $guest_order->post_id;
        } 
        // Testing ( raw output of processed orders IDS )
        var_dump($processed_orders);
    }
}

// Run the function
easter_bulk_editing_orders();

代码位于活动子主题(活动主题或任何插件文件)的 function.php 文件中.

您只需使用此功能一次,然后再将其删除(见下文).

You have to use this function only once and to remove it afterwards (see below).

用法:

将此代码粘贴并保存在 function.php 文件中后,在浏览器中从后端显示或重新加载任何管理页面.现在您可以通过这种方式评论该函数并保存:

Once this code is pasted and saved on function.php file, display or reload any Admin page from backend within your browser. Now you can comment the function this way and save:

// easter_bulk_editing_orders();

检查订单是否已根据需要更改,并删除所有这些代码.

Check that the orders have been changed as you want, and remove all this code.

代码已经过测试并且可以正常工作.

Code is tested and works.

这篇关于将来宾客户的订单分配给特定用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆