Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Unitary for single item #274

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Darryldecode/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
16 changes: 15 additions & 1 deletion src/Darryldecode/Cart/CartCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) )
{
Expand Down
38 changes: 37 additions & 1 deletion src/Darryldecode/Cart/ItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}