Няма описание

class-wc-legacy-cart.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. $legacy_keys = array_merge(
  57. array(
  58. 'dp',
  59. 'prices_include_tax',
  60. 'round_at_subtotal',
  61. 'cart_contents_total',
  62. 'total',
  63. 'subtotal',
  64. 'subtotal_ex_tax',
  65. 'tax_total',
  66. 'fee_total',
  67. 'discount_cart',
  68. 'discount_cart_tax',
  69. 'shipping_total',
  70. 'shipping_tax_total',
  71. 'display_totals_ex_tax',
  72. 'display_cart_ex_tax',
  73. 'cart_contents_weight',
  74. 'cart_contents_count',
  75. 'coupons',
  76. 'taxes',
  77. 'shipping_taxes',
  78. 'coupon_discount_amounts',
  79. 'coupon_discount_tax_amounts',
  80. 'fees',
  81. 'tax',
  82. 'discount_total',
  83. 'tax_display_cart',
  84. ),
  85. is_array( $this->cart_session_data ) ? array_keys( $this->cart_session_data ) : array()
  86. );
  87. if ( in_array( $name, $legacy_keys, true ) ) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Magic getters.
  94. *
  95. * If you add/remove cases here please update $legacy_keys in __isset accordingly.
  96. *
  97. * @param string $name Property name.
  98. * @return mixed
  99. */
  100. public function &__get( $name ) {
  101. $value = '';
  102. switch ( $name ) {
  103. case 'dp' :
  104. $value = wc_get_price_decimals();
  105. break;
  106. case 'prices_include_tax' :
  107. $value = wc_prices_include_tax();
  108. break;
  109. case 'round_at_subtotal' :
  110. $value = 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
  111. break;
  112. case 'cart_contents_total' :
  113. $value = $this->get_cart_contents_total();
  114. break;
  115. case 'total' :
  116. $value = $this->get_total( 'edit' );
  117. break;
  118. case 'subtotal' :
  119. $value = $this->get_subtotal() + $this->get_subtotal_tax();
  120. break;
  121. case 'subtotal_ex_tax' :
  122. $value = $this->get_subtotal();
  123. break;
  124. case 'tax_total' :
  125. $value = $this->get_fee_tax() + $this->get_cart_contents_tax();
  126. break;
  127. case 'fee_total' :
  128. $value = $this->get_fee_total();
  129. break;
  130. case 'discount_cart' :
  131. $value = $this->get_discount_total();
  132. break;
  133. case 'discount_cart_tax' :
  134. $value = $this->get_discount_tax();
  135. break;
  136. case 'shipping_total' :
  137. $value = $this->get_shipping_total();
  138. break;
  139. case 'shipping_tax_total' :
  140. $value = $this->get_shipping_tax();
  141. break;
  142. case 'display_totals_ex_tax' :
  143. case 'display_cart_ex_tax' :
  144. $value = ! $this->display_prices_including_tax();
  145. break;
  146. case 'cart_contents_weight' :
  147. $value = $this->get_cart_contents_weight();
  148. break;
  149. case 'cart_contents_count' :
  150. $value = $this->get_cart_contents_count();
  151. break;
  152. case 'coupons' :
  153. $value = $this->get_coupons();
  154. break;
  155. // Arrays returned by reference to allow modification without notices. TODO: Remove in 4.0.
  156. case 'taxes' :
  157. 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()' ) );
  158. $value = &$this->totals[ 'cart_contents_taxes' ];
  159. break;
  160. case 'shipping_taxes' :
  161. 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()' ) );
  162. $value = &$this->totals[ 'shipping_taxes' ];
  163. break;
  164. case 'coupon_discount_amounts' :
  165. $value = &$this->coupon_discount_totals;
  166. break;
  167. case 'coupon_discount_tax_amounts' :
  168. $value = &$this->coupon_discount_tax_totals;
  169. break;
  170. case 'fees' :
  171. wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::get_fees' ) );
  172. // Grab fees from the new API.
  173. $new_fees = $this->fees_api()->get_fees();
  174. // Add new fees to the legacy prop so it can be adjusted via legacy property.
  175. $this->fees = $new_fees;
  176. // Return by reference.
  177. $value = &$this->fees;
  178. break;
  179. // Deprecated args. TODO: Remove in 4.0.
  180. case 'tax' :
  181. wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax directly' );
  182. $this->tax = new WC_Tax();
  183. $value = $this->tax;
  184. break;
  185. case 'discount_total':
  186. 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/' );
  187. $value = 0;
  188. break;
  189. case 'tax_display_cart':
  190. wc_deprecated_argument( 'WC_Cart->tax_display_cart', '4.4', 'Use WC_Cart->get_tax_price_display_mode() instead.' );
  191. $value = $this->get_tax_price_display_mode();
  192. break;
  193. }
  194. return $value;
  195. }
  196. /**
  197. * Map legacy variables to setters.
  198. *
  199. * @param string $name Property name.
  200. * @param mixed $value Value to set.
  201. */
  202. public function __set( $name, $value ) {
  203. switch ( $name ) {
  204. case 'cart_contents_total' :
  205. $this->set_cart_contents_total( $value );
  206. break;
  207. case 'total' :
  208. $this->set_total( $value );
  209. break;
  210. case 'subtotal' :
  211. $this->set_subtotal( $value );
  212. break;
  213. case 'subtotal_ex_tax' :
  214. $this->set_subtotal( $value );
  215. break;
  216. case 'tax_total' :
  217. $this->set_cart_contents_tax( $value );
  218. $this->set_fee_tax( 0 );
  219. break;
  220. case 'taxes' :
  221. $this->set_cart_contents_taxes( $value );
  222. break;
  223. case 'shipping_taxes' :
  224. $this->set_shipping_taxes( $value );
  225. break;
  226. case 'fee_total' :
  227. $this->set_fee_total( $value );
  228. break;
  229. case 'discount_cart' :
  230. $this->set_discount_total( $value );
  231. break;
  232. case 'discount_cart_tax' :
  233. $this->set_discount_tax( $value );
  234. break;
  235. case 'shipping_total' :
  236. $this->set_shipping_total( $value );
  237. break;
  238. case 'shipping_tax_total' :
  239. $this->set_shipping_tax( $value );
  240. break;
  241. case 'coupon_discount_amounts' :
  242. $this->set_coupon_discount_totals( $value );
  243. break;
  244. case 'coupon_discount_tax_amounts' :
  245. $this->set_coupon_discount_tax_totals( $value );
  246. break;
  247. case 'fees' :
  248. wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::add_fee' ) );
  249. $this->fees = $value;
  250. break;
  251. default :
  252. $this->$name = $value;
  253. break;
  254. }
  255. }
  256. /**
  257. * Methods moved to session class in 3.2.0.
  258. */
  259. public function get_cart_from_session() { $this->session->get_cart_from_session(); }
  260. public function maybe_set_cart_cookies() { $this->session->maybe_set_cart_cookies(); }
  261. public function set_session() { $this->session->set_session(); }
  262. public function get_cart_for_session() { return $this->session->get_cart_for_session(); }
  263. public function persistent_cart_update() { $this->session->persistent_cart_update(); }
  264. public function persistent_cart_destroy() { $this->session->persistent_cart_destroy(); }
  265. /**
  266. * Get the total of all cart discounts.
  267. *
  268. * @return float
  269. */
  270. public function get_cart_discount_total() {
  271. return $this->get_discount_total();
  272. }
  273. /**
  274. * Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
  275. *
  276. * @return float
  277. */
  278. public function get_cart_discount_tax_total() {
  279. return $this->get_discount_tax();
  280. }
  281. /**
  282. * Renamed for consistency.
  283. *
  284. * @param string $coupon_code
  285. * @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
  286. */
  287. public function add_discount( $coupon_code ) {
  288. return $this->apply_coupon( $coupon_code );
  289. }
  290. /**
  291. * Remove taxes.
  292. *
  293. * @deprecated 3.2.0 Taxes are never calculated if customer is tax except making this function unused.
  294. */
  295. public function remove_taxes() {
  296. wc_deprecated_function( 'WC_Cart::remove_taxes', '3.2', '' );
  297. }
  298. /**
  299. * Init.
  300. *
  301. * @deprecated 3.2.0 Session is loaded via hooks rather than directly.
  302. */
  303. public function init() {
  304. wc_deprecated_function( 'WC_Cart::init', '3.2', '' );
  305. $this->get_cart_from_session();
  306. }
  307. /**
  308. * Function to apply discounts to a product and get the discounted price (before tax is applied).
  309. *
  310. * @deprecated 3.2.0 Calculation and coupon logic is handled in WC_Cart_Totals.
  311. * @param mixed $values Cart item.
  312. * @param mixed $price Price of item.
  313. * @param bool $add_totals Legacy.
  314. * @return float price
  315. */
  316. public function get_discounted_price( $values, $price, $add_totals = false ) {
  317. wc_deprecated_function( 'WC_Cart::get_discounted_price', '3.2', '' );
  318. $cart_item_key = $values['key'];
  319. $cart_item = $this->cart_contents[ $cart_item_key ];
  320. return $cart_item['line_total'];
  321. }
  322. /**
  323. * Gets the url to the cart page.
  324. *
  325. * @deprecated 2.5.0 in favor to wc_get_cart_url()
  326. * @return string url to page
  327. */
  328. public function get_cart_url() {
  329. wc_deprecated_function( 'WC_Cart::get_cart_url', '2.5', 'wc_get_cart_url' );
  330. return wc_get_cart_url();
  331. }
  332. /**
  333. * Gets the url to the checkout page.
  334. *
  335. * @deprecated 2.5.0 in favor to wc_get_checkout_url()
  336. * @return string url to page
  337. */
  338. public function get_checkout_url() {
  339. wc_deprecated_function( 'WC_Cart::get_checkout_url', '2.5', 'wc_get_checkout_url' );
  340. return wc_get_checkout_url();
  341. }
  342. /**
  343. * Sees if we need a shipping address.
  344. *
  345. * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
  346. * @return bool
  347. */
  348. public function ship_to_billing_address_only() {
  349. wc_deprecated_function( 'WC_Cart::ship_to_billing_address_only', '2.5', 'wc_ship_to_billing_address_only' );
  350. return wc_ship_to_billing_address_only();
  351. }
  352. /**
  353. * Coupons enabled function. Filterable.
  354. *
  355. * @deprecated 2.5.0
  356. * @return bool
  357. */
  358. public function coupons_enabled() {
  359. wc_deprecated_function( 'WC_Legacy_Cart::coupons_enabled', '2.5.0', 'wc_coupons_enabled' );
  360. return wc_coupons_enabled();
  361. }
  362. /**
  363. * Gets the total (product) discount amount - these are applied before tax.
  364. *
  365. * @deprecated 2.3.0 Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
  366. * @return mixed formatted price or false if there are none.
  367. */
  368. public function get_discounts_before_tax() {
  369. wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' );
  370. if ( $this->get_cart_discount_total() ) {
  371. $discounts_before_tax = wc_price( $this->get_cart_discount_total() );
  372. } else {
  373. $discounts_before_tax = false;
  374. }
  375. return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this );
  376. }
  377. /**
  378. * Get the total of all order discounts (after tax discounts).
  379. *
  380. * @deprecated 2.3.0 Order discounts (after tax) removed in 2.3.
  381. * @return int
  382. */
  383. public function get_order_discount_total() {
  384. wc_deprecated_function( 'get_order_discount_total', '2.3' );
  385. return 0;
  386. }
  387. /**
  388. * Function to apply cart discounts after tax.
  389. *
  390. * @deprecated 2.3.0 Coupons can not be applied after tax.
  391. * @param $values
  392. * @param $price
  393. */
  394. public function apply_cart_discounts_after_tax( $values, $price ) {
  395. wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' );
  396. }
  397. /**
  398. * Function to apply product discounts after tax.
  399. *
  400. * @deprecated 2.3.0 Coupons can not be applied after tax.
  401. *
  402. * @param $values
  403. * @param $price
  404. */
  405. public function apply_product_discounts_after_tax( $values, $price ) {
  406. wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' );
  407. }
  408. /**
  409. * Gets the order discount amount - these are applied after tax.
  410. *
  411. * @deprecated 2.3.0 Coupons can not be applied after tax.
  412. */
  413. public function get_discounts_after_tax() {
  414. wc_deprecated_function( 'get_discounts_after_tax', '2.3' );
  415. }
  416. }