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

173 lines
5.1 KiB
PHP
Raw Normal View History

2017-07-26 11:44:06 +00:00
<?php
/**
* Cart totals calculation class.
*
* Methods are protected and class is final to keep this as an internal API.
* May be opened in the future once structure is stable.
*
* @author Automattic
* @package WooCommerce/Classes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Cart_Totals class.
*
2017-07-26 13:32:43 +00:00
* @todo woocommerce_calculate_totals action for carts.
* @todo woocommerce_calculated_total filter for carts.
* @todo record coupon totals and counts for cart.
*
2017-07-26 11:44:06 +00:00
* @since 3.2.0
*/
final class WC_Cart_Totals extends WC_Totals {
/**
* Sets up the items provided, and calculate totals.
*
* @since 3.2.0
2017-07-26 13:32:43 +00:00
* @param object $object Cart or order object to calculate totals for.
2017-07-26 11:44:06 +00:00
*/
2017-07-26 13:32:43 +00:00
public function __construct( &$object = null ) {
2017-07-26 14:47:30 +00:00
parent::__construct( $object );
2017-07-26 13:32:43 +00:00
if ( is_a( $object, 'WC_Cart' ) ) {
2017-07-26 11:44:06 +00:00
$this->calculate();
}
}
/**
* Handles a cart or order object passed in for calculation. Normalises data
* into the same format for use by this class.
*
* Each item is made up of the following props, in addition to those returned by get_default_item_props() for totals.
* - key: An identifier for the item (cart item key or line item ID).
* - cart_item: For carts, the cart item from the cart which may include custom data.
* - quantity: The qty for this line.
* - price: The line price in cents.
* - product: The product object this cart item is for.
*
* @since 3.2.0
*/
protected function set_items() {
2017-07-26 13:32:43 +00:00
$this->items = array();
2017-07-26 11:44:06 +00:00
foreach ( $this->object->get_cart() as $cart_item_key => $cart_item ) {
$item = $this->get_default_item_props();
2017-07-26 13:32:43 +00:00
$item->object = $cart_item;
2017-07-26 14:47:30 +00:00
$item->price_includes_tax = wc_prices_include_tax();
2017-07-26 11:44:06 +00:00
$item->quantity = $cart_item['quantity'];
2017-07-26 15:50:34 +00:00
$item->subtotal = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
2017-07-26 11:44:06 +00:00
$item->product = $cart_item['data'];
$this->items[ $cart_item_key ] = $item;
}
}
/**
* Get fee objects from the cart. Normalises data
* into the same format for use by this class.
*
* @since 3.2.0
*/
protected function set_fees() {
$this->fees = array();
$this->object->calculate_fees();
foreach ( $this->object->get_fees() as $fee_key => $fee_object ) {
$fee = $this->get_default_fee_props();
$fee->object = $fee_object;
$fee->total = $this->add_precision( $fee->object->amount );
if ( wc_tax_enabled() && $fee->object->taxable ) {
$fee->taxes = WC_Tax::calc_tax( $fee->total, WC_Tax::get_rates( $fee->object->tax_class ), false );
$fee->total_tax = array_sum( $fee->taxes );
}
$this->fees[ $fee_key ] = $fee;
}
}
/**
* Get shipping methods from the cart and normalise.
*
* @since 3.2.0
*/
protected function set_shipping() {
$this->shipping = array();
foreach ( $this->object->calculate_shipping() as $key => $shipping_object ) {
$shipping_line = $this->get_default_shipping_props();
2017-07-26 13:32:43 +00:00
$shipping_line->object = $shipping_object;
2017-07-26 11:44:06 +00:00
$shipping_line->total = $this->add_precision( $shipping_object->cost );
2017-07-26 13:32:43 +00:00
$shipping_line->taxes = $this->add_precision( $shipping_object->taxes );
$shipping_line->total_tax = $this->add_precision( array_sum( $shipping_object->taxes ) );
2017-07-26 11:44:06 +00:00
$this->shipping[ $key ] = $shipping_line;
}
}
/**
* Return array of coupon objects from the cart. Normalises data
* into the same format for use by this class.
*
* @since 3.2.0
*/
protected function set_coupons() {
$this->coupons = $this->object->get_coupons();
}
/**
* Totals are costs after discounts.
*
* @since 3.2.0
*/
protected function calculate_item_totals() {
parent::calculate_item_totals();
$this->object->subtotal = $this->get_total( 'items_total' ) + $this->get_total( 'items_total_tax' );
$this->object->subtotal_ex_tax = $this->get_total( 'items_total' );
}
/**
* Triggers the cart fees API, grabs the list of fees, and calculates taxes.
*
* Note: This class sets the totals for the 'object' as they are calculated. This is so that APIs like the fees API can see these totals if needed.
*
* @since 3.2.0
*/
protected function calculate_fee_totals() {
parent::calculate_fee_totals();
foreach ( $this->fees as $fee_key => $fee ) {
$this->object->fees[ $fee_key ]->tax = $this->remove_precision( $fee->total_tax );
$this->object->fees[ $fee_key ]->tax_data = $this->remove_precision( $fee->taxes );
}
$this->object->fee_total = $this->remove_precision( array_sum( wp_list_pluck( $this->fees, 'total' ) ) );
}
/**
* Calculate any shipping taxes.
*
* @since 3.2.0
*/
protected function calculate_shipping_totals() {
parent::calculate_shipping_totals();
$this->object->shipping_total = $this->get_total( 'shipping_total' );
$this->object->shipping_tax_total = $this->get_total( 'shipping_tax_total' );
}
/**
* Main cart totals.
*
* @since 3.2.0
*/
protected function calculate_totals() {
parent::calculate_totals();
2017-07-26 13:32:43 +00:00
$this->object->tax_total = $this->get_total( 'tax_total' );
$this->object->total = $this->get_total( 'total' );
2017-07-26 11:44:06 +00:00
}
}