class-wc-legacy-cart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * Legacy cart
  4. *
  5. * Legacy and deprecated functions are here to keep the WC_Cart class clean.
  6. * This class will be removed in future versions.
  7. *
  8. * @version 3.2.0
  9. * @package WooCommerce/Classes
  10. * @category Class
  11. * @author Automattic
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit;
  15. }
  16. /**
  17. * Legacy cart class.
  18. */
  19. abstract class WC_Legacy_Cart {
  20. /**
  21. * Array of defaults. Not used since 3.2.
  22. *
  23. * @deprecated 3.2.0
  24. */
  25. public $cart_session_data = array(
  26. 'cart_contents_total' => 0,
  27. 'total' => 0,
  28. 'subtotal' => 0,
  29. 'subtotal_ex_tax' => 0,
  30. 'tax_total' => 0,
  31. 'taxes' => array(),
  32. 'shipping_taxes' => array(),
  33. 'discount_cart' => 0,
  34. 'discount_cart_tax' => 0,
  35. 'shipping_total' => 0,
  36. 'shipping_tax_total' => 0,
  37. 'coupon_discount_amounts' => array(),
  38. 'coupon_discount_tax_amounts' => array(),
  39. 'fee_total' => 0,
  40. 'fees' => array(),
  41. );
  42. /**
  43. * Contains an array of coupon usage counts after they have been applied.
  44. *
  45. * @deprecated 3.2.0
  46. * @var array
  47. */
  48. public $coupon_applied_count = array();
  49. /**
  50. * Map legacy variables.
  51. *
  52. * @param string $name Property name.
  53. * @param mixed $value Value to set.
  54. */
  55. public function __isset( $name ) {
  56. if ( array_key_exists( $name, $this->cart_session_data ) || 'fees' === $name ) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Magic getters.
  63. *
  64. * @param string $name Property name.
  65. * @return mixed
  66. */
  67. public function &__get( $name ) {
  68. $value = '';
  69. switch ( $name ) {
  70. case 'dp' :
  71. $value = wc_get_price_decimals();
  72. break;
  73. case 'prices_include_tax' :
  74. $value = wc_prices_include_tax();
  75. break;
  76. case 'round_at_subtotal' :
  77. $value = 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
  78. break;
  79. case 'cart_contents_total' :
  80. $value = $this->get_cart_contents_total();
  81. break;
  82. case 'total' :
  83. $value = $this->get_total( 'edit' );
  84. break;
  85. case 'subtotal' :
  86. $value = $this->get_subtotal() + $this->get_subtotal_tax();
  87. break;
  88. case 'subtotal_ex_tax' :
  89. $value = $this->get_subtotal();
  90. break;
  91. case 'tax_total' :
  92. $value = $this->get_fee_tax() + $this->get_cart_contents_tax();
  93. break;
  94. case 'fee_total' :
  95. $value = $this->get_fee_total();
  96. break;
  97. case 'discount_cart' :
  98. $value = $this->get_discount_total();
  99. break;
  100. case 'discount_cart_tax' :
  101. $value = $this->get_discount_tax();
  102. break;
  103. case 'shipping_total' :
  104. $value = $this->get_shipping_total();
  105. break;
  106. case 'shipping_tax_total' :
  107. $value = $this->get_shipping_tax();
  108. break;
  109. case 'display_totals_ex_tax' :
  110. case 'display_cart_ex_tax' :
  111. $value = ! $this->display_prices_including_tax();
  112. break;
  113. case 'cart_contents_weight' :
  114. $value = $this->get_cart_contents_weight();
  115. break;
  116. case 'cart_contents_count' :
  117. $value = $this->get_cart_contents_count();
  118. break;
  119. case 'coupons' :
  120. $value = $this->get_coupons();
  121. break;
  122. // Arrays returned by reference to allow modification without notices. TODO: Remove in 4.0.
  123. case 'taxes' :
  124. wc_deprecated_function( 'WC_Cart->taxes', '3.2', sprintf( 'getters (%s) and setters (%s)', 'WC_Cart::get_cart_contents_taxes()', 'WC_Cart::set_cart_contents_taxes()' ) );
  125. $value = &$this->totals[ 'cart_contents_taxes' ];
  126. break;
  127. case 'shipping_taxes' :
  128. wc_deprecated_function( 'WC_Cart->shipping_taxes', '3.2', sprintf( 'getters (%s) and setters (%s)', 'WC_Cart::get_shipping_taxes()', 'WC_Cart::set_shipping_taxes()' ) );
  129. $value = &$this->totals[ 'shipping_taxes' ];
  130. break;
  131. case 'coupon_discount_amounts' :
  132. $value = &$this->coupon_discount_totals;
  133. break;
  134. case 'coupon_discount_tax_amounts' :
  135. $value = &$this->coupon_discount_tax_totals;
  136. break;
  137. case 'fees' :
  138. wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::get_fees' ) );
  139. // Grab fees from the new API.
  140. $new_fees = $this->fees_api()->get_fees();
  141. // Add new fees to the legacy prop so it can be adjusted via legacy property.
  142. $this->fees = $new_fees;
  143. // Return by reference.
  144. $value = &$this->fees;
  145. break;
  146. // Deprecated args. TODO: Remove in 4.0.
  147. case 'tax' :
  148. wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax directly' );
  149. $this->tax = new WC_Tax();
  150. $value = $this->tax;
  151. break;
  152. case 'discount_total':
  153. wc_deprecated_argument( 'WC_Cart->discount_total', '2.3', 'After tax coupons are no longer supported. For more information see: https://woocommerce.wordpress.com/2014/12/upcoming-coupon-changes-in-woocommerce-2-3/' );
  154. $value = 0;
  155. break;
  156. }
  157. return $value;
  158. }
  159. /**
  160. * Map legacy variables to setters.
  161. *
  162. * @param string $name Property name.
  163. * @param mixed $value Value to set.
  164. */
  165. public function __set( $name, $value ) {
  166. switch ( $name ) {
  167. case 'cart_contents_total' :
  168. $this->set_cart_contents_total( $value );
  169. break;
  170. case 'total' :
  171. $this->set_total( $value );
  172. break;
  173. case 'subtotal' :
  174. $this->set_subtotal( $value );
  175. break;
  176. case 'subtotal_ex_tax' :
  177. $this->set_subtotal( $value );
  178. break;
  179. case 'tax_total' :
  180. $this->set_cart_contents_tax( $value );
  181. $this->set_fee_tax( 0 );
  182. break;
  183. case 'taxes' :
  184. $this->set_cart_contents_taxes( $value );
  185. break;
  186. case 'shipping_taxes' :
  187. $this->set_shipping_taxes( $value );
  188. break;
  189. case 'fee_total' :
  190. $this->set_fee_total( $value );
  191. break;
  192. case 'discount_cart' :
  193. $this->set_discount_total( $value );
  194. break;
  195. case 'discount_cart_tax' :
  196. $this->set_discount_tax( $value );
  197. break;
  198. case 'shipping_total' :
  199. $this->set_shipping_total( $value );
  200. break;
  201. case 'shipping_tax_total' :
  202. $this->set_shipping_tax( $value );
  203. break;
  204. case 'coupon_discount_amounts' :
  205. $this->set_coupon_discount_totals( $value );
  206. break;
  207. case 'coupon_discount_tax_amounts' :
  208. $this->set_coupon_discount_tax_totals( $value );
  209. break;
  210. case 'fees' :
  211. wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::add_fee' ) );
  212. $this->fees = $value;
  213. break;
  214. default :
  215. $this->$name = $value;
  216. break;
  217. }
  218. }
  219. /**
  220. * Methods moved to session class in 3.2.0.
  221. */
  222. public function get_cart_from_session() { $this->session->get_cart_from_session(); }
  223. public function maybe_set_cart_cookies() { $this->session->maybe_set_cart_cookies(); }
  224. public function set_session() { $this->session->set_session(); }
  225. public function get_cart_for_session() { return $this->session->get_cart_for_session(); }
  226. public function persistent_cart_update() { $this->session->persistent_cart_update(); }
  227. public function persistent_cart_destroy() { $this->session->persistent_cart_destroy(); }
  228. /**
  229. * Get the total of all cart discounts.
  230. *
  231. * @return float
  232. */
  233. public function get_cart_discount_total() {
  234. return $this->get_discount_total();
  235. }
  236. /**
  237. * Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
  238. *
  239. * @return float
  240. */
  241. public function get_cart_discount_tax_total() {
  242. return $this->get_discount_tax();
  243. }
  244. /**
  245. * Renamed for consistency.
  246. *
  247. * @param string $coupon_code
  248. * @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
  249. */
  250. public function add_discount( $coupon_code ) {
  251. return $this->apply_coupon( $coupon_code );
  252. }
  253. /**
  254. * Remove taxes.
  255. *
  256. * @deprecated 3.2.0 Taxes are never calculated if customer is tax except making this function unused.
  257. */
  258. public function remove_taxes() {
  259. wc_deprecated_function( 'WC_Cart::remove_taxes', '3.2', '' );
  260. }
  261. /**
  262. * Init.
  263. *
  264. * @deprecated 3.2.0 Session is loaded via hooks rather than directly.
  265. */
  266. public function init() {
  267. wc_deprecated_function( 'WC_Cart::init', '3.2', '' );
  268. $this->get_cart_from_session();
  269. }
  270. /**
  271. * Function to apply discounts to a product and get the discounted price (before tax is applied).
  272. *
  273. * @deprecated Calculation and coupon logic is handled in WC_Cart_Totals.
  274. * @param mixed $values Cart item.
  275. * @param mixed $price Price of item.
  276. * @param bool $add_totals Legacy.
  277. * @return float price
  278. */
  279. public function get_discounted_price( $values, $price, $add_totals = false ) {
  280. wc_deprecated_function( 'WC_Cart::get_discounted_price', '3.2', '' );
  281. $cart_item_key = $values['key'];
  282. $cart_item = $this->cart_contents[ $cart_item_key ];
  283. return $cart_item['line_total'];
  284. }
  285. /**
  286. * Gets the url to the cart page.
  287. *
  288. * @deprecated 2.5.0 in favor to wc_get_cart_url()
  289. * @return string url to page
  290. */
  291. public function get_cart_url() {
  292. wc_deprecated_function( 'WC_Cart::get_cart_url', '2.5', 'wc_get_cart_url' );
  293. return wc_get_cart_url();
  294. }
  295. /**
  296. * Gets the url to the checkout page.
  297. *
  298. * @deprecated 2.5.0 in favor to wc_get_checkout_url()
  299. * @return string url to page
  300. */
  301. public function get_checkout_url() {
  302. wc_deprecated_function( 'WC_Cart::get_checkout_url', '2.5', 'wc_get_checkout_url' );
  303. return wc_get_checkout_url();
  304. }
  305. /**
  306. * Sees if we need a shipping address.
  307. *
  308. * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
  309. * @return bool
  310. */
  311. public function ship_to_billing_address_only() {
  312. wc_deprecated_function( 'WC_Cart::ship_to_billing_address_only', '2.5', 'wc_ship_to_billing_address_only' );
  313. return wc_ship_to_billing_address_only();
  314. }
  315. /**
  316. * Coupons enabled function. Filterable.
  317. *
  318. * @deprecated 2.5.0 in favor to wc_coupons_enabled()
  319. * @return bool
  320. */
  321. public function coupons_enabled() {
  322. return wc_coupons_enabled();
  323. }
  324. /**
  325. * Gets the total (product) discount amount - these are applied before tax.
  326. *
  327. * @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
  328. * @return mixed formatted price or false if there are none.
  329. */
  330. public function get_discounts_before_tax() {
  331. wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' );
  332. if ( $this->get_cart_discount_total() ) {
  333. $discounts_before_tax = wc_price( $this->get_cart_discount_total() );
  334. } else {
  335. $discounts_before_tax = false;
  336. }
  337. return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this );
  338. }
  339. /**
  340. * Get the total of all order discounts (after tax discounts).
  341. *
  342. * @deprecated Order discounts (after tax) removed in 2.3.
  343. * @return int
  344. */
  345. public function get_order_discount_total() {
  346. wc_deprecated_function( 'get_order_discount_total', '2.3' );
  347. return 0;
  348. }
  349. /**
  350. * Function to apply cart discounts after tax.
  351. *
  352. * @deprecated Coupons can not be applied after tax.
  353. * @param $values
  354. * @param $price
  355. */
  356. public function apply_cart_discounts_after_tax( $values, $price ) {
  357. wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' );
  358. }
  359. /**
  360. * Function to apply product discounts after tax.
  361. *
  362. * @deprecated Coupons can not be applied after tax.
  363. *
  364. * @param $values
  365. * @param $price
  366. */
  367. public function apply_product_discounts_after_tax( $values, $price ) {
  368. wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' );
  369. }
  370. /**
  371. * Gets the order discount amount - these are applied after tax.
  372. *
  373. * @deprecated Coupons can not be applied after tax.
  374. */
  375. public function get_discounts_after_tax() {
  376. wc_deprecated_function( 'get_discounts_after_tax', '2.3' );
  377. }
  378. }