-
-
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 ); | |
}); |
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.
I think you can just modify line 32 from the code above.
https://gist.github.com/bdaley/d6cdb1d52ca8011003c79e072ef02391#file-wc-regenerate-and-notify-php-L32
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