Weight calculation based on product options (#221)

This commit is contained in:
Yura 2021-03-24 15:16:13 +03:00 committed by GitHub
parent 08df9043fa
commit 60998ad74f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 8 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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);;
}
}