Last active
July 28, 2021 00:02
-
-
Save bdaley/d6cdb1d52ca8011003c79e072ef02391 to your computer and use it in GitHub Desktop.
Adds an action to the order page that allows you to regenerate download permissions AND extends the expiration date for WooCommerce downloads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('woocommerce_order_actions', 'wc_regenerate_and_notify_custom_order_action'); | |
function wc_regenerate_and_notify_custom_order_action( $actions ) { | |
// Remove the woocommerce option to regenerate to avoid confusion | |
unset($actions['regenerate_download_permissions']); | |
// Add our new action (executed below) | |
$actions['wc_regenerate_and_notify'] = __('Regenerate permissions & send link to customer', 'wc-regenerate-and-notify'); | |
return $actions; | |
} | |
/** | |
* We need to do 4 things: | |
* 1) Reset the download counter | |
* 2) Reset the download expiration date | |
* 3) Send order confirmation (again) with the download links | |
* 4) Add a note for recording purposes. * | |
*/ | |
add_action('woocommerce_order_action_wc_regenerate_and_notify', function($order){ | |
// 1) Reset the download counter | |
$data_store = WC_Data_Store::load( 'customer-download' ); | |
$data_store->delete_by_order_id( $order->ID ); | |
wc_downloadable_product_permissions( $order->ID, true ); | |
// 2) Retrieve Download and remove the access expiration. (Our poor customer has had enough trouble) | |
// *Could optionally reset to product default (x days or whatever) | |
$downloads = $data_store->get_downloads(array('order_id' => $order->ID) ); | |
if(is_array($downloads)){ | |
foreach($downloads as $download){ | |
$download->set_access_expires(null); | |
$download->save(); | |
} | |
} | |
// 3 & 4) Yay! Send an updated invoice to the customer with this message: | |
$order->add_order_note( __( "We've reset your download permissions. Please try downloading again.", 'woocommerce' ), true, true ); | |
}); |
I really don't know anything about using code snippets and whether or not they work with wordpress actions. Your best bet is to add all of the code to your functions.php file or use the plugin version.
You would just need to change this line, using the code from my last response.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Bdaley,
Thank you for the quick response.
I am getting this error when trying to activate the code:
**The code snippet you are trying to save produced a fatal error on line 0:
Exception thrown without a stack frame**
Any idea where am I going wrong?