From f287ce8aa156421c89ff206999f2d88a091f1047 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Mon, 18 Jul 2022 11:19:22 +0300 Subject: [PATCH] Add price rounding from WC settings --- .../order/class-wc-retailcrm-order-item.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php index 3b1236a..c4f31b4 100644 --- a/src/include/order/class-wc-retailcrm-order-item.php +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -45,8 +45,11 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data */ public function build($item) { - $price = $this->calculate_price($item); - $discount_price = $this->calculate_discount($item, $price); + $decimalPlaces = wc_get_price_decimals(); + + // Calculate price and discount + $price = $this->calculatePrice($item, $decimalPlaces); + $discountPrice = $this->calculateDiscount($item, $price, $decimalPlaces); $data['productName'] = $item['name']; $data['initialPrice'] = $price; @@ -62,7 +65,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data $this->set_data_fields($data); $this->set_offer($item); - $this->set_data_field('discountManualAmount', round($discount_price, 2)); + $this->set_data_field('discountManualAmount', $discountPrice); return $this; } @@ -94,31 +97,31 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data /** * @param WC_Order_Item_Product $item + * @param int $decimalPlaces Price rounding from WC settings * * @return float */ - private function calculate_price(WC_Order_Item_Product $item) + private function calculatePrice(WC_Order_Item_Product $item, int $decimalPlaces) { $price = ($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()); - return round($price, 2); + return round($price, $decimalPlaces); } /** * @param WC_Order_Item_Product $item * @param $price + * @param int $decimalPlaces Price rounding from WC settings * * @return float|int - * @todo Rounding issues with prices in pennies / cents should be expected here. */ - private function calculate_discount(WC_Order_Item_Product $item, $price) + private function calculateDiscount(WC_Order_Item_Product $item, $price, int $decimalPlaces) { - $product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0; - $product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0; - $price_item = $product_price + $product_tax; - $discount_price = $price - $price_item; + $productPrice = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0; + $productTax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0; + $itemPrice = $productPrice + $productTax; - return round($discount_price, 2); + return round($price - $itemPrice, $decimalPlaces); } /**