diff --git a/src/Darryldecode/Cart/Cart.php b/src/Darryldecode/Cart/Cart.php index b582dca..1ee8a8c 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++; + } + } + return $quantity; + } + /** * validate Item data * diff --git a/src/Darryldecode/Cart/CartCondition.php b/src/Darryldecode/Cart/CartCondition.php index 2a42f09..05ae86b 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,12 +158,16 @@ 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->valueIsPercentage($conditionValue) ) + if( $this->getType() == 'multiply' ){ + $result = $totalOrSubTotalOrPrice + ( $this->getValue() * $this->getQuantity() ); + } + else if( $this->valueIsPercentage($conditionValue) ) { if( $this->valueIsToBeSubtracted($conditionValue) ) { diff --git a/src/Darryldecode/Cart/ItemCollection.php b/src/Darryldecode/Cart/ItemCollection.php index a71503f..ec24cb2 100644 --- a/src/Darryldecode/Cart/ItemCollection.php +++ b/src/Darryldecode/Cart/ItemCollection.php @@ -123,11 +123,47 @@ 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 */ 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); } }