I have 1 subscription with 3 variations.
I’m trying to force the first renewal date depending on the variation ID that was purchased.
Variation 1 (876) set to renew every day – I want first re-bill date to be 11/15/2020 12:00AM
Variation 2 (877) set to renew every 2 days – I want first re-bill date to be 2/15/2021 12:00AM
Variation 3 (878) set to renew every 3 days – I want first re-bill date to be 8/15/2021 12:00AM
I thought the below code worked. After the order is created the next bill date DOES show one of the above dates, but it must not be registering with WooCommerce or something, because the renewals are triggering regardless of the above dates.
For my test, I create an order for Variation 1, the next payment date is showing 11/15/2020, but it’s renewing the next day.
Hoping to get some insight from someone smarter than me. Again, the sub’s next bill date is showing the above dates, but renewals are still happening early/before the above dates.
function nextpaymentdatechange( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === 876 ) {
$subid = $order_id + 1;
$nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
$new_date = date( 'Y-m-d H:i:s', strtotime( '2020-11-15 07:00:00', strtotime( $nextdate )) );
update_post_meta( $subid , '_schedule_next_payment', $new_date);
}
else{ if ( $product_id === 877 ) {
$subid = $order_id + 1;
$nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
$new_date = date( 'Y-m-d H:i:s', strtotime( '2021-02-15 07:00:00', strtotime( $nextdate )) );
update_post_meta( $subid , '_schedule_next_payment', $new_date);
}
else{
if ( $product_id === 878 ) {
$subid = $order_id + 1;
$nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
$new_date = date( 'Y-m-d H:i:s', strtotime( '2021-08-03 07:00:00', strtotime( $nextdate )) );
update_post_meta( $subid , '_schedule_next_payment', $new_date);
}
}
}
}
} ```
2
Answers
To make that work you need first to get
WC_Subscription
Objects from the order using:that gives an array of the WC_Subscription Object(s) for the Order, from an order Id.
Now you can use the
WC_Subscription
methodupdate_dates($dates)
, that requires to set an array of all dates types from a subscription that are'start'
,'trial_end'
,'next_payment'
,'last_payment'
and'end'
.To get a specific date from a subscription, we use
WC_Subscription
methodget_date($date_type)
.Additionally I use
WC_Subscription
methodcan_date_be_updated($date_type)
, to check if a date type can be updated.I am not sure about which ones dates require to be updated as they are likely related.
You can try the following that should update the related(s) subscription(s) dates from an order, based on your code (the code doesn’t throw errors).
I thin that you need to change
'next_payment'
,'last_payment'
and'end'
dates.The function code:
Code goes in functions.php file of your active child theme (or active theme). It should works.
if i understand, this :
Set automaticaly a end date 05 of december for my product 588 (it’s a simple subscription)?