mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
Merge pull request #17 from wearepixel/feature/livewire-support-for-nested-collections
Better Livewire Support
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
name: PHP Formatting
|
||||
|
||||
on: pull_request
|
||||
|
||||
jobs:
|
||||
php-formatting:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: "Run Pint"
|
||||
uses: aglipanci/laravel-pint-action@latest
|
||||
with:
|
||||
verboseMode: true
|
||||
configPath: "pint.json"
|
||||
pintVersion: 1.17.2
|
||||
|
||||
- name: Commit changes
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: "Apply Laravel Pint"
|
||||
commit_options: '--no-verify --signoff'
|
||||
file_pattern: "*.php"
|
||||
commit_user_name: "Pixel Tech"
|
||||
commit_user_email: "tech@wearepixel.com.au"
|
||||
commit_author: "Pixel Tech<tech@wearepixel.com.au>"
|
||||
@@ -368,8 +368,30 @@ #### Getting conditions on the cart: **Cart::getConditions()**
|
||||
$condition->getMaximum(); // the maximum dollar amount of the target, needed to keep the condition active
|
||||
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
|
||||
}
|
||||
```
|
||||
|
||||
// You can also get a condition that has been applied on the cart by using its name:
|
||||
#### Getting conditions on the cart as an array: **Cart::getConditions(array: true)**
|
||||
|
||||
You can get all cart conditions in array format by passing "array: true". This is useful if you want to store the carts conditions on a Livewire component since by default we have collections inside collections for conditions which Livewire does not support.
|
||||
|
||||
```php
|
||||
$cartConditions = Cart::getConditions(true);
|
||||
$cartConditions = Cart::getConditions(active: true);
|
||||
|
||||
foreach ($cartConditions as $condition) {
|
||||
$condition['name']; // the name of the condition
|
||||
$condition['type']; // the type
|
||||
$condition['value']; // the value of the condition
|
||||
$condition['order']; // the order of the condition
|
||||
$condition['minimum']; // the minimum dollar amount of the target, needed to activate the condition
|
||||
$condition['maximum']; // the maximum dollar amount of the target, needed to keep the condition active
|
||||
$condition['attributes']; // the attributes of the condition, returns an empty [] if no attributes added
|
||||
}
|
||||
```
|
||||
|
||||
#### Getting conditions by name: **Cart::getCondition($conditionName)**
|
||||
|
||||
```php
|
||||
$condition = Cart::getCondition('GST');
|
||||
|
||||
$condition->getTarget(); // the target of which the condition was applied
|
||||
|
||||
+10
-2
@@ -340,7 +340,7 @@ public function condition(array|CartCondition $condition): self
|
||||
*
|
||||
* @param bool $active - If true, only conditions actually applied on the cart are returned
|
||||
*/
|
||||
public function getConditions(bool $active = false): CartConditionCollection
|
||||
public function getConditions(bool $array = false, bool $active = false): CartConditionCollection|array
|
||||
{
|
||||
$conditions = new CartConditionCollection(
|
||||
$this->session->get($this->sessionKeyCartConditions)
|
||||
@@ -358,7 +358,9 @@ public function getConditions(bool $active = false): CartConditionCollection
|
||||
});
|
||||
}
|
||||
|
||||
return $conditions;
|
||||
return $array ? $conditions->map(function (CartCondition $condition) {
|
||||
return $condition->toArray();
|
||||
})->toArray() : $conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -404,6 +406,9 @@ public function removeCartCondition($conditionName)
|
||||
|
||||
$conditions->pull($conditionName);
|
||||
|
||||
// reset indexes
|
||||
$conditions = $conditions->values();
|
||||
|
||||
$this->saveConditions($conditions);
|
||||
}
|
||||
|
||||
@@ -456,6 +461,9 @@ public function removeItemCondition($itemId, $conditionName)
|
||||
}
|
||||
}
|
||||
|
||||
// reset indexes
|
||||
$item['conditions'] = array_values($item['conditions']);
|
||||
|
||||
$this->update($itemId, [
|
||||
'conditions' => $item['conditions'],
|
||||
]);
|
||||
|
||||
@@ -8,15 +8,12 @@
|
||||
|
||||
class CartCondition
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $args;
|
||||
|
||||
/**
|
||||
* the parsed raw value of the condition
|
||||
*/
|
||||
public $parsedRawValue = 0;
|
||||
private $parsedRawValue = 0;
|
||||
|
||||
/**
|
||||
* @param array $args (name, type, target, value)
|
||||
@@ -269,4 +266,9 @@ protected function validate($args)
|
||||
throw new InvalidConditionException($validator->messages()->first());
|
||||
}
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\CartCondition;
|
||||
use Wearepixel\Cart\CartConditionCollection;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
@@ -21,7 +22,168 @@
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
describe('conditions', function () {
|
||||
test('can be completely cleared', function () {
|
||||
$siteWideDiscount = new CartCondition([
|
||||
'name' => 'Site Wide Discount',
|
||||
'type' => 'discount',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$giftCard = new CartCondition([
|
||||
'name' => 'Gift Card',
|
||||
'type' => 'gift_card',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$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);
|
||||
|
||||
$this->cart->condition([$siteWideDiscount, $giftCard]);
|
||||
|
||||
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
|
||||
expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Cart should have one condition');
|
||||
|
||||
$this->cart->clearAllConditions();
|
||||
|
||||
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 converted to array', function () {
|
||||
$condition = new CartCondition([
|
||||
'name' => 'Site Wide Discount',
|
||||
'type' => 'discount',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition);
|
||||
|
||||
$conditions = $this->cart->getConditions(true);
|
||||
|
||||
expect($conditions)->toHaveCount(1, 'Cart should have one condition');
|
||||
expect($conditions['Site Wide Discount'])->toEqual([
|
||||
'name' => 'Site Wide Discount',
|
||||
'type' => 'discount',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
'order' => 1,
|
||||
]);
|
||||
});
|
||||
|
||||
test('can be removed by name', function () {
|
||||
$conditionOne = new CartCondition([
|
||||
'name' => '5% Site Wide Sale',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$conditionTwo = new CartCondition([
|
||||
'name' => 'Mother\'s Day Gift Pack',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$this->cart->add([
|
||||
'id' => 1,
|
||||
'name' => 'Body Scrub Kit',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
]);
|
||||
|
||||
$this->cart->condition([$conditionOne, $conditionTwo]);
|
||||
|
||||
// let's very first the item has 2 conditions in it
|
||||
expect($this->cart->getConditions())->toHaveCount(2, 'The item should have two conditions');
|
||||
|
||||
// now let's remove a condition on that item using the condition name
|
||||
$this->cart->removeCartCondition('5% Site Wide Sale');
|
||||
|
||||
// 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()[0]->getName())->toEqual('Mother\'s Day Gift Pack');
|
||||
});
|
||||
});
|
||||
|
||||
describe('cart level conditions', function () {
|
||||
test('can get conditions', function () {
|
||||
$this->cart->add([
|
||||
'id' => 456,
|
||||
'name' => 'Leather Shoes',
|
||||
'price' => 129.99,
|
||||
'quantity' => 1,
|
||||
]);
|
||||
|
||||
$condition = new CartCondition([
|
||||
'name' => '$5 Discount',
|
||||
'type' => 'tax',
|
||||
'target' => 'subtotal',
|
||||
'value' => '-5',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition);
|
||||
|
||||
$conditions = $this->cart->getConditions();
|
||||
|
||||
expect($conditions)->toHaveCount(1, 'Cart should have one condition');
|
||||
expect($conditions)->toBeInstanceOf(CartConditionCollection::class);
|
||||
});
|
||||
|
||||
test('can get conditions by name', function () {
|
||||
$this->cart->add([
|
||||
'id' => 456,
|
||||
'name' => 'Leather Shoes',
|
||||
'price' => 129.99,
|
||||
'quantity' => 1,
|
||||
]);
|
||||
|
||||
$conditionOne = new CartCondition([
|
||||
'name' => '$5 Discount',
|
||||
'type' => 'tax',
|
||||
'target' => 'subtotal',
|
||||
'value' => '-5',
|
||||
]);
|
||||
|
||||
$conditionTwo = new CartCondition([
|
||||
'name' => '5% Off',
|
||||
'type' => 'tax',
|
||||
'target' => 'subtotal',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$this->cart->condition($conditionOne);
|
||||
$this->cart->condition($conditionTwo);
|
||||
|
||||
$conditions = $this->cart->getConditions();
|
||||
|
||||
expect($conditions)->toHaveCount(2, 'Cart should have two conditions');
|
||||
expect($conditions)->toBeInstanceOf(CartConditionCollection::class);
|
||||
|
||||
$condition = $this->cart->getCondition('5% Off');
|
||||
|
||||
expect($condition)->toBeInstanceOf(CartCondition::class);
|
||||
expect($condition->getName())->toEqual('5% Off');
|
||||
expect($condition->getValue())->toEqual('-5%');
|
||||
});
|
||||
|
||||
test('can discount the subtotal by dollar amount', function () {
|
||||
$this->cart->add([
|
||||
'id' => 456,
|
||||
@@ -1092,7 +1254,7 @@
|
||||
});
|
||||
|
||||
describe('item level conditions', function () {
|
||||
test('can add an item with a condition', function () {
|
||||
test('can be added to an item', function () {
|
||||
$saleCondition = new CartCondition([
|
||||
'name' => '5% Discount',
|
||||
'type' => 'sale',
|
||||
@@ -1116,6 +1278,41 @@
|
||||
expect($this->cart->getTotal())->toEqual(18, 'Cart should have total of 18');
|
||||
});
|
||||
|
||||
test('can be removed by name', function () {
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => '5% Site Wide Sale',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Mother\'s Day Gift Pack',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 1,
|
||||
'name' => 'Body Scrub Kit',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition1, $itemCondition2],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// let's very first the item has 2 conditions in it
|
||||
expect($this->cart->get(1)['conditions'])->toHaveCount(2, 'The item should have two conditions');
|
||||
|
||||
// now let's remove a condition on that item using the condition name
|
||||
$this->cart->removeItemCondition(1, '5% Site Wide Sale');
|
||||
|
||||
// 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'][0]->getName())->toEqual('Mother\'s Day Gift Pack');
|
||||
});
|
||||
|
||||
test('can add item with multiple negative item conditions', function () {
|
||||
$saleCondition = new CartCondition([
|
||||
'name' => '5% Sale',
|
||||
@@ -1656,83 +1853,4 @@
|
||||
// expect($this->cart->getConditions()->first()->getName())->toEqual('Item Gift Pack 20');
|
||||
// expect($this->cart->getConditions()->last()->getName())->toEqual('TAX');
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
describe('conditions', function () {
|
||||
test('can be completely cleared', function () {
|
||||
$siteWideDiscount = new CartCondition([
|
||||
'name' => 'Site Wide Discount',
|
||||
'type' => 'discount',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$giftCard = new CartCondition([
|
||||
'name' => 'Gift Card',
|
||||
'type' => 'gift_card',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$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);
|
||||
|
||||
$this->cart->condition([$siteWideDiscount, $giftCard]);
|
||||
|
||||
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
|
||||
expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Cart should have one condition');
|
||||
|
||||
$this->cart->clearAllConditions();
|
||||
|
||||
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('remove item condition by condition name', function () {
|
||||
// $itemCondition1 = new CartCondition([
|
||||
// 'name' => 'SALE 5%',
|
||||
// 'type' => 'sale',
|
||||
// 'value' => '-5%',
|
||||
// ]);
|
||||
// $itemCondition2 = new CartCondition([
|
||||
// 'name' => 'Item Gift Pack 25.00',
|
||||
// 'type' => 'promo',
|
||||
// 'value' => '-25',
|
||||
// ]);
|
||||
|
||||
// $item = [
|
||||
// 'id' => 456,
|
||||
// 'name' => 'Sample Item 1',
|
||||
// 'price' => 100,
|
||||
// 'quantity' => 1,
|
||||
// 'attributes' => [],
|
||||
// 'conditions' => [$itemCondition1, $itemCondition2],
|
||||
// ];
|
||||
|
||||
// $this->cart->add($item);
|
||||
|
||||
// // let's very first the item has 2 conditions in it
|
||||
// expect($this->cart->get(456)['conditions'])->toHaveCount(2, 'Item should have two conditions');
|
||||
|
||||
// // now let's remove a condition on that item using the condition name
|
||||
// $this->cart->removeItemCondition(456, 'SALE 5%');
|
||||
|
||||
// // now we should have only 1 condition left on that item
|
||||
// expect($this->cart->get(456)['conditions'])->toHaveCount(1, 'Item should have one condition left');
|
||||
// });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user