• I’m sure many using this would like to have existing orders mapped.
    So maybe it makes sense to loop through existing orders on activation
    The store I’m using this on had thousands of guest orders, so I queued up the processing in a paginated way.

    This processes 100 orders per pass, and spaces them out to run every 5 minutes until done.

    I updated the activation function:

    function wc_map_guest_activate() {
      
        if( !class_exists( 'WooCommerce' ) ) {
    	
    		 deactivate_plugins( plugin_basename( __FILE__ ) );
             wp_die( __( 'Please activate WooCommerce before activating this plugin.', 'wc-map-guest-orders-and-downloads' ), 'Plugin dependency check', array( 'back_link' => true ) );
            
        }
    
        $args= ['post_type'=>'shop_order',
            'posts_per_page'=>100,
            'post_status'=>['wc-on_hold', 'wc-processing', 'wc-pending'],
            'meta_query'=>[
                'relation'=>'OR',
                [
                    'key'=>'_customer_user',
                    'compare'=>'NOT EXISTS',
                    'value'=>''
                ],
                array(
                    'key' => '_customer_user',
                    'value' => '0'
                )
            ]
            ];
    
        $guest_orders = new WP_Query($args);
    
        if($guest_orders->have_posts()){
    
            for($page = 1; $page<=$guest_orders->max_num_pages; $page++){
    
                $timestamp = time() + (300*$page);
                $hook = 'wc_get_paginated_orders';
    
                wp_schedule_single_event( $timestamp, $hook, [$args, $page] );
            }
    
        }
    
    }

    and then added the hook and function:

    function wc_get_paginated_orders($args, $page){
    
       
    
        $guest_orders = new WP_Query($args);
    
        if($guest_orders->have_posts()){
            foreach($guest_orders->get_posts() as $post){
                $billing_email = get_post_meta($post->ID, '_billing_email', true);
    
                $user = get_user_by('email', $billing_email);
    
                if($user){
                    wc_map_guestinitial_match_past_orders($user->ID);
                }
            }
            wp_reset_postdata();
        }
    }
    
    add_action('wc_get_paginated_orders' ,'wc_get_paginated_orders', 10, 2);
    • This topic was modified 7 years, 7 months ago by bheadrick.
    • This topic was modified 7 years, 7 months ago by bheadrick.
  • The topic ‘Process Existing Orders’ is closed to new replies.