Migrate from WooCommerce API Manager to WooCommerce Software License

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

The WooCommerce API Manager plugin is the first code of its kind available on the market by years now, so it’s widely spread. It had good features and support, still, it was very expensive and integration wasn’t straightforward for everyone. On top of the minuses is always been very slow making it impossible to use on low profile hosting or on medium to large websites with thousands of API calls per day.
The WooCommerce Software License plugin is a recent plugin which uses the new WooCommerce features and took a different approach to achieve the functionalities. It’s extremely fast due to its light code and an included cache module which provide outstanding performance for any sites.

Many users prefer to migrate to WooCommerce Software License plugin as being more reliable and incomparable lower cost. The following code can be used for that purpose, it translates the old license keys to the new system. Before starting the migration process, the WooCommerce Software License plugin should be active and key activated. Also, all products which use licensing should be filled in with appropriate data:

Further, add the following code to domain root:

<?php
    
    include('wp-config.php');
    
    global $post, $WOO_SL, $wpdb;
    
    //fetch all orders
    $args   =   array(
                        'post_type'         =>  'shop_order',
                        'posts_per_page'    =>  -1,
                        'post_status'       =>  'any',
                        'fields'            =>  'ids',
                        'orderby'           =>  'ID',
                        'order'             =>  'DESC'
                        );
                        
    $custom_query       =   new WP_Query( $args );
    
    while( $custom_query->have_posts() )
        {
            $custom_query->the_post();
            
            //check if alreaady processed, in case the code previouslly timed out
            $woosl_migrate_processed    =   get_post_meta( $post, '_woosl_migrated', TRUE );
            if( ! empty($woosl_migrate_processed))
                continue;
            
            $order  =   new WC_Order( $post );
            
            //Add the licensing dataon order
            $WOO_SL->functions->order_setup_licensing( $post, array() );
            
            /**
            * 
            * Update the expiration as, by default, code use today 
            * @var mixed
            */
            $order_products =   $order->get_items();
            
            $found_licensed_product =   FALSE;
            foreach($order_products as  $key    =>  $order_product)
                {
                    if (WOO_SL_functions::is_product_licensed($order_product['product_id']))
                        {
                            $found_licensed_product =   TRUE;
                            break;   
                        }
                }
            
            // the order need at least one licensed item    
            if(!$found_licensed_product)
                {
                    //mark as processed
                    update_post_meta($post, '_woosl_migrated', 'true');
                    continue;
                }
                
            
            //get user data
            $user_licensing_data    =   get_user_meta( $order->get_user_id(), $wpdb->prefix . 'wc_am_orders', TRUE );
            
            //feetch the licences for current order
            $licences   =   array();
            foreach  ( $user_licensing_data  as $license_key    =>  $license_data )
                {
                    if ( $license_data['order_id']  !=  $order->get_ID() )
                        continue;
                        
                    $licences[ $license_key ]   =   $license_data;
                }
            
            if ( count ( $licences ) < 1 )
                continue;
                
            
            /**
            * Always use the first group
            * 
            * @var mixed
            */
            $__licence_group_key    =   0; 
                
            foreach ( $order_products as $order_key   =>  $order_product ) 
                {
                    if(! $WOO_SL->functions->is_product_licensed($order_product['product_id']))
                        continue;    
                    
                    //insert the key
                    foreach  ( $licences  as $license_key    =>  $license_data )
                        {
                            $query = "INSERT INTO `" . $wpdb->prefix ."woocommerce_software_licence` 
                                            (`id`, `order_id`, `order_item_id`, `group_id`, `licence`, `created`, `active_domain`) 
                                            VALUES (NULL, ". $order->get_ID()  .", $order_key, ". $__licence_group_key .", '" . $license_key ."', '".$time."', NULL);";
                            $results = $wpdb->get_results($query);
                        }
       
       
       
       
       
                    $_woo_sl    =   WOO_SL_functions::get_order_item_meta( $order_key,  '_woo_sl',  TRUE);
                    if(!    is_array($_woo_sl))
                        continue;
   
                        
                    //check for different number of instances pe key
                    $_max_instances_per_key =   $_woo_sl['max_instances_per_key'][$__licence_group_key];
                    foreach ($licences as   $licence_data )
                        {
                            if ( $licence_data['_api_activations_parent']   >   $_max_instances_per_key )
                                $_max_instances_per_key =   $licence_data['_api_activations_parent'];
                        }
                    
                    $_woo_sl['max_instances_per_key'][$__licence_group_key] =   $_max_instances_per_key;
                    
                    wc_update_order_item_meta( $order_key, '_woo_sl', $_woo_sl);
                    
                    
                    
                    /**
                    * Mark the licence status as active if the order is completed
                    */
                    if ( $order->get_status()   ==  'completed' )
                        wc_update_order_item_meta( $order_key, '_woo_sl_licensing_status', 'active');
                        
                }    
            
            
            //mark as processed
            update_post_meta($post, '_woosl_migrated', 'true');
            
        }
                        
    
    echo "All Done";
    
?>

The file can be called directly through the site domain using the filename it’s been saved.
Before the code run, a database back-up is highly recommended to prevent any import issues. Since there are many possible scenarios, rarely there might be pieces of information that apparently do not migrate. In that case, we can help and make adjustments to the code.


Category:

By woocommerce-sl,posted on April 7, 2020