Advertisement
tex2005

Woo Subscriptions- Modify Trial-Expire and Next-Payment Date

Mar 19th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. /**
  2.  * FREE TRIAL EXPIRATION DATE for MONTHLY-ONGOING
  3.  * Quarterly needs different calc !
  4.  * for Ongoing club subscriptions (monthly and quarterly), which charge on the 1st of the next month,
  5.  * NOT immediately (as other subscriptions do)
  6.  *
  7.  * @hook  in classes/class-wc-subscriptions-manager.php#1855
  8.  * @ref  http://www.onlineconversion.com/unix_time.htm
  9.  */
  10. add_filter ('woocommerce_subscription_calculated_trial_expiration_date','tz_wc_filter2',10,2);
  11. function tz_wc_filter2($expiration_date, $subscription_key) {
  12.  
  13.     // TODO: if($subscription_length == 0){ // ongoing!
  14.     // how to find that here??
  15.  
  16.     date_default_timezone_set('America/Los_Angeles');
  17.     $now = date('Y-m-d');
  18.     $d = new DateTime( $now );
  19.     $d->modify( 'last day of this month' );
  20.     $trial_expire_date = $d->format( 'Y-m-d H:i:s' );
  21.     $trial_expire_unix = strtotime($trial_expire_date);
  22.     $expiration_date = $trial_expire_unix;
  23.     return $expiration_date;
  24. }
  25.  
  26. /**
  27.  * NEXT PAYMENT DATE for MONTHLY-ONGOING
  28.  * Quarterly needs different calc !
  29.  * for Ongoing club subscriptions (monthly and quarterly), which charge on the 1st of the next month,
  30.  * not immediately (as other subscriptions do)
  31.  *
  32.  * @hook  in classes/class-wc-subscriptions-order.php#488
  33.  */
  34. add_filter ('woocommerce_subscriptions_calculated_next_payment_date','tz_wc_filter3',10,3);
  35. function tz_wc_filter3($next_payment,$product_id,$order) {
  36.  
  37.     $product_meta = get_post_meta($order);
  38.     $subscription_length = $product_meta['_subscription_length']['0'];
  39.     if($subscription_length == 0){ // ongoing!
  40.  
  41.         date_default_timezone_set('America/Los_Angeles');
  42.         $now = date('Y-m-d');
  43.         $d = new DateTime( $now );
  44.         $d->modify( 'first day of next month' );   
  45.         $next_payment_date = $d->format( 'Y-m-d H:i:s' );
  46.         $next_payment_unix = strtotime($next_payment_date);
  47.         $next_payment = $next_payment_unix;
  48.     }
  49.     return $next_payment;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement