I have developed a plugin for WordPress that allows me to add licenses to the downloadable products. I’m currently developing the backend admin area.
To keep thing’s simple, I’m displaying the order software details in the html-order-download-permission.php (in replacement for the WooCommerce content). I have three buttons, email (sends license to customer), revoke (revokes the license) and renew. The first two buttons work fine.
The “renew” button and form only shows if the license is expired or revoked. When I click the button, the page appears to simply reload. I’ve added debugging code to the functions and there’s nothing in the logs, I’ve also added a JavaScript event listener to see if the form is being triggered when the submit button is clicked but nothing displays in the browsers console.
Here’s the custom code for the WooCommerce html-order-download-permission.php file:
<?php
if (! defined('ABSPATH')) {
exit;
}
?>
<div class="wc-metabox closed">
<h3 class="fixed">
<div class="handlediv" aria-label="<?php esc_attr_e('Click to toggle', 'woocommerce'); ?>"></div>
<strong>
<?php
printf(
'#%s — %s — %s: %s — ',
esc_html($product->get_id()),
esc_html(apply_filters('woocommerce_admin_download_permissions_title', $product->get_name(), $download->get_product_id(), $download->get_order_id(), $download->get_order_key(), $download->get_download_id())),
esc_html($file_count),
esc_html(wc_get_filename_from_url($product->get_file_download_path($download->get_download_id())))
);
printf(_n('Downloaded %s time', 'Downloaded %s times', $download->get_download_count(), 'woocommerce'), esc_html($download->get_download_count()));
?>
</strong>
</h3>
<table cellpadding="0" cellspacing="0" class="wc-metabox-content">
<tbody>
<?php
global $wpdb;
$order_id = $download->get_order_id();
// Fetch the license data even if it is revoked or expired
$license_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}software_licenses WHERE order_id = %d", $order_id));
// Check if valid license exists (not revoked and not expired)
$valid_license = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}software_licenses WHERE order_id = %d AND revoked = 0 AND access_expires >= NOW()", $order_id));
if ($valid_license) {
// Display valid license information
?>
<tr>
<!-- License Key Field -->
<td>
<p class="form-field form-field-wide">
<label for="license_key"><?php esc_html_e('License Key:', 'woocommerce'); ?></label>
<input type="text" readonly value="<?php echo esc_html($valid_license->license_key); ?>" class="input-text regular-input" style="width: <?php echo strlen($valid_license->license_key) * 8; ?>px;">
</p>
</td>
<!-- User Email Field -->
<td>
<p class="form-field form-field-wide">
<label for="user_email"><?php esc_html_e('User Email:', 'woocommerce'); ?></label>
<input type="text" readonly value="<?php echo esc_html($valid_license->user_email); ?>" class="input-text regular-input" style="width: <?php echo strlen($valid_license->user_email) * 8; ?>px;">
</p>
</td>
<!-- Access Expires Field -->
<td style="padding-right: 50px; max-width: 150px;">
<p class="form-field form-field-wide">
<label for="access_expires"><?php esc_html_e('Access Expires:', 'woocommerce'); ?></label>
<?php $expires_str = date_i18n('d F Y at H:i', strtotime($valid_license->access_expires)); ?>
<input type="text" readonly value="<?php echo esc_html($expires_str); ?>" class="input-text regular-input" style="width: <?php echo strlen($expires_str) * 8; ?>px;">
</p>
</td>
<!-- Email License Button -->
<td>
<p class="form-field form-field-wide">
<label for="email_license"><?php esc_html_e('Email License Key:', 'woocommerce'); ?></label>
<a href="<?php echo esc_url(add_query_arg(array('action' => 'email_license_key_action', 'order_id' => $order_id), admin_url('admin-post.php'))); ?>" class="button button-primary"><?php esc_html_e('Email', 'woocommerce'); ?></a>
</p>
</td>
<!-- Revoke License Button -->
<td>
<p class="form-field form-field-wide">
<label for="revoke_license"><?php esc_html_e('Revoke License Key:', 'woocommerce'); ?></label>
<a href="<?php echo esc_url(add_query_arg(array('action' => 'revoke_license_key', 'license_key' => $valid_license->license_key), admin_url('admin-post.php'))); ?>" class="button button-secondary"><?php esc_html_e('Revoke', 'woocommerce'); ?></a>
</p>
</td>
</tr>
<?php
} else {
// License expired or revoked, but still fetch license key for renewal
?>
<tr>
<td colspan="6">
<p><?php esc_html_e('The license for this order has either expired or has been revoked.', 'woocommerce'); ?></p>
</td>
</tr>
<tr>
<td>
<p class="form-field form-field-wide">
<label for="renew_license"><?php esc_html_e('Renew License Key:', 'woocommerce'); ?></label>
<!-- Add the renewal form where the license is displayed -->
<form method="POST" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<input type="hidden" name="action" value="process_license_renewal">
<input type="hidden" name="license_key" value="<?php echo esc_attr($license_key); ?>">
<input type="hidden" name="order_id" value="<?php echo esc_attr($order_id); ?>">
<select name="license_option">
<option value="12-months">12 Months</option>
<option value="lifetime">Lifetime</option>
</select>
<?php wp_nonce_field('renew_license_nonce', 'renew_license_nonce_field'); ?>
<input type="submit" value="Renew" id="renew-license-button" class="button button-secondary" style="width: 80%; text-align: center;">
</form>
</p>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<script>
document.getElementById('renew-license-button').addEventListener('click', function(event) {
console.log('Renew button clicked');
});
</script>
Here’s the function code:
<?php
// Handle license renewal form submission
add_action('admin_post_process_license_renewal', 'process_license_renewal');
function process_license_renewal()
{
// Verify the nonce
if (!isset($_POST['renew_license_nonce_field']) || !wp_verify_nonce($_POST['renew_license_nonce_field'], 'renew_license_nonce')) {
wp_die('Invalid nonce');
}
// Debugging log
error_log('Process License Renewal function called.');
// Check if all necessary data is available
if (isset($_POST['license_key'], $_POST['license_option'], $_POST['order_id'])) {
// Get data from the form
$license_key = sanitize_text_field($_POST['license_key']);
$license_option = sanitize_text_field($_POST['license_option']);
$order_id = intval($_POST['order_id']);
global $wpdb;
$table_name = $wpdb->prefix . 'software_licenses';
// Retrieve the license data from the database
$license_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE license_key = %s", $license_key));
if (!$license_data) {
error_log('No license data found for license key: ' . $license_key);
wp_die('Invalid license key.');
}
$product_id = $license_data->product_id;
$product = wc_get_product($product_id);
// Check if the product is a variable product
if ($product && $product->is_type('variable')) {
$variations = $product->get_available_variations();
$variation_id = null;
// Find the matching variation for the selected license option
foreach ($variations as $variation) {
$variation_license_option = $variation['attributes']['attribute_license-option'];
if ($variation_license_option === $license_option) {
$variation_id = $variation['variation_id'];
break;
}
}
if (! $variation_id) {
wp_die('No matching variation found for the selected license option.');
}
// Create a new order for the product variation
$new_order = wc_create_order();
$new_order->add_product(wc_get_product($variation_id), 1);
// Get customer details from the original order
$original_order = wc_get_order($order_id);
$customer_id = $original_order->get_customer_id();
$new_order->set_customer_id($customer_id);
$new_order->set_address($original_order->get_address(), 'billing');
$new_order->set_address($original_order->get_address(), 'shipping');
// Calculate totals and set order status
$new_order->calculate_totals();
$new_order->update_status('pending'); // Set status to pending or another appropriate status
// Redirect customer to the checkout page
wp_safe_redirect($new_order->get_checkout_order_received_url());
exit;
} else {
wp_die('Product is not a variable product.');
}
} else {
wp_die('Invalid request. Required data is missing.');
}
}
Here is a screenshot:
8