/** * Create subscriptions purchased on checkout. * * @param int $order_id The post_id of a shop_order post/WC_Order object * @param array $posted_data The data posted on checkout * @since 2.0 */ public static function process_checkout($order_id, $posted_data) { if (!WC_Subscriptions_Cart::cart_contains_subscription()) { return; } $order = new WC_Order($order_id); $subscriptions = array(); // First clear out any subscriptions created for a failed payment to give us a clean slate for creating new subscriptions $subscriptions = wcs_get_subscriptions_for_order($order->id, array('order_type' => 'parent')); if (!empty($subscriptions)) { remove_action('before_delete_post', 'WC_Subscriptions_Manager::maybe_cancel_subscription'); foreach ($subscriptions as $subscription) { wp_delete_post($subscription->id); } add_action('before_delete_post', 'WC_Subscriptions_Manager::maybe_cancel_subscription'); } WC_Subscriptions_Cart::set_global_recurring_shipping_packages(); // Create new subscriptions for each group of subscription products in the cart (that is not a renewal) foreach (WC()->cart->recurring_carts as $recurring_cart) { $subscription = self::create_subscription($order, $recurring_cart); // Exceptions are caught by WooCommerce if (is_wp_error($subscription)) { throw new Exception($subscription->get_error_message()); } do_action('woocommerce_checkout_subscription_created', $subscription, $order, $recurring_cart); } do_action('subscriptions_created_for_order', $order); // Backward compatibility }