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