Completed
Branch issue-6 (9081e8)
by Sébastien
14:48
created

WCSATT_STT_Cart::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
			// Put both the subscription price and the sign-up fee together.
45
			$new_price = $price + $sign_up_fee;
46
47
			if ( $sign_up_fee > 0 ) {
48
				$cart_item['data']->initial_amount = $new_price;
49
				$cart_item['data']->subscription_sign_up_fee = $sign_up_fee;
50
			}
51
52
			$trial_length = isset( $subscription_prices['trial_length'] ) ? $subscription_prices['trial_length'] : 0;
53
			$trial_period = isset( $subscription_prices['trial_period'] ) ? $subscription_prices['trial_period'] : '';
54
55
			// If a trial length is more than zero then set the conditions for the cart.
56
			if ( $trial_length > 0 ) {
57
				$cart_item['data']->subscription_trial_length = $trial_length;
58
				$cart_item['data']->subscription_trial_period = $trial_period;
59
			} else {
60
				$cart_item['data']->subscription_trial_length = 0;
61
				$cart_item['data']->subscription_trial_period = '';
62
			}
63
64
		}
65
66
		return $cart_item;
67
	} // END update_cart_item_sub_data()
68
69
}
70
71
WCSATT_STT_Cart::init();
72