-
Notifications
You must be signed in to change notification settings - Fork 69
/
WooCommerceSubscriptions.php
378 lines (329 loc) · 11.9 KB
/
WooCommerceSubscriptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* Class WooCommerceSubscriptions
*
* @package WCPay\MultiCurrency\Compatibility
*/
namespace WCPay\MultiCurrency\Compatibility;
use WC_Payments_Features;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;
/**
* Class that controls Multi Currency Compatibility with WooCommerce Subscriptions Plugin and WCPay Subscriptions.
*/
class WooCommerceSubscriptions extends BaseCompatibility {
/**
* Subscription switch cart item.
*
* @var string
*/
public $switch_cart_item = '';
/**
* Init the class.
*
* @return void
*/
protected function init() {
// Add needed actions and filters if WC Subscriptions or WCPay Subscriptions are active.
if ( class_exists( 'WC_Subscriptions' ) || WC_Payments_Features::is_wcpay_subscriptions_enabled() ) {
if ( ! is_admin() && ! defined( 'DOING_CRON' ) ) {
add_filter( 'woocommerce_subscriptions_product_price', [ $this, 'get_subscription_product_price' ], 50, 2 );
add_filter( 'woocommerce_product_get__subscription_sign_up_fee', [ $this, 'get_subscription_product_signup_fee' ], 50, 2 );
add_filter( 'woocommerce_product_variation_get__subscription_sign_up_fee', [ $this, 'get_subscription_product_signup_fee' ], 50, 2 );
add_filter( 'option_woocommerce_subscriptions_multiple_purchase', [ $this, 'maybe_disable_mixed_cart' ], 50 );
add_filter( MultiCurrency::FILTER_PREFIX . 'override_selected_currency', [ $this, 'override_selected_currency' ], 50 );
add_filter( MultiCurrency::FILTER_PREFIX . 'should_convert_product_price', [ $this, 'should_convert_product_price' ], 50, 2 );
add_filter( MultiCurrency::FILTER_PREFIX . 'should_convert_coupon_amount', [ $this, 'should_convert_coupon_amount' ], 50, 2 );
add_filter( MultiCurrency::FILTER_PREFIX . 'should_hide_widgets', [ $this, 'should_hide_widgets' ], 50 );
}
}
}
/**
* Converts subscription prices, if needed.
*
* @param mixed $price The price to be filtered.
* @param object $product The product that will have a filtered price.
*
* @return mixed The price as a string or float.
*/
public function get_subscription_product_price( $price, $product ) {
if ( ! $price || ! $this->should_convert_product_price( true, $product ) ) {
return $price;
}
return $this->multi_currency->get_price( $price, 'product' );
}
/**
* Converts subscription sign up prices, if needed.
*
* @param mixed $price The price to be filtered.
* @param object $product The product that will have a filtered price.
*
* @return mixed The price as a string or float.
*/
public function get_subscription_product_signup_fee( $price, $product ) {
if ( ! $price ) {
return $price;
}
$switch_cart_items = $this->get_subscription_switch_cart_items();
if ( 0 < count( $switch_cart_items ) ) {
// There should only ever be one item, so use that item.
$item = array_shift( $switch_cart_items );
$item_id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$switch_cart_item = $this->switch_cart_item;
$this->switch_cart_item = $item['key'];
if ( $product->get_id() === $item_id ) {
/**
* These tests get mildly complex due to, when switching, the sign up fee is queried
* several times to determine prorated costs. This means we have to test to see when
* the fee actually needs be converted.
*/
if ( $this->utils->is_call_in_backtrace( [ 'WC_Subscriptions_Cart::set_subscription_prices_for_calculation' ] ) ) {
return $price;
}
// Check to see if it's currently determining prorated prices.
if ( $this->utils->is_call_in_backtrace( [ 'WC_Subscriptions_Product::get_sign_up_fee' ] )
&& $this->utils->is_call_in_backtrace( [ 'WC_Cart->calculate_totals' ] )
&& $item['key'] === $switch_cart_item
&& ! $this->utils->is_call_in_backtrace( [ 'WCS_Switch_Totals_Calculator->apportion_sign_up_fees' ] ) ) {
return $price;
}
// Check to see if the _subscription_sign_up_fee meta for the product has already been updated.
if ( $item['key'] === $switch_cart_item ) {
foreach ( $product->get_meta_data() as $meta ) {
if ( '_subscription_sign_up_fee' === $meta->get_data()['key'] && 0 < count( $meta->get_changes() ) ) {
return $price;
}
}
}
}
}
return $this->multi_currency->get_price( $price, 'product' );
}
/**
* Disables the mixed cart if needed.
*
* @param string|bool $value Option from the database, or false.
*
* @return mixed False, yes, or no.
*/
public function maybe_disable_mixed_cart( $value ) {
// If there's a subscription switch in the cart, disable multiple items in the cart.
// This is so that subscriptions with different currencies cannot be added to the cart.
if ( 0 < count( $this->get_subscription_switch_cart_items() ) ) {
return 'no';
}
return $value;
}
/**
* Checks to see if the if the selected currency needs to be overridden.
*
* @param mixed $return Default is false, but could be three letter currency code.
*
* @return mixed Three letter currency code or false if not.
*/
public function override_selected_currency( $return ) {
// If it's not false, return it.
if ( $return ) {
return $return;
}
$subscription_renewal = $this->cart_contains_renewal();
if ( $subscription_renewal ) {
return $subscription_renewal['subscription_renewal']['renewal_order_id']->get_currency();
}
$switch_id = $this->get_subscription_switch_id_from_superglobal();
if ( $switch_id ) {
return get_post_meta( $switch_id, '_order_currency', true );
}
$switch_cart_items = $this->get_subscription_switch_cart_items();
if ( 0 < count( $switch_cart_items ) ) {
$switch_cart_item = array_shift( $switch_cart_items );
return $switch_cart_item['subscription_switch']['subscription_id']->get_currency();
}
$subscription_resubscribe = $this->cart_contains_resubscribe();
if ( $subscription_resubscribe ) {
return $subscription_resubscribe['subscription_resubscribe']['subscription_id']->get_currency();
}
return $return;
}
/**
* Checks to see if the product's price should be converted.
*
* @param bool $return Whether to convert the product's price or not. Default is true.
* @param object $product Product object to test.
*
* @return bool True if it should be converted.
*/
public function should_convert_product_price( bool $return, $product ): bool {
// If it's already false, return it.
if ( ! $return ) {
return $return;
}
// Check for subscription renewal or resubscribe.
if ( $this->is_product_subscription_type_in_cart( $product, 'renewal' )
|| $this->is_product_subscription_type_in_cart( $product, 'resubscribe' ) ) {
$calls = [
'WC_Cart_Totals->calculate_item_totals',
'WC_Cart->get_product_subtotal',
'wc_get_price_excluding_tax',
'wc_get_price_including_tax',
];
if ( $this->utils->is_call_in_backtrace( $calls ) ) {
return false;
}
}
// WCPay Subs does a check against the product price and the total, we need to return the actual product price for this check.
if ( $this->utils->is_call_in_backtrace( [ 'WC_Payments_Subscription_Service->get_recurring_item_data_for_subscription' ] )
&& $this->utils->is_call_in_backtrace( [ 'WC_Product->get_price' ] ) ) {
return false;
}
return $return;
}
/**
* Checks to see if the coupon's amount should be converted.
*
* @param bool $return Whether to convert the coupon's price or not. Default is true.
* @param object $coupon Coupon object to test.
*
* @return bool True if it should be converted.
*/
public function should_convert_coupon_amount( bool $return, $coupon ): bool {
// If it's already false, return it.
if ( ! $return ) {
return $return;
}
// We do not need to convert percentage coupons.
if ( $this->is_coupon_type( $coupon, 'subscription_percent' ) ) {
return false;
}
// If there's not a renewal in the cart, we can convert.
$subscription_renewal = $this->cart_contains_renewal();
if ( ! $subscription_renewal ) {
return true;
}
/**
* We need to allow the early renewal to convert the cost, as it pulls the original value of the coupon.
* Subsequent queries for the amount use the first converted amount.
* This also works for normal manual renewals.
*/
if ( ! $this->utils->is_call_in_backtrace( [ 'WCS_Cart_Early_Renewal->setup_cart' ] )
&& $this->utils->is_call_in_backtrace( [ 'WC_Discounts->apply_coupon' ] )
&& $this->is_coupon_type( $coupon, 'subscription_recurring' ) ) {
return false;
}
return $return;
}
/**
* Checks to see if the widgets should be hidden.
*
* @param bool $return Whether widgets should be hidden or not. Default is false.
*
* @return bool
*/
public function should_hide_widgets( bool $return ): bool {
// If it's already true, return it.
if ( $return ) {
return $return;
}
if ( $this->cart_contains_renewal()
|| $this->get_subscription_switch_id_from_superglobal()
|| 0 < count( $this->get_subscription_switch_cart_items() )
|| $this->cart_contains_resubscribe() ) {
return true;
}
return $return;
}
/**
* Checks the cart to see if it contains a subscription product renewal.
*
* @return mixed The cart item containing the renewal as an array, else false.
*/
private function cart_contains_renewal() {
if ( ! function_exists( 'wcs_cart_contains_renewal' ) ) {
return false;
}
return wcs_cart_contains_renewal();
}
/**
* Gets the subscription switch items out of the cart.
*
* @return array Empty array or the cart items in an array..
*/
private function get_subscription_switch_cart_items(): array {
if ( ! function_exists( 'wcs_get_order_type_cart_items' ) ) {
return [];
}
return wcs_get_order_type_cart_items( 'switch' );
}
/**
* Checks $_GET superglobal for a switch id and returns it if found.
*
* @return mixed Id of the sub being switched, or false.
*/
private function get_subscription_switch_id_from_superglobal() {
if ( isset( $_GET['_wcsnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wcsnonce'] ), 'wcs_switch_request' ) ) {
if ( isset( $_GET['switch-subscription'] ) ) {
return (int) $_GET['switch-subscription'];
}
}
return false;
}
/**
* Checks the cart to see if it contains a resubscription.
*
* @return mixed The cart item containing the resubscription as an array, else false.
*/
private function cart_contains_resubscribe() {
if ( ! function_exists( 'wcs_cart_contains_resubscribe' ) ) {
return false;
}
return wcs_cart_contains_resubscribe();
}
/**
* Checks to see if the product passed is in the cart as a subscription type.
*
* @param object $product Product to test.
* @param string $type Type of subscription.
*
* @return bool True if found in the cart, false if not.
*/
private function is_product_subscription_type_in_cart( $product, $type ): bool {
$subscription = false;
switch ( $type ) {
case 'renewal':
$subscription = $this->cart_contains_renewal();
break;
case 'resubscribe':
$subscription = $this->cart_contains_resubscribe();
break;
}
if ( $subscription && $product ) {
if ( ( isset( $subscription['variation_id'] ) && $subscription['variation_id'] === $product->get_id() )
|| $subscription['product_id'] === $product->get_id() ) {
return true;
}
}
return false;
}
/**
* Checks to see if the coupon passed is of a specified type.
*
* @param \WC_Coupon $coupon Coupon to test.
* @param string $type Type of coupon to test for.
*
* @return bool True on match.
*/
private function is_coupon_type( $coupon, string $type ) {
switch ( $type ) {
case 'subscription_percent':
$types = [ 'recurring_percent', 'sign_up_fee_percent', 'renewal_percent' ];
break;
case 'subscription_recurring':
$types = [ 'recurring_fee', 'recurring_percent', 'renewal_fee', 'renewal_percent', 'renewal_cart' ];
break;
}
if ( in_array( $coupon->get_discount_type(), $types, true ) ) {
return true;
}
return false;
}
}