Compare commits

..
4 Commits
Author SHA1 Message Date
Joel Male b3ef4c38fc 1.0.11 2024-09-23 07:48:27 +10:00
Joel Male a45121ca63 Merge pull request #20 from wearepixel/task/fix-subtotal-always-being-used-if-the-total-is-$0
Task: Fix subtotal always being used when cart conditions on the total bring it to $0
2024-09-23 07:48:06 +10:00
Joel Male 5505f121c7 fix subtotal always being used 2024-09-23 07:45:59 +10:00
Joel Male 472d505041 wip: fix issue with conditions not being properly removed 2024-09-18 15:56:12 +10:00
3 changed files with 12 additions and 14 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "wearepixel/laravel-cart", "name": "wearepixel/laravel-cart",
"version": "1.0.9", "version": "1.0.11",
"description": "A cart implementation for Laravel", "description": "A cart implementation for Laravel",
"keywords": [ "keywords": [
"laravel", "laravel",
+7 -9
View File
@@ -406,9 +406,6 @@ public function removeCartCondition($conditionName)
$conditions->pull($conditionName); $conditions->pull($conditionName);
// reset indexes
$conditions = $conditions->values();
$this->saveConditions($conditions); $this->saveConditions($conditions);
} }
@@ -461,9 +458,6 @@ public function removeItemCondition($itemId, $conditionName)
} }
} }
// reset indexes
$item['conditions'] = array_values($item['conditions']);
$this->update($itemId, [ $this->update($itemId, [
'conditions' => $item['conditions'], 'conditions' => $item['conditions'],
]); ]);
@@ -604,10 +598,14 @@ public function getTotal(): int|float
return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config); return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config);
} }
$conditionsForTotal->each(function (CartCondition $condition) use ($subTotal, &$total) { $index = 0;
$toBeCalculated = $total > 0 ? $total : $subTotal;
$conditionsForTotal->each(function (CartCondition $condition) use ($subTotal, &$total, &$index) {
$toBeCalculated = ($index > 0) ? $total : $subTotal;
$total = $condition->applyCondition($toBeCalculated); $total = $condition->applyCondition($toBeCalculated);
$index++;
}); });
return Helpers::formatValue($total, $this->config['format_numbers'], $this->config); return Helpers::formatValue($total, $this->config['format_numbers'], $this->config);
@@ -736,7 +734,7 @@ protected function addItem($id, $item)
/** /**
* save the cart * save the cart
* *
* @param $cart CartCollection * @param $cart CartCollection
*/ */
protected function save($cart) protected function save($cart)
{ {
+4 -4
View File
@@ -10,11 +10,11 @@
$events->shouldReceive('dispatch'); $events->shouldReceive('dispatch');
$this->cart = new Cart( $this->cart = new Cart(
new SessionMock, new SessionMock(),
$events, $events,
'cart', 'cart',
'SAMPLESESSIONKEY', 'SAMPLESESSIONKEY',
require (__DIR__ . '/../Helpers/ConfigConditionsMock.php') require(__DIR__ . '/../Helpers/ConfigConditionsMock.php')
); );
}); });
@@ -119,7 +119,7 @@
// now we should have only 1 condition left on that item // now we should have only 1 condition left on that item
expect($this->cart->getConditions())->toHaveCount(1, 'Item should have one condition left'); expect($this->cart->getConditions())->toHaveCount(1, 'Item should have one condition left');
expect($this->cart->getConditions()[0]->getName())->toEqual('Mother\'s Day Gift Pack'); expect($this->cart->getConditions()->first()->getName())->toEqual('Mother\'s Day Gift Pack');
}); });
}); });
@@ -1310,7 +1310,7 @@
// now we should have only 1 condition left on that item // now we should have only 1 condition left on that item
expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Item should have one condition left'); expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Item should have one condition left');
expect($this->cart->get(1)['conditions'][0]->getName())->toEqual('Mother\'s Day Gift Pack'); expect($this->cart->get(1)['conditions'][1]->getName())->toEqual('Mother\'s Day Gift Pack');
}); });
test('can add item with multiple negative item conditions', function () { test('can add item with multiple negative item conditions', function () {