From 1bdcd72937e1e8480fb14dc81de9ff9bc63ef0c5 Mon Sep 17 00:00:00 2001 From: Doremidon Date: Sat, 22 Aug 2020 09:48:33 +0300 Subject: [PATCH 1/6] Type Unitary for single item --- src/Darryldecode/Cart/ItemCollection.php | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Darryldecode/Cart/ItemCollection.php b/src/Darryldecode/Cart/ItemCollection.php index a71503f..c8cea66 100644 --- a/src/Darryldecode/Cart/ItemCollection.php +++ b/src/Darryldecode/Cart/ItemCollection.php @@ -128,6 +128,41 @@ public function getPriceWithConditions($formatted = true) */ public function getPriceSumWithConditions($formatted = true) { - return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config); + $originalPrice = $this->price; + $newPrice = 0.00; + $price_summ = 0.00; + $unitary_summ = 0.00; + $processed = 0; + + if ($this->hasConditions()) { + if (is_array($this->conditions)) { + foreach ($this->conditions as $condition) { + ($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice; + if($condition->getType() == 'unitary'){ + $unitary_summ += $condition->applyCondition(0); + $newPrice = ($processed > 0) ? $newPrice : $originalPrice; + } + else{ + $newPrice = $condition->applyCondition($toBeCalculated); + } + $processed++; + } + $price_summ = Helpers::formatValue( ($newPrice * $this->quantity) + $unitary_summ, $formatted, $this->config); + } else { + if($this['conditions']->getType() == 'unitary'){ + $unitary_summ = $this['conditions']->applyCondition(0); + $newPrice = ($originalPrice * $this->quantity) + $unitary_summ; + } + else{ + $newPrice = $this['conditions']->applyCondition($originalPrice); + $newPrice = $newPrice * $this->quantity; + } + $price_summ = Helpers::formatValue($newPrice, $formatted, $this->config); + } + } + else{ + $price_summ = Helpers::formatValue($originalPrice * $this->quantity, $formatted, $this->config); + } + return Helpers::formatValue($price_summ, $formatted, $this->config); } } From 3a3eb34691b82fb8c790eab58b5e6ef9f09abf7d Mon Sep 17 00:00:00 2001 From: Doremidon Date: Thu, 26 Nov 2020 13:34:08 +0200 Subject: [PATCH 2/6] Update ItemCollection.php Add some comment --- src/Darryldecode/Cart/ItemCollection.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Darryldecode/Cart/ItemCollection.php b/src/Darryldecode/Cart/ItemCollection.php index c8cea66..ec24cb2 100644 --- a/src/Darryldecode/Cart/ItemCollection.php +++ b/src/Darryldecode/Cart/ItemCollection.php @@ -123,6 +123,7 @@ public function getPriceWithConditions($formatted = true) /** * get the sum of price in which conditions are already applied + * If "unitary" condition, price of unitary added for product without multiplying ProductQuantity * @param bool $formatted * @return mixed|null */ From 94de03fabb01f861085bda3bae6da93bd852153d Mon Sep 17 00:00:00 2001 From: Doremidon Date: Thu, 26 Nov 2020 14:58:53 +0200 Subject: [PATCH 3/6] Update CartCondition.php if type of condition is multiply we make count it --- src/Darryldecode/Cart/CartCondition.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Darryldecode/Cart/CartCondition.php b/src/Darryldecode/Cart/CartCondition.php index 2a42f09..43c3905 100644 --- a/src/Darryldecode/Cart/CartCondition.php +++ b/src/Darryldecode/Cart/CartCondition.php @@ -93,6 +93,16 @@ public function getValue() return $this->args['value']; } + /** + * the quantity of this the condition + * + * @return mixed + */ + public function getQuantity() + { + return $this->args['quantity']; + } + /** * Set the order to apply this condition. If no argument order is applied we return 0 as * indicator that no assignment has been made @@ -148,11 +158,15 @@ public function getCalculatedValue($totalOrSubTotalOrPrice) */ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { + // if type of condition is multiply we make count it or // if value has a percentage sign on it, we will get first // its percentage then we will evaluate again if the value // has a minus or plus sign so we can decide what to do with the // percentage, whether to add or subtract it to the total/subtotal/price // if we can't find any plus/minus sign, we will assume it as plus sign + if( $this->getType() == 'multiply' ){ + $result = $totalOrSubTotalOrPrice + ( $this->getValue() * $this->getQuantity() ); + } if( $this->valueIsPercentage($conditionValue) ) { if( $this->valueIsToBeSubtracted($conditionValue) ) From b91554e2c68ca365fe104af49a3785861f9a52e4 Mon Sep 17 00:00:00 2001 From: Doremidon Date: Tue, 1 Dec 2020 09:31:43 +0200 Subject: [PATCH 4/6] Multiply bug fix --- src/Darryldecode/Cart/CartCondition.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Darryldecode/Cart/CartCondition.php b/src/Darryldecode/Cart/CartCondition.php index 43c3905..05ae86b 100644 --- a/src/Darryldecode/Cart/CartCondition.php +++ b/src/Darryldecode/Cart/CartCondition.php @@ -167,7 +167,7 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue) if( $this->getType() == 'multiply' ){ $result = $totalOrSubTotalOrPrice + ( $this->getValue() * $this->getQuantity() ); } - if( $this->valueIsPercentage($conditionValue) ) + else if( $this->valueIsPercentage($conditionValue) ) { if( $this->valueIsToBeSubtracted($conditionValue) ) { From d30abf3ac36b1b928e3725dda68193a50ebf5744 Mon Sep 17 00:00:00 2001 From: Doremidon Date: Mon, 7 Dec 2020 16:08:41 +0200 Subject: [PATCH 5/6] Add countVsMultiply #1 --- src/Darryldecode/Cart/Cart.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Darryldecode/Cart/Cart.php b/src/Darryldecode/Cart/Cart.php index b582dca..bd58c26 100644 --- a/src/Darryldecode/Cart/Cart.php +++ b/src/Darryldecode/Cart/Cart.php @@ -687,6 +687,27 @@ public function isEmpty() return $this->getContent()->isEmpty(); } + /** + * count all items included inner multiple items + * + * @return int + */ + public function countVsMultiply() + { + $quantity = 0; + foreach($this->getContent() as $item){ + // vd( $item->attributes->quantityAllItems ); + if( $item->attributes->quantityAllItems > 0 ){ + $quantity += $item->attributes->quantityAllItems; + // vd($item->attributes->quantityAllItems); + } + else{ + $quantity += $item->quantity; + } + } + return $quantity; + } + /** * validate Item data * From 24af31e4d17d7cd9a51b5f103595327161abcb09 Mon Sep 17 00:00:00 2001 From: Doremidon Date: Wed, 9 Dec 2020 16:30:27 +0200 Subject: [PATCH 6/6] countVsMultiply $quantity++ --- src/Darryldecode/Cart/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Darryldecode/Cart/Cart.php b/src/Darryldecode/Cart/Cart.php index bd58c26..1ee8a8c 100644 --- a/src/Darryldecode/Cart/Cart.php +++ b/src/Darryldecode/Cart/Cart.php @@ -702,7 +702,7 @@ public function countVsMultiply() // vd($item->attributes->quantityAllItems); } else{ - $quantity += $item->quantity; + $quantity++; } } return $quantity;