How to set licensing for previous Orders after installing WooCommerce Software License

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

WooCommerce Software License is a powerful tool to maintain licensing for any product type. It feature an API through which licence management is a breeze.

Many customers migrated from old systems and plugins to WooCommerce Software License, to take advantage of it’s features and functionality. At the point the previous orders need to be processed to include required data. For all new orders those will be created automatically at the time of checkout.

Before starting the migration process, the following need to be ensured to meet:

Create a file within WordPress root directory and include the following code:


include ('wp-config.php');

global $post, $WOO_SL;

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

    $woosl_migrate_processed = get_post_meta($post, '_woosl_migrate_processed', TRUE);
    if (!empty($woosl_migrate_processed)) continue;
    $order = new WC_Order($post);

    // process

    $WOO_SL->functions->woocommerce_checkout_order_processed($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_migrate_processed', 'true');
        continue;
    }

    foreach($order_products as $key => $order_product)
    {
        if (!WOO_SL_functions::is_product_licensed($order_product['product_id'])) continue;
        $is_licence_extend = FALSE;
        $_woo_sl_extend = wc_get_order_item_meta($key, '_woo_sl_extend', TRUE);
        if (!empty($_woo_sl_extend)) $is_licence_extend = TRUE;

        // no need to process if is an licence extend

        if ($is_licence_extend) continue;

        // add licence expire informations if apply

        if (WOO_SL_functions::product_using_licence_expire($order_product['product_id']))
        {
            wc_update_order_item_meta($key, '_woo_sl_licensing_using_expire', 'yes');

            // use order date

            $today = $order->order_date;
            $start_at = strtotime($today);
            wc_update_order_item_meta($key, '_woo_sl_licensing_start', $start_at);
            $_sl_product_expire_units = get_post_meta($order_product['product_id'], '_sl_product_expire_units', TRUE);
            $_sl_product_expire_time = get_post_meta($order_product['product_id'], '_sl_product_expire_time', TRUE);
            $expire_at = strtotime(" + " . $_sl_product_expire_units . " " . $_sl_product_expire_time, $start_at);
            wc_update_order_item_meta($key, '_woo_sl_licensing_expire_at', $expire_at);

            // set currently as inactive

            wc_update_order_item_meta($key, '_woo_sl_licensing_status', 'inactive');
        }

        /**
         *
         * Generate  keys
         * @var {WOO_SL_functions|WOO_SL_functions}

         */
        $_woo_sl = WOO_SL_functions::get_order_item_meta($key, '_woo_sl', TRUE);
        if (!is_array($_woo_sl)) continue;
        foreach($_woo_sl['group_title'] as $group_key => $_group_title)
        {
            $license_keys = (array)WOO_SL_functions::get_order_product_generated_keys($order->get_id() , $key, $group_key);
            if (count($license_keys) > 0) continue;

            // allow to change the number of keys to be generated; default is 1

            $generate_keys_count = apply_filters('woo_sl/generate_licence_keys_count', 1, $order->get_id() , $key, $group_key);
            $new_licence_keys = WOO_SL_functions::generate_license_key($order->get_id() , $key, $group_key, $generate_keys_count);
        }

        /**
         * Mark the licence status as active if the order is completed
         */
        if ($order->get_status() == 'completed') wc_update_order_item_meta($key, '_woo_sl_licensing_status', 'active');
    }

    // mark as processed

    update_post_meta($post, '_woosl_migrate_processed', 'true');
}

echo "Completed";

Call the file through your domain e.g. domain.com/created_file.php and wait for the process to complete. Depending on the number of orders to process, this can take few seconds, up to few minutes. If the code times out, just call again the file and it will resume.

If there’s any product which use expiration, it will set the start time at the order date. Also license is being set active only for completed orders.


By woocommerce-sl,posted on August 1, 2017