Skip to content

Instantly share code, notes, and snippets.

@bdaley
Last active July 28, 2021 00:02
Show Gist options
  • Save bdaley/d6cdb1d52ca8011003c79e072ef02391 to your computer and use it in GitHub Desktop.
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.
<?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 );
});
@paaweu
Copy link

paaweu commented May 23, 2021

Hi Bdaley!
Could you write me where where exactly i must put this script in WooCommerce? I'm newbie...

@bdaley
Copy link
Author

bdaley commented May 25, 2021

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.

@Mcasas
Copy link

Mcasas commented May 26, 2021

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.

@bdaley
Copy link
Author

bdaley commented May 27, 2021

@elron
Copy link

elron commented Jul 18, 2021

Thanks! This helped me.

@ManaliChitalia
Copy link

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?

@bdaley
Copy link
Author

bdaley commented Jul 27, 2021

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!

@ManaliChitalia
Copy link

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?

@bdaley
Copy link
Author

bdaley commented Jul 28, 2021

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.

https://github.com/bdaley/woocommerce-regenerate-and-notify/blob/afaeda5f9c498f51018eaa77065951e415811b5a/woocommerce-regenerate-and-notify.php#L75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment