woocommerce/includes/legacy/class-wc-legacy-cart.php

366 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* Legacy cart
*
* Legacy and deprecated functions are here to keep the WC_Cart class clean.
* This class will be removed in future versions.
*
* @version 3.2.0
* @package WooCommerce/Classes
* @category Class
* @author Automattic
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Legacy cart class.
*/
abstract class WC_Legacy_Cart {
2017-08-18 11:24:17 +00:00
/**
* Array of defaults. Not used since 3.2.
*
* @deprecated 3.2.0
*/
public $cart_session_data = array(
'cart_contents_total' => 0,
'total' => 0,
'subtotal' => 0,
'subtotal_ex_tax' => 0,
'tax_total' => 0,
'taxes' => array(),
'shipping_taxes' => array(),
'discount_cart' => 0,
'discount_cart_tax' => 0,
'shipping_total' => 0,
'shipping_tax_total' => 0,
'coupon_discount_amounts' => array(),
'coupon_discount_tax_amounts' => array(),
'fee_total' => 0,
'fees' => array(),
);
/**
* Contains an array of coupon usage counts after they have been applied.
*
2017-08-08 09:51:35 +00:00
* @deprecated 3.2.0
* @var array
*/
public $coupon_applied_count = array();
/**
* Map legacy variables.
*
* @param string $name Property name.
* @param mixed $value Value to set.
*/
public function __isset( $name ) {
if ( array_key_exists( $name, $this->cart_session_data ) || 'fees' === $name ) {
return true;
}
return false;
}
2017-08-18 10:37:22 +00:00
/**
2017-08-18 11:24:17 +00:00
* Magic getters.
2017-08-18 10:37:22 +00:00
*
2017-08-18 11:24:17 +00:00
* @param string $name Property name.
2017-08-18 10:37:22 +00:00
* @return mixed
*/
2017-08-18 11:24:17 +00:00
public function __get( $name ) {
switch ( $name ) {
case 'dp' :
return wc_get_price_decimals();
2017-08-18 10:37:22 +00:00
case 'prices_include_tax' :
return wc_prices_include_tax();
case 'round_at_subtotal' :
return 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
2017-08-18 11:24:17 +00:00
// map old public props to methods.
case 'cart_contents_total' :
return $this->get_cart_contents_total();
case 'total' :
return $this->get_total( 'edit' );
case 'subtotal' :
return $this->get_subtotal() + $this->get_subtotal_tax();
case 'subtotal_ex_tax' :
return $this->get_subtotal();
case 'tax_total' :
2017-08-18 14:05:01 +00:00
return $this->get_fee_tax() + $this->get_cart_contents_tax();
2017-08-18 11:24:17 +00:00
case 'taxes' :
return $this->get_taxes();
case 'shipping_taxes' :
return $this->get_shipping_taxes();
case 'fee_total' :
return $this->get_fee_total();
case 'discount_cart' :
return $this->get_discount_total();
case 'discount_cart_tax' :
return $this->get_discount_tax();
case 'shipping_total' :
return $this->get_shipping_total();
case 'shipping_tax_total' :
return $this->get_shipping_tax();
case 'coupon_discount_amounts' :
return $this->get_coupon_discount_totals();
case 'coupon_discount_tax_amounts' :
return $this->get_coupon_discount_tax_totals();
2017-08-18 10:37:22 +00:00
case 'display_totals_ex_tax' :
case 'display_cart_ex_tax' :
return 'excl' === $this->tax_display_cart;
case 'cart_contents_weight' :
return $this->get_cart_contents_weight();
case 'cart_contents_count' :
return $this->get_cart_contents_count();
2017-08-22 14:17:58 +00:00
case 'fees' :
return $this->fees_api->get_fees();
2017-08-18 10:37:22 +00:00
case 'tax' :
wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax:: directly' );
$this->tax = new WC_Tax();
return $this->tax;
case 'discount_total':
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/' );
return 0;
case 'coupons' :
return $this->get_coupons();
}
}
2017-08-18 11:24:17 +00:00
/**
* Map legacy variables to setters.
*
* @param string $name Property name.
* @param mixed $value Value to set.
*/
public function __set( $name, $value ) {
switch ( $name ) {
case 'cart_contents_total' :
$this->set_cart_contents_total( $value );
break;
case 'total' :
$this->set_total( $value );
break;
case 'subtotal' :
$this->set_subtotal( $value );
break;
case 'subtotal_ex_tax' :
$this->set_subtotal( $value );
break;
case 'tax_total' :
$this->set_cart_contents_tax( $value );
$this->set_fee_tax( 0 );
break;
case 'taxes' :
$this->set_cart_contents_taxes( $value );
break;
case 'shipping_taxes' :
$this->set_shipping_taxes( $value );
break;
case 'fee_total' :
$this->set_fee_total( $value );
break;
case 'discount_cart' :
$this->set_discount_total( $value );
break;
case 'discount_cart_tax' :
$this->set_discount_tax( $value );
break;
case 'shipping_total' :
$this->set_shipping_total( $value );
break;
case 'shipping_tax_total' :
$this->set_shipping_tax( $value );
break;
case 'coupon_discount_amounts' :
$this->set_coupon_discount_totals( $value );
break;
case 'coupon_discount_tax_amounts' :
$this->set_coupon_discount_tax_totals( $value );
break;
2017-08-22 14:17:58 +00:00
case 'fees' :
$this->fees_api->set_fees( $value );
break;
2017-08-18 11:24:17 +00:00
default :
$this->$name = $value;
break;
}
}
2017-08-18 10:37:22 +00:00
2017-08-18 10:29:26 +00:00
/**
* Methods moved to session class in 3.2.0.
*/
public function get_cart_from_session() { $this->session->get_cart_from_session(); }
public function maybe_set_cart_cookies() { $this->session->maybe_set_cart_cookies(); }
public function set_session() { $this->session->set_session(); }
public function get_cart_for_session() { return $this->session->get_cart_for_session(); }
2017-08-18 10:29:26 +00:00
public function persistent_cart_update() { $this->session->persistent_cart_update(); }
public function persistent_cart_destroy() { $this->session->persistent_cart_destroy(); }
2017-08-18 11:51:45 +00:00
/**
* Get the total of all cart discounts.
*
* @return float
*/
public function get_cart_discount_total() {
return $this->get_discount_total();
}
/**
* Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
*
* @return float
*/
public function get_cart_discount_tax_total() {
return $this->get_discount_tax();
}
2017-08-18 10:29:26 +00:00
/**
* Renamed for consistency.
*
* @param string $coupon_code
* @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
*/
public function add_discount( $coupon_code ) {
return $this->apply_coupon( $coupon_code );
}
/**
* Remove taxes.
*
* @deprecated 3.2.0 Taxes are never calculated if customer is tax except making this function unused.
*/
public function remove_taxes() {
wc_deprecated_function( 'WC_Cart::remove_taxes', '3.2', '' );
}
/**
* Init.
*
* @deprecated 3.2.0 Session is loaded via hooks rather than directly.
*/
public function init() {
wc_deprecated_function( 'WC_Cart::init', '3.2', '' );
$this->get_cart_from_session();
}
/**
2017-08-08 09:51:35 +00:00
* Function to apply discounts to a product and get the discounted price (before tax is applied).
*
2017-08-08 09:51:35 +00:00
* @deprecated Calculation and coupon logic is handled in WC_Cart_Totals.
* @param mixed $values Cart item.
* @param mixed $price Price of item.
* @param bool $add_totals Legacy.
* @return float price
*/
public function get_discounted_price( $values, $price, $add_totals = false ) {
wc_deprecated_function( 'WC_Cart::get_discounted_price', '3.2', '' );
$cart_item_key = $values['key'];
$cart_item = $this->cart_contents[ $cart_item_key ];
return $cart_item->get_line_total();
}
/**
* Gets the url to the cart page.
*
* @deprecated 2.5.0 in favor to wc_get_cart_url()
* @return string url to page
*/
public function get_cart_url() {
wc_deprecated_function( 'WC_Cart::get_cart_url', '2.5', 'wc_get_cart_url' );
return wc_get_cart_url();
}
/**
* Gets the url to the checkout page.
*
* @deprecated 2.5.0 in favor to wc_get_checkout_url()
* @return string url to page
*/
public function get_checkout_url() {
wc_deprecated_function( 'WC_Cart::get_checkout_url', '2.5', 'wc_get_checkout_url' );
return wc_get_checkout_url();
}
/**
* Sees if we need a shipping address.
*
* @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
* @return bool
*/
public function ship_to_billing_address_only() {
wc_deprecated_function( 'WC_Cart::ship_to_billing_address_only', '2.5', 'wc_ship_to_billing_address_only' );
return wc_ship_to_billing_address_only();
}
/**
* Coupons enabled function. Filterable.
*
* @deprecated 2.5.0 in favor to wc_coupons_enabled()
* @return bool
*/
public function coupons_enabled() {
return wc_coupons_enabled();
}
/**
* Gets the total (product) discount amount - these are applied before tax.
*
* @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
* @return mixed formatted price or false if there are none.
*/
2017-08-08 09:51:35 +00:00
public function get_discounts_before_tax() {
wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' );
if ( $this->get_cart_discount_total() ) {
$discounts_before_tax = wc_price( $this->get_cart_discount_total() );
} else {
$discounts_before_tax = false;
}
2017-08-08 09:51:35 +00:00
return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this );
}
/**
* Get the total of all order discounts (after tax discounts).
*
* @deprecated Order discounts (after tax) removed in 2.3.
* @return int
*/
public function get_order_discount_total() {
wc_deprecated_function( 'get_order_discount_total', '2.3' );
return 0;
}
/**
* Function to apply cart discounts after tax.
*
* @deprecated Coupons can not be applied after tax.
* @param $values
* @param $price
*/
public function apply_cart_discounts_after_tax( $values, $price ) {
wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' );
}
/**
* Function to apply product discounts after tax.
*
* @deprecated Coupons can not be applied after tax.
*
* @param $values
* @param $price
*/
public function apply_product_discounts_after_tax( $values, $price ) {
wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' );
}
/**
* Gets the order discount amount - these are applied after tax.
*
* @deprecated Coupons can not be applied after tax.
*/
public function get_discounts_after_tax() {
wc_deprecated_function( 'get_discounts_after_tax', '2.3' );
}
}