PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Frontend / Checkout.php

Checkout.php in Subscriptions for WooCommerce with Stripe Recurring Payments 2.0.0, at includes/Frontend/Checkout.php

221 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Checkout handlers for subscription plugin.
4 *
5 * @package wp_subscription
6 */
7
8 namespace SpringDevs\Subscription\Frontend;
9
10 use SpringDevs\Subscription\Illuminate\Helper;
11 use SpringDevs\Subscription\Illuminate\Subscription\Subscription;
12
13 /**
14 * Checkout class
15 */
16 class Checkout {
17
18 /**
19 * Initialize the class
20 */
21 public function __construct() {
22 // Subscription upgrade/downgrade order created hook.
23 add_action( 'woocommerce_checkout_order_processed', [ $this, 'trigger_subscription_switch_order_created' ] );
24 add_action( 'woocommerce_store_api_checkout_order_processed', [ $this, 'trigger_subscription_switch_order_created_storeapi' ] );
25
26 add_action( 'woocommerce_checkout_order_processed', [ $this, 'create_subscription_after_checkout' ] );
27 add_action( 'woocommerce_store_api_checkout_order_processed', [ $this, 'create_subscription_after_checkout_storeapi' ] );
28 add_action( 'woocommerce_resume_order', [ $this, 'remove_subscriptions' ] );
29 add_action( 'woocommerce_checkout_create_order_line_item', [ $this, 'save_order_item_product_meta' ], 10, 3 );
30 }
31
32 /**
33 * Create subscription during checkout on storeAPI.
34 *
35 * @param \WC_Order $order Order Object.
36 */
37 public function create_subscription_after_checkout_storeapi( $order ) {
38 $this->create_subscription_after_checkout( $order->get_id() );
39 }
40
41 /**
42 * Create subscription during checkout.
43 *
44 * @param int $order_id Order ID.
45 */
46 public function create_subscription_after_checkout( $order_id ) {
47 $order = wc_get_order( $order_id );
48
49 // Grab the post status based on order status.
50 $post_status = 'active';
51 switch ( $order->get_status() ) {
52 case 'on-hold':
53 case 'pending':
54 $post_status = 'pending';
55 break;
56
57 case 'failed':
58 case 'cancelled':
59 $post_status = 'cancelled';
60 break;
61
62 default:
63 break;
64 }
65
66 // Create subscription for order items.
67 $order_items = $order->get_items();
68 foreach ( $order_items as $order_item ) {
69 $product = Subscription::get_subs_product( $order_item['product_id'] );
70
71 // Plan items are created by Frontend\PlanCheckout on `subscrpt_product_checkout`
72 // below (resolution order: tied plan first, else classic meta). Skipping
73 // them here keeps the classic path from creating a second subscription.
74 if ( $product->is_type( 'simple' ) && ! subscrpt_pro_activated() && ! $order_item->get_meta( '_subscrpt_plan_id' ) ) {
75 // A plan product bought as One-Time carries no plan id — it is not a
76 // subscription, so never record one for it.
77 $is_one_time = function_exists( 'subscrpt_product_has_plan' ) && subscrpt_product_has_plan( $product->get_id() );
78
79 if ( $product->is_enabled() && ! $is_one_time ) {
80 $is_renew = isset( $order_item['renew_subscrpt'] );
81
82 $timing_option = $product->get_timing_option();
83 $trial = $product->get_trial();
84
85 wc_update_order_item_meta(
86 $order_item->get_id(),
87 '_subscrpt_meta',
88 array(
89 'time' => 1,
90 'type' => $timing_option,
91 'trial' => $trial,
92 )
93 );
94
95 // Renew subscription if need!
96 $renew_subscription_id = Helper::subscription_exists( $product->get_id(), 'expired' );
97 $selected_subscription_id = null;
98 if ( $is_renew && $renew_subscription_id && 'cancelled' !== $post_status ) {
99 $selected_subscription_id = $renew_subscription_id;
100 Helper::process_order_renewal( $selected_subscription_id, $order_id, $order_item->get_id() );
101 } else {
102 $selected_subscription_id = Helper::process_new_subscription_order( $order_item, $post_status, $product );
103 }
104
105 if ( $selected_subscription_id ) {
106 // product related.
107 update_post_meta( $selected_subscription_id, '_subscrpt_timing_option', $timing_option );
108 update_post_meta( $selected_subscription_id, '_subscrpt_price', $product->get_price() );
109 update_post_meta( $selected_subscription_id, '_subscrpt_user_cancel', $product->get_meta( '_subscrpt_user_cancel' ) );
110
111 // order related.
112 update_post_meta( $selected_subscription_id, '_subscrpt_order_id', $order_id );
113 update_post_meta( $selected_subscription_id, '_subscrpt_order_item_id', $order_item->get_id() );
114
115 // subscription related.
116 update_post_meta( $selected_subscription_id, '_subscrpt_trial', $trial );
117
118 do_action( 'subscrpt_order_checkout', $selected_subscription_id, $order_item );
119 }
120 }
121 }
122
123 do_action( 'subscrpt_product_checkout', $order_item, $product, $post_status );
124 }
125 }
126
127 /**
128 * Remove subscriptions for resumed orders.
129 *
130 * @param int $order_id Order id.
131 *
132 * @return void
133 */
134 public function remove_subscriptions( $order_id ) {
135 global $wpdb;
136 // delete subscriptions & order item meta.
137 $histories = Helper::get_subscriptions_from_order( $order_id );
138 foreach ( $histories as $history ) {
139 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
140 // @phpcs:ignore
141 $relation_count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE subscription_id=%d', array( $table_name, $history->subscription_id ) ) );
142 if ( 1 === (int) $relation_count ) {
143 wp_delete_post( $history->subscription_id, true );
144 }
145 wc_delete_order_item_meta( $history->order_item_id, '_subscrpt_meta' );
146 }
147
148 // delete order subscription relation.
149 global $wpdb;
150 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
151 // phpcs:ignore
152 $wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) );
153 }
154
155 /**
156 * Save renew meta
157 *
158 * @param object $item Item.
159 * @param string $cart_item_key Cart Item Key.
160 * @param array $cart_item Cart Item.
161 */
162 public function save_order_item_product_meta( $item, $cart_item_key, $cart_item ) {
163 if ( isset( $cart_item['renew_subscrpt'] ) ) {
164 $item->update_meta_data( '_renew_subscrpt', $cart_item['renew_subscrpt'] );
165 }
166
167 if ( ! empty( $cart_item['wp_subs_switch'] ?? null ) && ! empty( $cart_item['switch_context'] ?? null ) ) {
168 $switch_context = $cart_item['switch_context'];
169
170 // Add switch context data to order item meta.
171 $item->update_meta_data( '_wp_subs_switch', true, true );
172 $item->update_meta_data( '_wp_subs_switch_context', $switch_context, true );
173 }
174 }
175
176 /**
177 * Trigger subscription switch order created store API.
178 *
179 * @param \WC_Order $order Order.
180 */
181 public function trigger_subscription_switch_order_created_storeapi( $order ) {
182 $this->trigger_subscription_switch_order_created( $order->get_id() ?? 0 );
183 }
184
185 /**
186 * Trigger subscription switch order created.
187 *
188 * @param int $order_id Order ID.
189 */
190 public function trigger_subscription_switch_order_created( $order_id ) {
191 if ( ! $order_id || ! subscrpt_pro_activated() ) {
192 return;
193 }
194
195 $order = wc_get_order( $order_id );
196 $order_items = $order->get_items();
197
198 foreach ( $order_items as $order_item ) {
199 $is_switch = in_array( $order_item->get_meta( '_wp_subs_switch' ), [ true, 1, '1' ], true );
200 $switch_context = $order_item->get_meta( '_wp_subs_switch_context' );
201
202 if ( $is_switch ) {
203 $switch_type = $switch_context['switch_type'] ?? 'upgrade';
204
205 unset( $switch_context['nonce'] );
206 unset( $switch_context['redirect_back_url'] );
207
208 /**
209 * Action fired when subscription switch order is created.
210 *
211 * @param string $switch_type Switch type (upgrade/downgrade).
212 * @param \WC_Order $order Order object.
213 * @param \WC_Order_Item_Product $order_item Order Item object.
214 * @param array $switch_context Switch context data. [ 'switch_type', 'subscription_id', 'order_id', 'product_id', 'old_variation_id', 'new_variation_id' ]
215 */
216 do_action( 'subscrpt_switch_order_created', $switch_type, $order, $order_item, $switch_context );
217 }
218 }
219 }
220 }
221