Update Order licence groups

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

Once checked-out, the licensing data will be saved as meta data, into customer order, according to product licensing set-up. The order licensing meta data will stay as is even if the product licensing will modify over time.

There are cased when changing the product license groups is required to be updated for specific orders. This can be achieved through a custom code:

include ('wp-config.php');

    global $post, $WOO_SL;
    
    
    $product_ID =   10;
    $order_ID   =   132546;
    
    
    $order = new WC_Order( $order_ID );
    
    $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']) &&  $order_product['product_id']    ==  $product_ID )
                {
                    $found_licensed_product = TRUE;
                    break;
                }
        }
        
        
    if (!$found_licensed_product)
        {
            echo "The Order does not contain the specified product or is not licensed.";
            die();
        }
        
    foreach($order_products as $order_item_key => $order_product)
        {
            if ( ! WOO_SL_functions::is_product_licensed($order_product['product_id']) ||  $order_product['product_id']    !=  $product_ID )
                continue;
            
           
            /**
            *
            * Generate  keys
            * @var {WOO_SL_functions|WOO_SL_functions}
            */
            $_woo_sl = WOO_SL_functions::get_order_item_meta($order_item_key, '_woo_sl', TRUE);
            
            $get_product_groups =   get_post_meta($order_product['product_id'], '_sl_groups', TRUE);
            
            //reindex the $_woo_sl
            $_woo_sl    =   array(
                                    'group_title'           =>  array(),
                                    'licence_prefix'        =>  array(),
                                    'max_keys'              =>  array(),
                                    'max_instances_per_key' =>  array(),
                                    'use_predefined_keys'   =>  array()  
                                    );
                                    
            foreach($get_product_groups as  $key    =>  $group_data)
                {
                    $_woo_sl['group_title'][$key]           =   $group_data['group_title'];
                    $_woo_sl['licence_prefix'][$key]        =   $group_data['licence_prefix'];
                    $_woo_sl['max_keys'][$key]              =   $group_data['max_keys'];
                    $_woo_sl['max_instances_per_key'][$key] =   $group_data['max_instances_per_key'];
                    $_woo_sl['use_predefined_keys'][$key]   =   $group_data['use_predefined_keys'];
                    
                }
                
                
            wc_update_order_item_meta ( $order_item_key, '_woo_sl', $_woo_sl );
                
            echo "Completed";
            
        }

The above code should be placed within a file saved in WordPress root.
There are 2 variables within the code which require to be changed for site specific:

  • $product_ID – The product for which the licensing groups where changed
  • $order_ID – The order ID for which the groups should be changed
  • Previous order generated licence keys will not lost, they will be assigned to the same group as before, unless it has been removed.


By woocommerce-sl,posted on November 17, 2017