Last active
August 26, 2021 08:22
-
-
Save thenbrent/5873892 to your computer and use it in GitHub Desktop.
Example code to sync a subscription to a specific date. All the good parts are thanks to @peterwilsoncc, all the bad parts are my own. This is not intended to be a complete and working snippet. Instead, treat it as a guide to what needs to be changed for syncing a payment date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Subscriptions Date Sync | |
* Plugin URI: | |
* Description: Sets the start date of a subscription to September 13, 2013 | |
* Author: Peter Wilson | |
* Author URI: http://peterwilson.cc/ | |
* Version: 1.0.0 | |
*/ | |
if ( ! defined( 'WCS_DATESYNC_STARTYEAR' ) ) define( 'WCS_DATESYNC_STARTYEAR', 2013 ); | |
if ( ! defined( 'WCS_DATESYNC_STARTMONTH' ) ) define( 'WCS_DATESYNC_STARTMONTH', 9 ); | |
if ( ! defined( 'WCS_DATESYNC_STARTDAY' ) ) define( 'WCS_DATESYNC_STARTDAY', 13 ); | |
class WCS_Datesync { | |
public $start_date; | |
public $current_date; | |
public $days_until_start; | |
function __construct() { | |
$this->init_dates(); | |
add_filter( 'woocommerce_subscriptions_calculated_next_payment_date', array( &$this, 'calculated_next_payment_date' ), 10, 6 ); | |
add_filter( 'get_post_metadata', array( &$this, 'filter_post_metadata' ), 10, 4 ); | |
add_filter( 'woocommerce_paypal_args', array( &$this, 'paypal_standard_subscription_args' ), 11 ); | |
} | |
function init_dates() { | |
$start_date = &$this->start_date; | |
$current_date = &$this->current_date; | |
$days_until_start = &$this->days_until_start; | |
$hour = 12; | |
$minute = 0; | |
$second = 0; | |
$month = WCS_DATESYNC_STARTMONTH; | |
$day = WCS_DATESYNC_STARTDAY; | |
$year = WCS_DATESYNC_STARTYEAR; | |
if ( $year < 100 ) { | |
$year = $year + 2000; | |
} | |
$start_date = mktime( $hour, $minute, $second, $month, $day, $year ); | |
$current_date = time(); | |
$days_until_start = floor( ( $start_date - $current_date ) / ( 60 * 60 * 24 ) ); | |
if ( ( $start_date < $current_date ) OR ( $days_until_start < 0 ) ) { | |
$start_date = time(); | |
$days_until_start = 0; | |
} | |
} | |
function calculated_next_payment_date( $next_payment, $order, $product_id, $type, $from_date, $from_date_arg ) { | |
$start_date = &$this->start_date; | |
if ( $next_payment < $start_date ) { | |
$next_payment = $start_date; | |
} | |
return $next_payment; | |
} | |
function filter_post_metadata( $meta_data, $object_id, $meta_key, $single ) { | |
$meta_type = 'post'; | |
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); | |
if ( !$meta_cache ) { | |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); | |
$meta_cache = $meta_cache[$object_id]; | |
} | |
if ( $meta_cache['_subscription_period_interval'] > 0 ) { | |
// it's a subscription | |
$meta_cache['_subscription_trial_length'][0] = $this->days_until_start; | |
$meta_cache['_subscription_trial_period'][0] = 'day'; | |
} | |
if ( !$meta_key ) | |
return $meta_cache; | |
if ( isset($meta_cache[$meta_key]) ) { | |
if ( $single ) | |
return maybe_unserialize( $meta_cache[$meta_key][0] ); | |
else | |
return array_map('maybe_unserialize', $meta_cache[$meta_key]); | |
} | |
if ($single) | |
return ''; | |
else | |
return array(); | |
return $meta_data; | |
} | |
function paypal_standard_subscription_args( $paypal_args ) { | |
if ( ( $paypal_args['cmd'] == '_xclick-subscriptions' ) AND ( $this->days_until_start > 0 ) ) { | |
// it's a subscription and we're trialling it | |
$paypal_args['item_name'] .= "–"; | |
$paypal_args['item_name'] .= "Your subscription begins on "; | |
$paypal_args['item_name'] .= date( "F d, Y", $this->start_date ); | |
} | |
return $paypal_args; | |
} | |
} | |
$wcs_datesync = new WCS_Datesync(); |
@scotty0100 Did you ever complete start / end date integration? If so, can you please post your code?
Has this been tested with WooCommerce 3.0+?
How do you use this?
I need to sync all orders on a to a single Thursday every 4 weeks. I've searched the web high and low and this is the only resource that seems to offer hope... can anyone give me some basic pointers on how to deploy it?
@stephenclark2000 did you find any solution?
This plugin is a great option: https://shopplugins.com/plugins/woocommerce-subscriptions-schedule/
(I'm not associated with it, I just know that it works)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setting $single = 0 at the start of the function solved it ;)
Btw, would you know how to pass variable product data to the function so I can use it as a start/end date?
Eg. I have a variable product option that declares start and end date of the subscription, so just want to replace the hardcoded start date in the function.