i use woocommerce subscriptions and software license manager master plugin, i combined the software license manager master plugin with woocommerce subscriptions plugin, i want to create a license for subscription products, for this reason, i want to make changes in some functions :
1 – by default, the software license manager master plugin only creates a license for the type of slm_license products, but i want to change it to a simple subscription product type. as soon as the order is paid, a license will be created, the function related to creating a license in the software license manager master plugin is as follows :
function wc_slm_on_complete_purchase($order_id) {
//SLM_Helper_Class::write_log('loading wc_slm_on_complete_purchase');
if (SLM_SITE_HOME_URL != '' && WOO_SLM_API_SECRET != '') {
wc_slm_create_license_keys($order_id);
}
} add_action('woocommerce_order_status_completed', 'wc_slm_on_complete_purchase', 10);
function wc_slm_create_license_keys($order_id) {
// SLM_Helper_Class::write_log('loading wc_slm_create_license_keys');
$order = wc_get_order($order_id);
$purchase_id_ = $order->get_id();
// SLM_Helper_Class::write_log('purchase_id_ -- '.$purchase_id_ );
// SLM_Helper_Class::write_log('purchase_id_ -- '.$user_id );
global $user_id;
$user_id = $order->get_user_id();
$user_info = get_userdata($user_id);
$get_user_meta = get_user_meta($user_id);
$payment_meta['user_info']['first_name'] = $get_user_meta['billing_first_name'][0];
$payment_meta['user_info']['last_name'] = $get_user_meta['billing_last_name'][0];
$payment_meta['user_info']['email'] = $get_user_meta['billing_email'][0];
$payment_meta['user_info']['company'] = $get_user_meta['billing_company'][0];
// SLM_Helper_Class::write_log('user_id -- '.$user_id );
// Collect license keys
$licenses = array();
$items = $order->get_items();
foreach ($items as $item => $values) {
$download_id = $product_id = $values['product_id'];
$product = $values->get_product();
if ($product->is_type('slm_license')) {
$download_quantity = absint($values['qty']);
//Get all existing licence keys of the product
$order_item_lic_key = $values->get_meta('_slm_lic_key', false);
$lic_to_add = $download_quantity - count($order_item_lic_key);
//Create keys only if there are not keys created already
for ($i = 1; $i <= $lic_to_add; $i++) {
/**
* Calculate Expire date
* @since 1.0.3
*/
$expiration = '';
$renewal_period = (int)wc_slm_get_licensing_renewal_period($product_id);
$renewal_term = wc_slm_get_licensing_renewal_period_term($product_id);
$slm_billing_length = $renewal_period;
$slm_billing_interval = $renewal_term;
if ($renewal_period == 'onetime') {
$expiration = '0000-00-00';
}
// elseif ($renewal_period == 30) {
// $renewal_period = date('Y-m-d', strtotime('+' . 31 . ' days'));
// }
else {
$expiration = date('Y-m-d', strtotime('+' . $renewal_period . ' ' . $renewal_term));
}
// SLM_Helper_Class::write_log('renewal_period -- '.$renewal_period );
// SLM_Helper_Class::write_log('exp -- ' . $expiration);
// SLM_Helper_Class::write_log('term -- ' . $renewal_term);
// Sites allowed get license meta from variation
$sites_allowed = wc_slm_get_sites_allowed($product_id);
if (!$sites_allowed) {
$sites_allowed_error = __('License could not be created: Invalid sites allowed number.', 'softwarelicensemanager');
$int = wc_insert_payment_note($purchase_id_, $sites_allowed_error);
break;
}
// Get the custumer ID
// $user_id = $order->get_user_id();
$order_data = $order->get_data(); // The Order data
## Access Order Items data properties (in an array of values) ##
$item_data = $values->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$_license_current_version = get_post_meta($product_id, '_license_current_version', true);
$_license_until_version = get_post_meta($product_id, '_license_until_version', true);
$amount_of_licenses_devices = wc_slm_get_devices_allowed($product_id);
$current_version = (int)get_post_meta($product_id, '_license_current_version', true);
$license_type = get_post_meta($product_id, '_license_type', true);
$lic_item_ref = get_post_meta($product_id, '_license_item_reference', true);
// Transaction id
$transaction_id = wc_get_payment_transaction_id($product_id);
// Build item name
$item_name = $product->get_title();
// Build parameters
$api_params = array();
$api_params['slm_action'] = 'slm_create_new';
$api_params['secret_key'] = KEY_API;
$api_params['first_name'] = (isset($payment_meta['user_info']['first_name'])) ? $payment_meta['user_info']['first_name'] : '';
$api_params['last_name'] = (isset($payment_meta['user_info']['last_name'])) ? $payment_meta['user_info']['last_name'] : '';
$api_params['email'] = (isset($payment_meta['user_info']['email'])) ? $payment_meta['user_info']['email'] : '';
$api_params['company_name'] = $payment_meta['user_info']['company'];
$api_params['purchase_id_'] = $purchase_id_;
$api_params['product_ref'] = $product_id; // TODO: get product id
$api_params['txn_id'] = $purchase_id_;
$api_params['max_allowed_domains'] = $sites_allowed;
$api_params['max_allowed_devices'] = $amount_of_licenses_devices;
$api_params['date_created'] = date('Y-m-d');
$api_params['date_expiry'] = $expiration;
$api_params['slm_billing_length'] = $slm_billing_length;
$api_params['slm_billing_interval'] = $slm_billing_interval;
$api_params['until'] = $_license_until_version;
$api_params['current_ver'] = $_license_current_version;
$api_params['subscr_id'] = $order->get_customer_id();
$api_params['lic_type'] = $license_type;
$api_params['item_reference'] = $lic_item_ref;
//access_expires
//SLM_Helper_Class::write_log('license_type -- ' . $license_type );
// Send query to the license manager server
$url = SLM_SITE_HOME_URL . '?' . http_build_query($api_params);
$url = str_replace(array('http://', 'https://'), '', $url);
$url = 'http://' . $url;
$response = wp_remote_get($url, array('timeout' => 20, 'sslverify' => false));
$license_key = wc_slm_get_license_key($response);
// Collect license keys
if ($license_key) {
$licenses[] = array(
'item' => $item_name,
'key' => $license_key,
'expires' => $expiration,
'type' => $license_type,
'item_ref' => $lic_item_ref,
'slm_billing_length' => $slm_billing_length,
'slm_billing_interval' => $slm_billing_interval,
'status' => 'pending',
'version' => $_license_current_version,
'until' => $_license_until_version
);
$item_id = $values->get_id();
wc_add_order_item_meta($item_id, '_slm_lic_key', $license_key);
wc_add_order_item_meta($item_id, '_slm_lic_type', $license_type);
}
}
}
}
if (count($licenses) > 0) {
// Payment note
wc_slm_payment_note($order_id, $licenses);
// Assign licenses
//What does this do? The meta is not used in the plugin anywhere
//wc_slm_assign_licenses($order_id, $licenses);
}
}
i want to change only one line of code : if ($product->is_type('slm_license')) change to if ( $product->is_type('subscription') && wcs_order_contains_subscription( $order, 'parent' ) && $order->has_status('completed') ), if i put the above 2 functions in the function.php file and apply my changes, it will be ok, is there a way to change just that one line of code without completely rewriting the 2 functions again?
2 – by default, after successful payment and automatic creation of the license, the license is pending, i want to change it to active. it means that after creating the automatic license, it should be activated, not pending. i found its function in software license manager master plugin, it is as follows :
function creation_api_listener() {
if (isset($_REQUEST['slm_action']) && trim($_REQUEST['slm_action']) == 'slm_create_new') {
//Handle the licene creation API query
global $slm_debug_logger, $wpdb;
$tbl_name = SLM_TBL_LICENSE_KEYS;
$options = get_option('slm_plugin_options');
$lic_key_prefix = $options['lic_prefix'];
SLM_API_Utility::verify_secret_key_for_creation(); //Verify the secret key first.
$slm_debug_logger->log_debug("API - license creation (slm_create_new) request received.");
//Action hook
do_action('slm_api_listener_slm_create_new');
$fields = array();
if (isset($_REQUEST['license_key']) && !empty($_REQUEST['license_key'])) {
$fields['license_key'] = strip_tags($_REQUEST['license_key']); //Use the key you pass via the request
} else {
$fields['license_key'] = slm_get_license($lic_key_prefix);
}
$fields['lic_status'] = isset($_REQUEST['lic_status']) ? wp_unslash(strip_tags($_REQUEST['lic_status'])) : 'pending';
$fields['first_name'] = wp_unslash(strip_tags($_REQUEST['first_name']));
$fields['last_name'] = wp_unslash(strip_tags($_REQUEST['last_name']));
$fields['purchase_id_'] = wp_unslash(strip_tags($_REQUEST['purchase_id_']));
$fields['email'] = strip_tags($_REQUEST['email']);
$fields['company_name'] = isset($_REQUEST['company_name']) ? wp_unslash(strip_tags($_REQUEST['company_name'])) : '';
$fields['txn_id'] = strip_tags($_REQUEST['txn_id']);
if (empty($_REQUEST['max_allowed_domains'])) {
$fields['max_allowed_domains'] = $options['default_max_domains'];
} else {
$fields['max_allowed_domains'] = strip_tags($_REQUEST['max_allowed_domains']);
}
if (empty($_REQUEST['max_allowed_devices'])) {
$fields['max_allowed_devices'] = $options['default_max_devices'];
} else {
$fields['max_allowed_devices'] = strip_tags($_REQUEST['max_allowed_devices']);
}
$fields['date_created'] = isset($_REQUEST['date_created']) ? strip_tags($_REQUEST['date_created']) : wp_date("Y-m-d");
$fields['date_expiry'] = isset($_REQUEST['date_expiry']) ? strip_tags($_REQUEST['date_expiry']) : '';
$fields['product_ref'] = isset($_REQUEST['product_ref']) ? wp_unslash(strip_tags($_REQUEST['product_ref'])) : '';
$fields['until'] = isset($_REQUEST['until']) ? wp_unslash(strip_tags($_REQUEST['until'])) : '';
$fields['current_ver'] = isset($_REQUEST['current_ver']) ? wp_unslash(strip_tags($_REQUEST['current_ver'])) : '';
//current_ver
$fields['subscr_id'] = isset($_REQUEST['subscr_id']) ? wp_unslash(strip_tags($_REQUEST['subscr_id'])) : '';
$fields['item_reference'] = isset($_REQUEST['item_reference']) ? wp_unslash(strip_tags($_REQUEST['item_reference'])) : '';
$fields['lic_type'] = isset($_REQUEST['lic_type']) ? wp_unslash(strip_tags($_REQUEST['lic_type'])) : '';
$fields['slm_billing_length'] = isset($_REQUEST['slm_billing_length']) ? wp_unslash(strip_tags($_REQUEST['slm_billing_length'])) : '';
$fields['slm_billing_interval'] = isset($_REQUEST['slm_billing_interval']) ? wp_unslash(strip_tags($_REQUEST['slm_billing_interval'])) : '';
if ($_REQUEST['lic_type'] == 'subscription' && !isset($_REQUEST['slm_billing_length'])) {
//error inserting
$error_args = (array(
'result' => 'error',
'message' => 'License creation failed. Specify license length "slm_billing_length".',
'error_code' => SLM_Error_Codes::CREATE_FAILED
));
SLM_API_Utility::output_api_response($error_args);
}
if ($_REQUEST['lic_type'] == 'subscription' && !isset($_REQUEST['slm_billing_interval'])) {
//error inserting
$error_args = (array(
'result' => 'error',
'message' => 'License creation failed. Specify license length "slm_billing_interval".',
'error_code' => SLM_Error_Codes::CREATE_FAILED
));
SLM_API_Utility::output_api_response($error_args);
}
$result = $wpdb->insert($tbl_name, $fields);
if ($result === false) {
//error inserting
$args = (array(
'result' => 'error',
'message' => 'License creation failed',
'error_code' => SLM_Error_Codes::CREATE_FAILED
));
SLM_API_Utility::output_api_response($args);
} else {
$args = (array(
'result' => 'success',
'message' => 'License successfully created',
'key' => $fields['license_key'],
'code' => SLM_Error_Codes::LICENSE_CREATED
));
// log data
global $wpdb;
SLM_Utility::create_log($fields['license_key'], 'slm_create_new');
SLM_API_Utility::output_api_response($args);
}
}
} add_action('slm_api_listener_init', 'creation_api_listener_1', 10);
i want to change only one line of code : $fields['lic_status'] = isset($_REQUEST['lic_status']) ? wp_unslash(strip_tags($_REQUEST['lic_status'])) : 'pending'; change to $fields['lic_status'] = isset($_REQUEST['lic_status']) ? wp_unslash(strip_tags($_REQUEST['lic_status'])) : 'active';, if i put the above function in the function.php file and apply my changes, it will be ok, is there a way to change just that one line of code without completely rewriting the function again?
any answer from you brings my gratitude