From b9b4336c1b4f2ffe759065edc370963cf21679f8 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 26 Jul 2017 16:50:34 +0100 Subject: [PATCH] Removed some duplicate keys --- includes/abstracts/abstract-wc-totals.php | 44 ++++++++++------------- includes/class-wc-cart-totals.php | 3 +- includes/class-wc-discounts.php | 8 +++-- includes/class-wc-order-totals.php | 8 +++-- tests/unit-tests/discounts/discounts.php | 1 - 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/includes/abstracts/abstract-wc-totals.php b/includes/abstracts/abstract-wc-totals.php index 3c07cf4f643..e42def01076 100644 --- a/includes/abstracts/abstract-wc-totals.php +++ b/includes/abstracts/abstract-wc-totals.php @@ -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 ); diff --git a/includes/class-wc-cart-totals.php b/includes/class-wc-cart-totals.php index 20f3e62bf9a..802a7ff8039 100644 --- a/includes/class-wc-cart-totals.php +++ b/includes/class-wc-cart-totals.php @@ -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; } diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index fbcc4cb8aa1..977f207ac23 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -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 ); } diff --git a/includes/class-wc-order-totals.php b/includes/class-wc-order-totals.php index 9ebdeb6ceb4..d34beb0ddfd 100644 --- a/includes/class-wc-order-totals.php +++ b/includes/class-wc-order-totals.php @@ -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() ); diff --git a/tests/unit-tests/discounts/discounts.php b/tests/unit-tests/discounts/discounts.php index 1cb509fd029..2e019c4cd53 100644 --- a/tests/unit-tests/discounts/discounts.php +++ b/tests/unit-tests/discounts/discounts.php @@ -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'];