Completed
Pull Request — master (#11889)
by Mike
12:12
created

WC_Cart_Session::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
include_once( WC_ABSPATH . 'includes/legacy/class-wc-legacy-cart.php' );
7
8
/**
9
 * Cart session class.
10
 * @version		2.7.0
11
 * @package		WooCommerce/Classes
12
 * @category	Class
13
 * @author 		WooThemes
14
 */
15
abstract class WC_Cart_Session extends WC_Legacy_Cart {
16
17
	/**
18
	 * Cart items class.
19
	 * @var WC_Cart_Items
20
	 */
21
	protected $items;
22
23
	/**
24
	 * Cart coupons class.
25
	 * @var WC_Cart_Coupons
26
	 */
27
	protected $coupons;
28
29
	/**
30
	 * Cart fees class.
31
	 * @var WC_Cart_Fees
32
	 */
33
	public $fees;
34
35
	/**
36
	 * Cart totals class.
37
	 * @var WC_Cart_Totals
38
	 */
39
	protected $totals;
40
41
	/**
42
	 * Constructor.
43
	 */
44
	public function __construct() {
45
		$this->items   = new WC_Cart_Items;
46
		$this->coupons = new WC_Cart_Coupons;
47
		$this->fees    = new WC_Cart_Fees;
48
		$this->totals  = new WC_Cart_Totals;
49
		add_action( 'wp_loaded', array( $this, 'get_cart_from_session' ) );
50
		add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 );
51
		add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 );
52
		add_action( 'woocommerce_add_to_cart', array( $this, 'maybe_set_cart_cookies' ) );
53
		add_action( 'woocommerce_cart_emptied', array( $this, 'destroy_cart_session' ) );
54
		add_action( 'woocommerce_after_calculate_totals', array( $this, 'set_session' ) );
55
		add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'set_session' ) );
56
		add_action( 'woocommerce_removed_coupon', array( $this, 'set_session' ) );
57
	}
58
59
	/**
60
	 * Destroy cart session data.
61
	 */
62
	public function destroy_cart_session( $deprecated = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $deprecated is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
		WC()->session->set( 'cart', null );
64
	}
65
66
	/**
67
	 * Will set cart cookies if needed, once, during WP hook.
68
	 */
69
	public function maybe_set_cart_cookies() {
70
		if ( headers_sent() || ! did_action( 'wp_loaded' ) ) {
71
			return;
72
		}
73
		if ( ! $this->is_empty() ) {
74
			$this->set_cart_cookies( true );
75
		} elseif ( isset( $_COOKIE['woocommerce_items_in_cart'] ) ) {
76
			$this->set_cart_cookies( false );
77
		}
78
	}
79
80
	/**
81
	 * Set cart hash cookie and items in cart.
82
	 *
83
	 * @access private
84
	 * @param bool $set (default: true)
85
	 */
86
	private function set_cart_cookies( $set = true ) {
87
		if ( $set ) {
88
			wc_setcookie( 'woocommerce_items_in_cart', 1 );
89
			wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( $this->get_cart_for_session() ) ) );
90
		} else {
91
			wc_setcookie( 'woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS );
92
			wc_setcookie( 'woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS );
93
		}
94
		do_action( 'woocommerce_set_cart_cookies', $set );
95
	}
96
97
	/**
98
	 * Returns the contents of the cart in an array without the 'data' element.
99
	 *
100
	 * @return array contents of the cart
101
	 */
102
	public function get_cart_for_session() {
103
		return wc_list_pluck( $this->items->get_items(), 'get_data' );
104
	}
105
106
	/**
107
	 * Get the cart data from the PHP session and store it in class variables.
108
	 */
109
	public function get_cart_from_session() {
110
		$cart = wp_parse_args( (array) WC()->session->get( 'cart', array() ), array(
111
			'items'         => array(),
112
			'removed_items' => array(),
113
			'coupons'       => array(),
114
			'totals'        => null,
115
		) );
116
117
		foreach ( $cart['items'] as $key => $values ) {
118
			if ( ! isset( $values['product_id'], $values['quantity'] ) || ! ( $product = wc_get_product( $values['product_id'] ) ) ) {
119
				unset( $cart['items'][ $key ] );
120
				continue;
121
			}
122
			// Put session data into array. Run through filter so other plugins can load their own session data.
123
			$cart['items'][ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $values, $values, $key );
124
		}
125
126
		$this->items->set_items( $cart['items'] );
127
		$this->items->set_removed_items( $cart['removed_items'] );
128
		$this->coupons->set_coupons( $cart['coupons'] );
129
		$this->totals->set_totals( (array) $cart['totals'] );
130
131
		do_action( 'woocommerce_cart_loaded_from_session', $this );
132
	}
133
134
	/**
135
	 * Sets the php session data for the cart and coupons.
136
	 */
137
	public function set_session() {
138
		$session_data = array(
139
			'items'         => $this->get_cart_for_session(),
140
			'removed_items' => wc_list_pluck( $this->items->get_removed_items(), 'get_data' ),
141
			'coupons'       => $this->coupons->get_coupons(),
142
			'totals'        => $this->totals->get_totals(),
143
		);
144
		if ( WC()->session->set( 'cart', $session_data ) ) {
145
			do_action( 'woocommerce_cart_updated' );
146
		}
147
	}
148
}
149