| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Collection of Cart Items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @version		2.7.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package		WooCommerce/Classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @category	Class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author 		WooThemes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class WC_Cart_Items { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * An array of items in the cart. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @var WC_Item_Product[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	private $items = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * An array of items removed from the cart which can be restored. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @var WC_Item_Product[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	private $removed_items = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		add_action( 'woocommerce_check_cart_items', array( $this, 'check_items' ), 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * Generate a unique ID for the cart item being added. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @param array $item Cart item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @return string cart item key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function generate_key( $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		unset( $item['quantity'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		return md5( json_encode( $item ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * Get an item by it's key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param  string $item_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return WC_Item_Product|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	public function get_item_by_key( $item_key ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		return isset( $this->items[ $item_key ] ) ? $this->items[ $item_key ] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * Get items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	public function get_items() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		return $this->items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * Get removed items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	public function get_removed_items() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return $this->removed_items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * Set items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 76 |  | View Code Duplication | 	public function set_items( $items ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$this->items = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$items       = array_filter( (array) $items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		foreach ( $items as $key => $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			if ( ! is_a( $item, 'WC_Item_Product' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				$item = new WC_Item_Product( $item ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			$this->items[ $key ] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * Set items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 92 |  | View Code Duplication | 	public function set_removed_items( $items ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$this->removed_items = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$items               = array_filter( (array) $items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		foreach ( $items as $key => $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			if ( ! is_a( $item, 'WC_Item_Product' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				$item = new WC_Item_Product( $item ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$this->removed_items[ $key ] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * Remove a cart item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @param  string $cart_item_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 109 |  | View Code Duplication | 	public function remove_item( $cart_item_key ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		if ( isset( $this->items[ $cart_item_key ] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			do_action( 'woocommerce_remove_cart_item', $cart_item_key, WC()->cart ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			$this->removed_items[ $cart_item_key ] = $this->items[ $cart_item_key ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			unset( $this->items[ $cart_item_key ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			do_action( 'woocommerce_cart_item_removed', $cart_item_key, WC()->cart ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * Restore a cart item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @param  string $cart_item_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 127 |  | View Code Duplication | 	public function restore_item( $cart_item_key ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		if ( isset( $this->removed_items[ $cart_item_key ] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			do_action( 'woocommerce_restore_cart_item', $cart_item_key, WC()->cart ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			$this->items[ $cart_item_key ] = new WC_Item_Product( $this->removed_items[ $cart_item_key ] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			unset( $this->removed_items[ $cart_item_key ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			do_action( 'woocommerce_cart_item_restored', $cart_item_key, WC()->cart ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * Filter items needing shipping callback. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	protected function filter_items_needing_shipping( $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		$product = $item->get_product(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return $product && $product->needs_shipping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * Get only items that need shipping. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	public function get_items_needing_shipping() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		return array_filter( $this->get_items(), array( $this, 'filter_items_needing_shipping' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Check all cart items for errors. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  | 	public function check_items() { | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  | 			foreach ( $this->get_items() as $cart_item_key => $item ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  | 				$product = $item->get_product(); | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  | 				if ( ! $product || ! $product->is_purchasable() ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  | 					unset( $this->items[ $cart_item_key ] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | 					wc_add_notice( __( 'An item which is no longer available was removed from your cart.', 'woocommerce' ), 'error' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  | 					continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  | 				} | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  | 				wc_add_to_cart_validate_stock( $product ); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  | 		} catch ( Exception $e ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  | 			wc_add_notice( $e->getMessage(), 'error' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * Get number of items in the cart. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	public function get_quantity() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		return apply_filters( 'woocommerce_cart_contents_count', array_sum( wc_list_pluck( $this->items, 'get_quantity' ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 * Get all tax classes for items in the cart. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	public function get_tax_classes() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		return array_unique( array_values( wc_list_pluck( $this->items, 'get_tax_class' ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	 * Get weight of items in the cart. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	public function get_weight() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 		return apply_filters( 'woocommerce_cart_contents_weight', array_sum( wc_list_pluck( $this->items, 'get_weight' ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 201 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.