mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
undo extra changes
This commit is contained in:
+4
-80
@@ -6,7 +6,6 @@
|
|||||||
use Wearepixel\Cart\Validators\CartItemValidator;
|
use Wearepixel\Cart\Validators\CartItemValidator;
|
||||||
use Wearepixel\Cart\Exceptions\InvalidItemException;
|
use Wearepixel\Cart\Exceptions\InvalidItemException;
|
||||||
use Wearepixel\Cart\Exceptions\UnknownModelException;
|
use Wearepixel\Cart\Exceptions\UnknownModelException;
|
||||||
use Wearepixel\Cart\Exceptions\InvalidConditionException;
|
|
||||||
|
|
||||||
class Cart
|
class Cart
|
||||||
{
|
{
|
||||||
@@ -296,68 +295,18 @@ public function clear()
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a condition to the cart
|
* Adds a condition to the cart
|
||||||
*
|
|
||||||
* @deprecated Use addCondition or addConditions instead
|
|
||||||
*/
|
*/
|
||||||
public function condition(array|CartCondition $condition): self
|
public function condition(array|CartCondition $condition): self
|
||||||
{
|
{
|
||||||
if (is_array($condition)) {
|
if (is_array($condition)) {
|
||||||
foreach ($condition as $c) {
|
foreach ($condition as $c) {
|
||||||
$this->addCondition($c);
|
$this->condition($c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->addCondition($condition);
|
return $this->condition($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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -416,44 +365,19 @@ public function getConditionsByType($type)
|
|||||||
public function removeConditionsByType(string $type)
|
public function removeConditionsByType(string $type)
|
||||||
{
|
{
|
||||||
$this->getConditionsByType($type)->each(function ($condition) {
|
$this->getConditionsByType($type)->each(function ($condition) {
|
||||||
$this->removeCondition($condition->getName());
|
$this->removeCartCondition($condition->getName());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a condition from the cart
|
* Remove a condition from the cart
|
||||||
*
|
|
||||||
* @deprecated Use removeCondition instead
|
|
||||||
*/
|
*/
|
||||||
public function removeCartCondition(string $conditionName)
|
public function removeCartCondition($conditionName)
|
||||||
{
|
|
||||||
$this->removeCondition($conditionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a condition from the cart
|
|
||||||
*/
|
|
||||||
public function removeCondition(string $conditionName, $clearItemConditions = false)
|
|
||||||
{
|
{
|
||||||
$conditions = $this->getConditions();
|
$conditions = $this->getConditions();
|
||||||
|
|
||||||
$conditions->pull($conditionName);
|
$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);
|
$this->saveConditions($conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1256,36 +1256,6 @@
|
|||||||
expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(0.00, "The item's price should be prevented from going below zero.");
|
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 () {
|
test('can be removed by condition name', function () {
|
||||||
$heavyItemSurcharge = new CartCondition([
|
$heavyItemSurcharge = new CartCondition([
|
||||||
'name' => 'Heavy Item Surcharge',
|
'name' => 'Heavy Item Surcharge',
|
||||||
@@ -1315,7 +1285,7 @@
|
|||||||
$this->cart->removeItemCondition(12, 'Tax');
|
$this->cart->removeItemCondition(12, 'Tax');
|
||||||
|
|
||||||
expect($this->cart->get(12)['conditions'])->toHaveCount(1, 'Item should have one condition');
|
expect($this->cart->get(12)['conditions'])->toHaveCount(1, 'Item should have one condition');
|
||||||
})->only();
|
});
|
||||||
|
|
||||||
// test('remove item condition by condition name scenario two', function () {
|
// test('remove item condition by condition name scenario two', function () {
|
||||||
// // NOTE: in this scenario, we will add the conditions not in array format
|
// // NOTE: in this scenario, we will add the conditions not in array format
|
||||||
|
|||||||
Reference in New Issue
Block a user