WOOSL/API_call/response

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

Type
Filter

Arguments
(array) $response
(array) $args
(class) $api_object

Description
Allow to filter the API response.

Observation
The cache module need to be off for the filter to trigger.

Example of usage
Return additional custom field on key activation.

        add_filter('WOOSL/API_call/response', 'woosl_api_call_response', 10, 3);
        function woosl_api_call_response( $response, $args, $api_object )
            {
                //check if the API call method is not activate
                if  ( $args['woo_sl_action']    !=  'activate' ) 
                    return $response;
                    
                //feetch the last response in the list
                reset ( $response );
                
                $response   =   current( $response );
                
                //check for specific status, if operation was sucess
                if  (   ! in_array( 's100', $response['status_code'])   ||  ! in_array( 's100', $response['status_code']) )
                    return $response;
                
                //retrieve the product id
                $product_unique_id  = isset($args['product_unique_id']) ?   preg_replace("/[^a-zA-Z0-9.\-\_ ]/", "", $args['product_unique_id'] )       : '';
                $licence_key        = isset($args['licence_key'])       ?   preg_replace("/[^a-zA-Z0-9.\-\_\: ]/", "", $args['licence_key'] )             : '';
                $product_id = $api_object->product_validation($licence_key, $product_unique_id);
                
                //add the additional field
                $additional_field   =   get_post_meta( $product_id, 'field_name', TRUE );  
                
                $response['new_field']  =   $additional_field;
                          
                return $response;
            }

The following example returns an additional field with the customer name.

    add_filter('WOOSL/API_call/response', '_woosl_api_call_response', 10, 3);
    function _woosl_api_call_response( $response, $args, $api_object )
        {
            //check if the API call method is not activate
            if  ( $args['woo_sl_action']    !=  'status-check' ) 
                return $response;
                 
            //feetch the last response in the list
            reset ( $response );
             
            $response_block   =   current( $response );
             
            //check for specific status, if operation was sucess
            if  (   ! in_array( $response_block['status_code'], array ( 's203', 's205', 's215'  ) ) )
                return $response;
            
            $WOO_SL_Functions = new WOO_SL_functions();
             
            //retrieve the product id
            $product_unique_id  = isset($args['product_unique_id']) ?   preg_replace("/[^a-zA-Z0-9.\-\_ ]/", "", $args['product_unique_id'] )       : '';
            $licence_key        = isset($args['licence_key'])       ?   preg_replace("/[^a-zA-Z0-9.\-\_\: ]/", "", $args['licence_key'] )             : '';
            $product_id = $api_object->product_validation($licence_key, $product_unique_id);
            
            $licence_key_data   =   $WOO_SL_Functions->get_licence_key_data_by_licence_key( $licence_key );
            
            $order_data =   wc_get_order( $licence_key_data->order_id );
             
            //add the additional field
            $response_block['customer_name']  =   $order_data->billing_first_name . ' ' . $order_data->billing_last_name;
            
            $response[ key ( $response ) ]  =   $response_block;
                       
            return $response;
        }

By woocommerce-sl, posted on August 2, 2017

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments