From 60998ad74f75ee9f5c05a58292bcc65fb062fc02 Mon Sep 17 00:00:00 2001 From: Yura Date: Wed, 24 Mar 2021 15:16:13 +0300 Subject: [PATCH] Weight calculation based on product options (#221) --- .../admin/model/extension/retailcrm/icml.php | 11 +++++--- .../system/library/retailcrm/retailcrm.php | 25 ++++++++++++++++--- tests/system/RetailcrmTest.php | 23 +++++++++++++++++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/upload/admin/model/extension/retailcrm/icml.php b/src/upload/admin/model/extension/retailcrm/icml.php index 5187470..1963ee9 100644 --- a/src/upload/admin/model/extension/retailcrm/icml.php +++ b/src/upload/admin/model/extension/retailcrm/icml.php @@ -293,6 +293,7 @@ class ModelExtensionRetailcrmIcml extends Model ) ) ); + // Options if (!empty($options)) { foreach($options as $optionKey => $optionData) { @@ -314,10 +315,12 @@ class ModelExtensionRetailcrmIcml extends Model $weight = $this->dd->createElement('param'); $weight->setAttribute('code', 'weight'); $weight->setAttribute('name', $this->language->get('weight')); - $weightValue = (isset($product['weight_class'])) - ? round($product['weight'], 3) . ' ' . $product['weight_class'] - : round($product['weight'], 3) - ; + $weightValue = round($product['weight'] + $optionsValues['weight'], 3); + + if (isset($product['weight_class'])) { + $weightValue = $weightValue . ' ' . $product['weight_class']; + } + $weight->appendChild($this->dd->createTextNode($weightValue)); $e->appendChild($weight); } diff --git a/src/upload/system/library/retailcrm/retailcrm.php b/src/upload/system/library/retailcrm/retailcrm.php index d010712..12ce31f 100644 --- a/src/upload/system/library/retailcrm/retailcrm.php +++ b/src/upload/system/library/retailcrm/retailcrm.php @@ -150,7 +150,8 @@ class Retailcrm { foreach($requiredOption['product_option_value'] as $optionValue) { $offers[$requiredOption['product_option_id'].':'.$requiredOption['option_id'].'-'.$optionValue['option_value_id']] = array( 'price' => (float)$this->getOptionPrice($optionValue), - 'qty' => $optionValue['quantity'] + 'qty' => $optionValue['quantity'], + 'weight' => round($this->getWeightOption($optionValue), 3) ); } } else { @@ -160,7 +161,8 @@ class Retailcrm { $offers[$optionKey.'_'.$requiredOption['product_option_id'].':'.$requiredOption['option_id'].'-'.$optionValue['option_value_id']] = array( 'price' => $optionAttr['price'] + (float)$this->getOptionPrice($optionValue), 'qty' => ($optionAttr['qty'] > $optionValue['quantity']) ? - $optionValue['quantity'] : $optionAttr['qty'] + $optionValue['quantity'] : $optionAttr['qty'], + 'weight' => round($optionAttr['weight'] + $this->getWeightOption($optionValue), 3) ); } } @@ -173,7 +175,8 @@ class Retailcrm { foreach($notRequiredOption['product_option_value'] as $optionValue) { $offers[$notRequiredOption['product_option_id'].':'.$notRequiredOption['option_id'].'-'.$optionValue['option_value_id']] = array( 'price' => (float)$this->getOptionPrice($optionValue), - 'qty' => $optionValue['quantity'] + 'qty' => $optionValue['quantity'], + 'weight' => round($this->getWeightOption($optionValue), 3) ); } } else { @@ -182,7 +185,8 @@ class Retailcrm { $offers[$optionKey.'_'.$notRequiredOption['product_option_id'].':'.$notRequiredOption['option_id'].'-'.$optionValue['option_value_id']] = array( 'price' => $optionAttr['price'] + (float)$this->getOptionPrice($optionValue), 'qty' => ($optionAttr['qty'] > $optionValue['quantity']) ? - $optionValue['quantity'] : $optionAttr['qty'] + $optionValue['quantity'] : $optionAttr['qty'], + 'weight' => round($optionAttr['weight'] + $this->getWeightOption($optionValue), 3) ); } } @@ -196,6 +200,19 @@ class Retailcrm { return $offers; } + /** + * @param array $option + * + * @return float + */ + private function getWeightOption($option) { + if ($option['weight_prefix'] === '-') { + return $option['weight'] * -1; + } + + return $option['weight']; + } + /** * @param array $optionValue * @return float|int|mixed diff --git a/tests/system/RetailcrmTest.php b/tests/system/RetailcrmTest.php index 76ff790..6be8d10 100644 --- a/tests/system/RetailcrmTest.php +++ b/tests/system/RetailcrmTest.php @@ -3,6 +3,7 @@ require_once __DIR__ . '/../' . getenv('TEST_SUITE') . '/TestCase.php'; class RetailcrmTest extends TestCase { + public function testGetOrderManager() { $retailcrm = new retailcrm\Retailcrm(static::$registry); @@ -15,4 +16,26 @@ class RetailcrmTest extends TestCase { $this->assertInstanceOf(\retailcrm\service\OrderManager::class, $manager); } + + public function testGetWeightOption() { + $retailCrm = new \retailcrm\Retailcrm(self::$registry); + + $reflection = new ReflectionClass($retailCrm); + $reflectionMethod = $reflection->getMethod('getWeightOption'); + $reflectionMethod->setAccessible('true'); + + $result = $reflectionMethod->invokeArgs( + $retailCrm, + [['weight_prefix' => '+', 'weight' => 5]] + ); + + $this->assertEquals(5, $result); + + $result = $reflectionMethod->invokeArgs( + $retailCrm, + [['weight_prefix' => '-', 'weight' => 5]] + ); + + $this->assertEquals(-5, $result);; + } }