undo extra changes

This commit is contained in:
Joel Male
2024-09-04 09:14:23 +10:00
parent 05318b7b13
commit 7ee26add8d
2 changed files with 5 additions and 111 deletions
+4 -80
View File
@@ -6,7 +6,6 @@
use Wearepixel\Cart\Validators\CartItemValidator;
use Wearepixel\Cart\Exceptions\InvalidItemException;
use Wearepixel\Cart\Exceptions\UnknownModelException;
use Wearepixel\Cart\Exceptions\InvalidConditionException;
class Cart
{
@@ -296,68 +295,18 @@ public function clear()
/**
* Adds a condition to the cart
*
* @deprecated Use addCondition or addConditions instead
*/
public function condition(array|CartCondition $condition): self
{
if (is_array($condition)) {
foreach ($condition as $c) {
$this->addCondition($c);
$this->condition($c);
}
return $this;
}
return $this->addCondition($condition);
}
/**
* Add a condition to the cart
*/
public function addCondition(CartCondition $condition): self
{
if (is_array($condition)) {
foreach ($condition as $c) {
$this->addCondition($c);
}
return $this;
}
if (! $condition instanceof CartCondition) {
throw new InvalidConditionException('Argument 1 must be an instance of \'Wearepixel\Cart\CartCondition\'');
}
$conditions = $this->getConditions();
// Check if order has been applied
if ($condition->getOrder() == 0) {
$last = $conditions->last();
$condition->setOrder(! is_null($last) ? $last->getOrder() + 1 : 1);
}
$conditions->put($condition->getName(), $condition);
$conditions = $conditions->sortBy(function ($condition, $key) {
return $condition->getOrder();
});
$this->saveConditions($conditions);
return $this;
}
/**
* Add multiple conditions to the cart
*/
public function addConditions(array $conditions): self
{
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
return $this->condition($condition);
}
/**
@@ -416,44 +365,19 @@ public function getConditionsByType($type)
public function removeConditionsByType(string $type)
{
$this->getConditionsByType($type)->each(function ($condition) {
$this->removeCondition($condition->getName());
$this->removeCartCondition($condition->getName());
});
}
/**
* Remove a condition from the cart
*
* @deprecated Use removeCondition instead
*/
public function removeCartCondition(string $conditionName)
{
$this->removeCondition($conditionName);
}
/**
* Remove a condition from the cart
*/
public function removeCondition(string $conditionName, $clearItemConditions = false)
public function removeCartCondition($conditionName)
{
$conditions = $this->getConditions();
$conditions->pull($conditionName);
if ($clearItemConditions) {
// check if condition name is on any item's conditions
$items = $this->getContent();
foreach ($items as $item) {
if (isset($item['conditions'])) {
foreach ($item['conditions'] as $condition) {
if ($condition->getName() === $conditionName) {
$this->removeItemCondition($item['id'], $conditionName);
}
}
}
}
}
$this->saveConditions($conditions);
}
+1 -31
View File
@@ -1256,36 +1256,6 @@
expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(0.00, "The item's price should be prevented from going below zero.");
});
test('can be removed by name when calling removeCondition with clearItemConditions: true', function () {
$itemCondition = new CartCondition([
'name' => 'Item Discount',
'type' => 'discount',
'value' => '-5%',
]);
$item = [
'id' => 1,
'name' => 'Backpack',
'price' => 168.72,
'quantity' => 1,
'attributes' => [],
'conditions' => [$itemCondition],
];
$this->cart->add($item);
expect($this->cart->getConditions())->toHaveCount(0, 'Cart should have no conditions');
expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Cart item should have one condition');
$this->cart->removeCondition(
conditionName: 'Item Discount',
clearItemConditions: true
);
expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now');
expect($this->cart->get(1)['conditions'])->toHaveCount(0, 'Cart items should have no conditions now');
});
test('can be removed by condition name', function () {
$heavyItemSurcharge = new CartCondition([
'name' => 'Heavy Item Surcharge',
@@ -1315,7 +1285,7 @@
$this->cart->removeItemCondition(12, 'Tax');
expect($this->cart->get(12)['conditions'])->toHaveCount(1, 'Item should have one condition');
})->only();
});
// test('remove item condition by condition name scenario two', function () {
// // NOTE: in this scenario, we will add the conditions not in array format