I have used woo-commerce subscription plugin for my products. Now client want that if a subscription is expired, a new subscription is automatically started for the same order and customer. I an using the schedule action expiry hook to proceed further. Now when a subscription expired, below my script is used, which create a new subscription for order. But the initial payment is not working for that. Will you please look into that and help me. This is my code after expiry hook script.
/* after subscription expired */
$subscription = wcs_get_subscription( $subscription_id );
// wcs_create_resubscribe_order( $subscription );
$order = method_exists( $subscription, 'get_parent' ) ? $subscription->get_parent() : $subscription->order;
$items = $order->get_items();
foreach ( $items as $item ) {
$order_id = $order->get_order_number();
$product_name = $item->get_name();
$product_id = $item->get_product_id();
/* Get variation attribute based on product ID */
$product = new WC_Product_Variable( $product_id );
$variations = $product->get_available_variations();
foreach ($variations as $variation) {
if($variation['attributes']['attribute_velg-antall'] == 'Custom'){
$variationID = $variation['variation_id'];
$price_args = number_format($variation['display_price'],2);
}
}
}
$quantity = 1;
$variation_id = (string)$variationID;
$variation_factory = new WC_Product_Variation($variation_id);
$variation_obj = $variation_factory->get_variation_attributes();
$price_params = array( 'variation' => $variation_obj, 'totals' => array( 'subtotal' => $price_args*$quantity, 'total' => $price_args*$quantity, 'subtotal_tax' => 0, 'tax' => 0 ) );
$start_date = wcs_get_datetime_utc_string( wcs_get_objects_property( $order, 'date_created' ) );
$period = WC_Subscriptions_Product::get_period( $variation_id );
$interval = WC_Subscriptions_Product::get_interval( $variation_id );
$sub = wcs_create_subscription( array( 'order_id' => $order_id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date )) ;
$sub->add_product( wc_get_product( $product_id ), $quantity, $price_params );
$sub->calculate_totals();
WC_Subscriptions_Manager::activate_subscriptions_for_order($order);