From ebdc410fdaadf030316c1a84f4400e1116aacd74 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Wed, 20 Jul 2022 14:50:15 +0300 Subject: [PATCH] Change logic work with delivery cost --- CHANGELOG.md | 9 +++++ VERSION | 2 +- src/include/class-wc-retailcrm-history.php | 42 ++++++++++++++++------ src/include/class-wc-retailcrm-orders.php | 9 +++-- src/include/functions.php | 16 +++++++++ src/readme.txt | 11 +++++- src/retailcrm.php | 2 +- src/uninstall.php | 2 +- tests/test-wc-retailcrm-orders.php | 15 ++++++++ 9 files changed, 92 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b795aa7..a34304e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 2022-07-18 4.4.5 +* Change logic work with delivery cost +* Add price rounding from WC settings +* Add functionality for changing the time interval for cron tasks +* Fix error with empty 'paidAt' +* Change processing history by sinceId +* Fix spanish accents processing in ICML +* Fix WA icon positioning + ## 2022-05-26 4.4.4 * Add product description to ICML * Fix fatal error using API without api_key diff --git a/VERSION b/VERSION index f15ec04..5f70498 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.4.4 \ No newline at end of file +4.4.5 \ No newline at end of file diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index de8508a..77c4318 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -341,12 +341,12 @@ if (!class_exists('WC_Retailcrm_History')) : $shipping->set_method_id($options[$order['delivery']['code']]); } - if (isset($order['delivery']['cost']) && !wc_tax_enabled()) { - $shipping->set_total($order['delivery']['cost']); - } + if (wc_tax_enabled()) { + $rate = getShippingRates(); - if (!empty($order['delivery']['netCost']) && wc_tax_enabled()) { - $shipping->set_total($order['delivery']['netCost']); + $shipping->set_total($this->getDeliveryCost($order, $rate)); + } else { + $shipping->set_total($this->getDeliveryCost($order)); } if (isset($order['delivery']['service']['code'])) { @@ -881,15 +881,17 @@ if (!class_exists('WC_Retailcrm_History')) : } } - if (isset($order['delivery']['cost']) && !wc_tax_enabled()) { - $shipping->set_total($order['delivery']['cost']); - } elseif (isset($order['delivery']['netCost'])) { - $shipping->set_total($order['delivery']['netCost']); + if (wc_tax_enabled()) { + $rate = getShippingRates(); + + $shipping->set_total($this->getDeliveryCost($order, $rate)); + } else { + $shipping->set_total($this->getDeliveryCost($order)); } $shipping->set_order_id($wcOrder->get_id()); - $shipping->save(); + $wcOrder->add_item($shipping); } } @@ -1134,6 +1136,26 @@ if (!class_exists('WC_Retailcrm_History')) : return $handled; } + /** + * Get delivery cost for WC order + * + * @param array $order + * + * @return double + */ + private function getDeliveryCost($order, $rate = null) + { + $deliveryCost = $order['delivery']['cost'] ?? 0.0; + + if (empty($rate) || empty($deliveryCost)) { + return $deliveryCost; + } + + $decimalPlaces = wc_get_price_decimals(); + + return round($deliveryCost / (1 + $rate / 100), $decimalPlaces); + } + /** * Get custom fields mapping with settings. * diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index f3beb65..fc43baf 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -375,9 +375,14 @@ if (!class_exists('WC_Retailcrm_Orders')) : if (isset($shipping['total'])) { $orderData['delivery']['netCost'] = $shipping['total']; + $orderData['delivery']['cost'] = isset($shipping['total_tax']) + ? $shipping['total'] + $shipping['total_tax'] + : $shipping['total']; - if (isset($shipping['total_tax'])) { - $orderData['delivery']['cost'] = $shipping['total'] + $shipping['total_tax']; + $rate = getShippingRates(); + + if (!empty($rate)) { + $orderData['delivery']['vatRate'] = $rate; } } } diff --git a/src/include/functions.php b/src/include/functions.php index 1cedb1e..d56b1a0 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -150,3 +150,19 @@ function validateUrl(string $url) return (preg_match("/https:\/\/(.*).(retailcrm.(pro|ru|es)|simla.com)/", $url)) ? $url : ''; } +/** + * Get shipping rate. + * + * @return mixed + */ +function getShippingRates() +{ + $shippingRates = WC_Tax::get_shipping_tax_rates(); + + // Only one tax can be selected for shipping + if (is_array($shippingRates)) { + $shippingRates = array_shift($shippingRates); + } + + return $shippingRates['rate'] ?? $shippingRates; +} diff --git a/src/readme.txt b/src/readme.txt index d3c4c69..10ee19c 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -5,7 +5,7 @@ Tags: Интеграция, Simla.com, simla Requires PHP: 5.6 Requires at least: 5.3 Tested up to: 5.9 -Stable tag: 4.4.4 +Stable tag: 4.4.5 License: GPLv1 or later License URI: http://www.gnu.org/licenses/gpl-1.0.html @@ -82,6 +82,15 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.4.5 = +* Change logic work with delivery cost +* Add price rounding from WC settings +* Add functionality for changing the time interval for cron tasks +* Fix error with empty 'paidAt' +* Change processing history by sinceId +* Fix spanish accents processing in ICML +* Fix WA icon positioning + = 4.4.4 = * Add product description to ICML * Fix fatal error using API without api_key diff --git a/src/retailcrm.php b/src/retailcrm.php index f79e780..aec28bd 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -5,7 +5,7 @@ * Description: Integration plugin for WooCommerce & Simla.com * Author: RetailDriver LLC * Author URI: http://retailcrm.pro/ - * Version: 4.4.4 + * Version: 4.4.5 * Tested up to: 5.9 * WC requires at least: 5.4 * WC tested up to: 6.5 diff --git a/src/uninstall.php b/src/uninstall.php index bce0011..ee491bb 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -16,7 +16,7 @@ * * @link https://wordpress.org/plugins/woo-retailcrm/ * - * @version 4.4.4 + * @version 4.4.5 * * @package RetailCRM */ diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index c6d649e..3e98e7f 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -338,6 +338,21 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals(false, WC_Retailcrm_Orders::isCorporateOrder($this->order)); } + public function test_get_shipping_rates() + { + $rate = getShippingRates(); + + $this->assertEquals(null, $rate); + } + + public function test_validate_url() + { + $this->assertEquals('https://test.simla.com', validateUrl('https://test.simla.com')); + + // Not valid url + $this->assertEquals('', validateUrl('https://test.com')); + } + public function test_is_corporate_crm_order() { $this->assertEquals(