Completed
Push — master ( 365d35...b33229 )
by Sébastien
02:55 queued 48s
created

WCSATT_STT_Cart::update_cart_item_sub_data()   D

Complexity

Conditions 9
Paths 65

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 43
rs 4.909
cc 9
eloc 21
nc 65
nop 1
1
<?php
2
/**
3
 * Cart functionality for converting cart items to subscriptions.
4
 *
5
 * @class   WCSATT_STT_Cart
6
 * @version 1.0.0
7
 */
8
9
class WCSATT_STT_Cart extends WCS_ATT_Cart {
10
11
	/**
12
	 * Initialize the cart.
13
	 *
14
	 * @access public
15
	 * @static
16
	 */
17
	public static function init() {
18
		// Overrides the price of the subscription for sign up fee and/or trial on the 'wcsatt_cart_item' filter.
19
		add_filter( 'wcsatt_cart_item', __CLASS__ . '::update_cart_item_sub_data', 10, 1 );
20
	}
21
22
	/**
23
	 * Updates the cart item data for a subscription product that
24
	 * has a sign-up fee and/or trial period applied.
25
	 *
26
	 * @access public
27
	 * @static
28
	 * @param  array $cart_item
29
	 * @return array
30
	 */
31
	public static function update_cart_item_sub_data( $cart_item ) {
32
		$active_scheme = WCS_ATT_Schemes::get_active_subscription_scheme( $cart_item );
33
34
		$subscription_prices = WCS_ATT_Scheme_Prices::get_active_subscription_scheme_prices( $cart_item, $active_scheme );
35
36
		if ( $active_scheme && $cart_item['data']->is_converted_to_sub == 'yes' ) {
37
38
			// Subscription Price
39
			$price = $cart_item['data']->subscription_price;
40
41
			// Is there a sign up fee?
42
			$sign_up_fee = isset( $subscription_prices['sign_up_fee'] ) ? $subscription_prices['sign_up_fee'] : '';
43
44
			// Checks if the cart item is a supported bundle type child.
45
			$container_key = WCS_ATT_Integrations::has_bundle_type_container( $cart_item );
46
47
			// If the cart item is a child item then reset the sign-up fee.
48
			if ( false !== $container_key ) { $sign_up_fee = ''; }
49
50
			// Put both the subscription price and the sign-up fee together.
51
			$new_price = $price + $sign_up_fee;
52
53
			if ( $sign_up_fee > 0 ) {
54
				$cart_item['data']->initial_amount = $new_price;
55
				$cart_item['data']->subscription_sign_up_fee = $sign_up_fee;
56
			}
57
58
			$trial_length = isset( $subscription_prices['trial_length'] ) ? $subscription_prices['trial_length'] : 0;
59
			$trial_period = isset( $subscription_prices['trial_period'] ) ? $subscription_prices['trial_period'] : '';
60
61
			// If a trial length is more than zero then set the conditions for the cart.
62
			if ( $trial_length > 0 ) {
63
				$cart_item['data']->subscription_trial_length = $trial_length;
64
				$cart_item['data']->subscription_trial_period = $trial_period;
65
			} else {
66
				$cart_item['data']->subscription_trial_length = 0;
67
				$cart_item['data']->subscription_trial_period = '';
68
			}
69
70
		}
71
72
		return $cart_item;
73
	} // END update_cart_item_sub_data()
74
75
}
76
77
WCSATT_STT_Cart::init();
78