Removed some duplicate keys

This commit is contained in:
Mike Jolley 2017-07-26 16:50:34 +01:00
parent abdb325d83
commit b9b4336c1b
5 changed files with 31 additions and 33 deletions

View File

@ -111,6 +111,18 @@ abstract class WC_Totals {
$this->object = $object;
}
/**
* Run all calculations methods on the given items in sequence. @todo More documentation, and add other calculation methods for taxes and totals only?
*
* @since 3.2.0
*/
public function calculate() {
$this->calculate_item_totals();
$this->calculate_fee_totals();
$this->calculate_shipping_totals();
$this->calculate_totals();
}
/**
* Handles a cart or order object passed in for calculation. Normalises data
* into the same format for use by this class.
@ -192,10 +204,8 @@ abstract class WC_Totals {
*/
protected function get_default_item_props() {
return (object) array(
'key' => '',
'object' => null,
'quantity' => 0,
'price' => 0,
'product' => false,
'price_includes_tax' => false,
'subtotal' => 0,
@ -249,10 +259,10 @@ abstract class WC_Totals {
if ( $item_tax_rates !== $base_tax_rates ) {
// Work out a new base price without the shop's base tax.
$taxes = WC_Tax::calc_tax( $item->price, $base_tax_rates, true, true );
$taxes = WC_Tax::calc_tax( $item->subtotal, $base_tax_rates, true, true );
// Now we have a new item price (excluding TAX).
$item->price = $item->price - array_sum( $taxes );
$item->subtotal = $item->subtotal - array_sum( $taxes );
$item->price_includes_tax = false;
}
return $item;
@ -262,11 +272,11 @@ abstract class WC_Totals {
* Get discounted price of an item with precision (in cents).
*
* @since 3.2.0
* @param object $item Item to get the price of.
* @param object $item_key Item to get the price of.
* @return int
*/
protected function get_discounted_price_in_cents( $item ) {
return $item->price - $this->discount_totals[ $item->key ];
protected function get_discounted_price_in_cents( $item_key ) {
return $item->subtotal - $this->discount_totals[ $item_key ];
}
/**
@ -350,18 +360,6 @@ abstract class WC_Totals {
|--------------------------------------------------------------------------
*/
/**
* Run all calculations methods on the given items in sequence.
*
* @since 3.2.0
*/
protected function calculate() {
$this->calculate_item_totals();
$this->calculate_fee_totals();
$this->calculate_shipping_totals();
$this->calculate_totals();
}
/**
* Calculate item totals.
*
@ -372,8 +370,8 @@ abstract class WC_Totals {
$this->calculate_item_subtotals();
$this->calculate_discounts();
foreach ( $this->items as $item ) {
$item->total = $this->get_discounted_price_in_cents( $item );
foreach ( $this->items as $item_key => $item ) {
$item->total = $this->get_discounted_price_in_cents( $item_key );
$item->total_tax = 0;
if ( wc_tax_enabled() && $item->product->is_taxable() ) {
@ -411,10 +409,6 @@ abstract class WC_Totals {
if ( $item->price_includes_tax && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
$item = $this->adjust_non_base_location_price( $item );
}
$item->subtotal = $item->price;
$item->subtotal_tax = 0;
if ( wc_tax_enabled() && $item->product->is_taxable() ) {
$subtotal_taxes = WC_Tax::calc_tax( $item->subtotal, $this->get_item_tax_rates( $item ), $item->price_includes_tax );
$item->subtotal_tax = array_sum( $subtotal_taxes );

View File

@ -56,11 +56,10 @@ final class WC_Cart_Totals extends WC_Totals {
foreach ( $this->object->get_cart() as $cart_item_key => $cart_item ) {
$item = $this->get_default_item_props();
$item->key = $cart_item_key;
$item->object = $cart_item;
$item->price_includes_tax = wc_prices_include_tax();
$item->quantity = $cart_item['quantity'];
$item->price = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
$item->subtotal = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
$item->product = $cart_item['data'];
$this->items[ $cart_item_key ] = $item;
}

View File

@ -124,7 +124,7 @@ class WC_Discounts {
* Set cart/order items which will be discounted.
*
* @since 3.2.0
* @param array $items List items, normailised, by WC_Totals.
* @param array $items List items.
*/
public function set_items( $items ) {
$this->items = array();
@ -132,7 +132,11 @@ class WC_Discounts {
$this->applied_coupons = array();
if ( ! empty( $items ) && is_array( $items ) ) {
$this->items = $items;
foreach ( $items as $key => $item ) {
$this->items[ $key ] = $item;
$this->items[ $key ]->key = $key;
$this->items[ $key ]->price = $item->subtotal;
}
$this->discounts = array_fill_keys( array_keys( $items ), 0 );
}

View File

@ -30,7 +30,11 @@ final class WC_Order_Totals extends WC_Totals {
parent::__construct( $object );
if ( is_a( $object, 'WC_Order' ) ) {
$this->calculate();
// Get items from the order. @todo call calculate or make it manual?
$this->set_items();
$this->set_fees();
$this->set_shipping();
$this->set_coupons();
}
}
@ -44,11 +48,9 @@ final class WC_Order_Totals extends WC_Totals {
foreach ( $this->object->get_items() as $item_key => $item_object ) {
$item = $this->get_default_item_props();
$item->key = $item_key;
$item->object = $item_object;
$item->product = $item_object->get_product();
$item->quantity = $item_object->get_quantity();
$item->price = $this->add_precision( $item_object->get_subtotal() );
$item->subtotal = $this->add_precision( $item_object->get_subtotal() );
$item->subtotal_tax = $this->add_precision( $item_object->get_subtotal_tax() );
$item->total = $this->add_precision( $item_object->get_total() );

View File

@ -24,7 +24,6 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
'taxes' => array(),
'discounted_price' => 0,
);
$item->key = $cart_item_key;
$item->cart_item = $cart_item;
$item->quantity = $cart_item['quantity'];
$item->price = $cart_item['data']->get_price() * $precision * $cart_item['quantity'];