Assign orders from guest customers to particular u

2019-07-11 01:11发布

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.

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

ORIGINAL ISSUE

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 ..

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

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..

Please help

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-11 01:25

There is error in your importing so try to re-import. Have you tried WP All Import Premium. It will import anything with drag and drop titles, categories, and meta fields. you can also assign new meta name to post meta while importing. If you need more help then share more details.

查看更多
甜甜的少女心
3楼-- · 2019-07-11 01:30

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 :

  • A max date (in YYYY-MM-DD 00:00:00 date time format)
  • Order number range

So here is that code:

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

Code goes in function.php file of your active child theme (active theme or in any plugin file).

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


USAGE:

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.

查看更多
登录 后发表回答