I am creating a functionality for my client to offer subscription as a gift,
the last thing I need to do is to affect a subscription to a user.
I have succeeded, but since the user buys the subscription with a certain duration, I need to have an expiration date.
But when I use the built-in function “update dates” only the next payment gets affected
$product = wc_get_product($formuleId);
$order->add_product( $product, 1 );
$dt = new DateTime('now');
$dayNumber1 = date_format($dt, 'd');
$dayNumber1 = intval($dayNumber1);
$dayNumber = $dayNumber1-14;
$dayNumber *= -1;
$dt->modify($dayNumber.' days');
$startDate = $dt->format('Y-m-d H:i:s');
if($dayNumber1<=14){
$nbrMois = $row->NbrMois-1;
} else {
$nbrMois = $row->NbrMois;
}
$dt->modify('+'.$nbrMois.' month');
$endDate = $dt->format('Y-m-d H:i:s');
$sub = wcs_create_subscription(array(
'order_id' => $order->get_id(),
'status' => 'pending', // Status should be initially set to pending to match how normal checkout process goes
'billing_period' => WC_Subscriptions_Product::get_period( $product ),
'billing_interval' => WC_Subscriptions_Product::get_interval( $product )
));
if( is_wp_error( $sub ) ){
return false;
}
// Modeled after WC_Subscriptions_Cart::calculate_subscription_totals()
// Add product to subscription
$sub->add_product( $product, 1 );
$dates = array(
'trial_end' => WC_Subscriptions_Product::get_trial_expiration_date( $product, $endDate ),
'next_payment' => WC_Subscriptions_Product::get_first_renewal_payment_date( $product, $endDate ),
'end' => WC_Subscriptions_Product::get_expiration_date( $product, $endDate )
);
$sub->update_dates( $dates );
Aucun commentaire:
Enregistrer un commentaire