• Resolved fluiditystudio

    (@fluiditystudio)


    Hey guys,

    I created a WooCommerce integration addon for your plugin a while back (posted here) and you had mentioned that you could create some hooks that would allow for more integration. I wanted to add another field or two to the add/edit license area in order to get my addon to fully integrate with your plugin. Is that possible anytime soon? I’m kind of stuck in finishing this addon until I can get these extra fields added.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Support mbrsolution

    (@mbrsolution)

    Hi, I have submitted a message to the plugin developers to investigate further your request.

    Regards

    Thread Starter fluiditystudio

    (@fluiditystudio)

    Thank you!!

    Do you just want a hook and a filter or you also need database column for it?

    I can add a hook that gets triggered when the save is processed. This can be used to save your own additional data. Then there will be a filter in that add/edit interface so you can render the data just above the submit button. Will that work for you?

    Thread Starter fluiditystudio

    (@fluiditystudio)

    That would be perfect! I will need a column to save the data in, but I can always have my plugin add that additional column in the database upon activation.

    I have added the hooks and filters.

    You can use any of the following two hooks to listen for the form submission then read the values from the REQUEST parameter and save to your database or a custom post type:

    slm_add_edit_interface_save_submission
    slm_add_edit_interface_save_record_processed

    The following is the filter that you can use to add the HTML code just before the submit button:

    slm_add_edit_interface_above_submit

    Rough usage example:

    
    add_filter('slm_add_edit_interface_above_submit, 'my_custom_output', 10, 2);
    function my_custom_output($output, $data){
        $row_id = $data['row_id'];
        $key = $data['key'];
        //TODO - query to retrieve your extra data so you can create the HTML output here
        $output .= '<input type="text" name="my_test_field" value="...." />'; 
    
        return $output
    }
    
    Thread Starter fluiditystudio

    (@fluiditystudio)

    Thank you, this is great!!

    Thread Starter fluiditystudio

    (@fluiditystudio)

    This worked like a charm by the way!

    Glad to hear that.

    Thread Starter fluiditystudio

    (@fluiditystudio)

    So, I have added my extra field to the “add/edit interface” and upon my plugin activation the filed is added to the database. When I manually create a new key from the “add/edit interface” it works great and my new field data is saved. However, when I try to insert this data upon completion of an order, it does not add the data. I’ve also discovered a while back that when I try to add the company name after a product purchase, that data doesn’t get inserted as well.

    Here is the foreach loop that I’m using:

    $items = $order->get_items();
    
    foreach($items as $item)
    	{
    	$product = $order->get_product_from_item($item);
    
    	// echo $item['name'];
    	$product_name = $item['name'];
    	$product_id = $item['product_id'];
    	$variation_id = $item['variation_id'];
    	$firstname = $order->billing_first_name;
    	$lastname = $order->billing_last_name;
    	$email = $order->billing_email;
    	$company = $order->billing_company;
    	$date = date('Y-m-d');
    	$dateExpiry = date('Y-m-d', strtotime('+1 year'));
    
    	// retrieve slm key from options
    	$slm_options = get_option('slm_plugin_options');
    	$secret_key = $slm_options['lic_creation_secret'];
    
    	// prepare the data
    	$api_params = array(
    		'slm_action' => 'slm_create_new',
    		'secret_key' => $secret_key,
    		'first_name' => $firstname,
    		'last_name' => $lastname,
    		'email' => $email,
    		'company_name' => '$company',
    		'txn_id' => $order_id,
    		'product_id' => $product_id,
    		'product_name' => $product_name,
    		'max_allowed_domains' => $license_count,
    		'date_created' => $date,
    		'date_expiry' => $dateExpiry,
    	);
    
    	// Send query to the license manager server
    	$query = esc_url_raw(add_query_arg($api_params, YOUR_LICENSE_SERVER_URL));
    	$response = wp_remote_get($query, array(
    		'timeout' => 20,
    		'sslverify' => false
    	));
    
    	// Check for error in the response
    	if (is_wp_error($response))
    		{
    		echo "Unexpected Error! The query returned with an error.";
    		}
    
    	// License data.
    	$license_data = json_decode(wp_remote_retrieve_body($response));
    
    } //end foreach item

    Out of the info attempted to insert into fields on license creation, the only data that doesn’t get added is “Company Name” as well as my two new fields added “product_id” & “product_name”. I have tested that the data is getting stored in the variables by printing the variables out on order completion. I just can’t figure out why those fields won’t get inserted into the db like the others.

    There is no database column with name “product_id” or “product_name” etc. So you can’t just pass them in to the plugin and expect it to magically insert them in the database table somehow.

    Your own custom data need to be saved in your own plugin’s database or maybe using custom post types. You can use the reference of license entry ID or the key so you can connect them (so you know which data in your custom table belongs to which key).

    Thread Starter fluiditystudio

    (@fluiditystudio)

    Actually I set my plugin on install to create those two columns in your license key table, so those fields are there. I have also added the input fields to the add/edit licenses section per your recently created hook. I can add the product id and name manually through the add/edit section and the data saves and updates. But, getting it to go through on my order completion does not work when using the code I stated above.

    The code base is what you supply on your site here, under “Creating license keys on your license manager server”. So, I’m thinking it may have something to do with that code and maybe it having preset fields, so adding mine won’t work. I’m not sure though.

    Thread Starter fluiditystudio

    (@fluiditystudio)

    Never mind, I got it myself. I just used the following code from your slm-third-party-integration.php file in your plugins includes folder. It worked perfectly and is storing the extra field data to the “wp_lic_key_tbl” table, since my plugin added the two new fields to the table on activation.

    $items = $order->get_items();
    
    foreach ( $items as $item ) {
    	$product = $order->get_product_from_item( $item );
    	$product_name = $item['name'];
    	$product_id = $item['product_id'];
    	$variation_id = $item['variation_id'];
    	$firstname = $order->billing_first_name;
    	$lastname = $order->billing_last_name;
    	$email = $order->billing_email;
    	$company = $order->billing_company;
    	$date = date('Y-m-d');
    	$dateExpiry = date('Y-m-d', strtotime('+1 year'));
    	
    	// retrieve slm key from options
    	global $slm_debug_logger;
    	global $wpdb;
    
    	$options = get_option('slm_plugin_options');
    
    	$lic_key_prefix = $options['lic_prefix'];
    
    	$fields = array();
    	$fields['license_key'] = uniqid($lic_key_prefix);
    	$fields['lic_status'] = 'pending';
    	$fields['first_name'] = $firstname;
    	$fields['last_name'] = $lastname;
    	$fields['email'] = $email;
    	$fields['company_name'] = $company;
    	$fields['txn_id'] = $order_id;
    	$fields['product_id'] = $product_id;
    	$fields['product_name'] = $product_name;
    	$fields['date_created'] = $date;//Today's date
    	$fields['max_allowed_domains'] = $license_count; //TODO - later take from estore's product configuration
    
    	$slm_debug_logger->log_debug('Inserting license data into the license manager DB table.');
    
    	$fields = array_filter($fields);//Remove any null values.
    
    	$tbl_name = SLM_TBL_LICENSE_KEYS;
    
    	$result = $wpdb->insert($tbl_name, $fields);
    
    	if(!$result){
    
    		$slm_debug_logger->log_debug('Notice! initial database table insert failed on license key table (User Email: '.$fields['email'].'). Trying again by converting charset', true);
    	}
    	
    }//end foreach item

    Thank you.

    I have created a tutorial giving a rough guideline on how to handle the custom fields:
    https://www.tipsandtricks-hq.com/adding-your-own-custom-fields-in-the-software-license-manager-plugin-8772

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Add/Edit License Hooks’ is closed to new replies.