Skip to content

Instantly share code, notes, and snippets.

@bernattorras
Created July 5, 2018 17:11
Show Gist options
  • Save bernattorras/a984463cc968dba2074d8e4a40ec604b to your computer and use it in GitHub Desktop.
Save bernattorras/a984463cc968dba2074d8e4a40ec604b to your computer and use it in GitHub Desktop.
Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
<?php
/**
* Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
**/
add_action('woocommerce_subscription_pre_update_status', 'save_trial_end_meta', 10, 3);
function save_trial_end_meta($old_status, $new_status, $sub){
if(in_array($new_status, array('cancelled', 'pending-cancel', 'switched', 'expired'))){
$sub_id = $sub->get_id();
$trial_end_date = $sub->get_date('trial_end');
update_post_meta($sub_id, '_trial_end_date', $trial_end_date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment