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 );
});
@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