-
-
Save bdaley/d6cdb1d52ca8011003c79e072ef02391 to your computer and use it in GitHub Desktop.
<?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 ); | |
}); |
Hi Bdaley!
Could you write me where where exactly i must put this script in WooCommerce? I'm newbie...
Good question. lol. It's been a while since I've used this script.
I think I refactored this snippet as a plugin: https://github.com/bdaley/woocommerce-regenerate-and-notify
So, you could use it as a plugin or drop this snippet into your functions.php file.
Hello bdaley,
I'm ecstatic with your code for regenerating downloads. I've tried it and works like a charm.
My only concern is that I'd like go set the expiry date for one month can you tell me how to write it.
I appreciate your help.
I think you can just modify line 32 from the code above.
Here are the docs from WooCommerce on changing the expiry date:
https://woocommerce.github.io/code-reference/classes/WC-Customer-Download.html#method_set_access_expires
Thanks! This helped me.
Hello Bdaley,
I would like to set the download expiry date as Today+21 days. I did go through the link you have shared above, but could not figure it out. Will you please be able to help me with this?
Hmm... yeah, so I can't really test this out, but I think it might be something like this:
// Set the expiration date to +21 days
$expiration = new DateTime('+21 days');
// Code from Gist above
$downloads = $data_store->get_downloads(array('order_id' => $order->ID) );
if(is_array($downloads)){
foreach($downloads as $download){
$download->set_access_expires($expiration); // Use expiration date here
$download->save();
}
}
HTH. Good luck!
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?
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.
Hi Bdaley!
Could you write me where where exactly i must put this script in WooCommerce? I'm newbie...