mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b102dd2356 | ||
|
|
25fc662c05 | ||
|
|
57328c2f64 | ||
|
|
4d19211f2c | ||
|
|
8cee4f6208 | ||
|
|
0ec0668bcf |
@@ -3,15 +3,15 @@ version: 2
|
|||||||
updates:
|
updates:
|
||||||
- package-ecosystem: "github-actions"
|
- package-ecosystem: "github-actions"
|
||||||
directory: "/"
|
directory: "/"
|
||||||
target-branch: "develop"
|
target-branch: "master"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "weekly"
|
interval: "weekly"
|
||||||
labels:
|
labels:
|
||||||
- "dependencies"
|
- "deps"
|
||||||
- package-ecosystem: "composer"
|
- package-ecosystem: "composer"
|
||||||
directory: "/"
|
directory: "/"
|
||||||
target-branch: "develop"
|
target-branch: "master"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "weekly"
|
interval: "weekly"
|
||||||
labels:
|
labels:
|
||||||
- "dependencies"
|
- "deps"
|
||||||
|
|||||||
@@ -3,3 +3,4 @@
|
|||||||
.idea
|
.idea
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
READMETODO.md
|
READMETODO.md
|
||||||
|
build
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) Pixel <hello@wearepixel.com.au>
|
Copyright (c) Pixel Digital Pty Ltd <hello@wearepixel.com.au>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -499,10 +499,18 @@ #### Add condition to existing Item on the cart: **Cart::addItemCondition($produ
|
|||||||
|
|
||||||
### Clearing Cart Conditions: **Cart::clearCartConditions()**
|
### Clearing Cart Conditions: **Cart::clearCartConditions()**
|
||||||
|
|
||||||
|
This clears all cart level conditions, and does not affect item level conditions.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::clearCartConditions()
|
Cart::clearCartConditions()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you wish to clear all conditions from all items and the cart, use **Cart::clearAllConditions()**
|
||||||
|
|
||||||
|
```php
|
||||||
|
Cart::clearAllConditions()
|
||||||
|
```
|
||||||
|
|
||||||
### Remove a specific cart condtion: **Cart::removeCartCondition($conditionName)**
|
### Remove a specific cart condtion: **Cart::removeCartCondition($conditionName)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wearepixel/laravel-cart",
|
"name": "wearepixel/laravel-cart",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"description": "A cart implementation for Laravel",
|
"description": "A cart implementation for Laravel",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel",
|
"laravel",
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.0",
|
"php": ">=8.1",
|
||||||
"illuminate/support": "^9.0|^10.0|^11.0",
|
"illuminate/support": "^9.0|^10.0|^11.0",
|
||||||
"illuminate/validation": "^9.0|^10.0|^11.0",
|
"illuminate/validation": "^9.0|^10.0|^11.0",
|
||||||
"illuminate/translation": "^9.0|^10.0|^11.0"
|
"illuminate/translation": "^9.0|^10.0|^11.0"
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
"require-dev": {
|
"require-dev": {
|
||||||
"mockery/mockery": "^1.4.2",
|
"mockery/mockery": "^1.4.2",
|
||||||
"laravel/pint": "dev-main",
|
"laravel/pint": "dev-main",
|
||||||
"pestphp/pest": "2.34.4",
|
"pestphp/pest": "2.35.0",
|
||||||
"larapack/dd": "dev-master"
|
"larapack/dd": "dev-master"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
Generated
+28
-27
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "b15572aabc5ce8af520d03870b1943ea",
|
"content-hash": "f22ad76e04818683f42bf13aa7a5e4ef",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "brick/math",
|
"name": "brick/math",
|
||||||
@@ -2963,36 +2963,36 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "pestphp/pest",
|
"name": "pestphp/pest",
|
||||||
"version": "v2.34.4",
|
"version": "v2.35.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/pestphp/pest.git",
|
"url": "https://github.com/pestphp/pest.git",
|
||||||
"reference": "6a1161ead830294ef8e21fab83c0bd118b0df7cc"
|
"reference": "d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/pestphp/pest/zipball/6a1161ead830294ef8e21fab83c0bd118b0df7cc",
|
"url": "https://api.github.com/repos/pestphp/pest/zipball/d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646",
|
||||||
"reference": "6a1161ead830294ef8e21fab83c0bd118b0df7cc",
|
"reference": "d0ff2c8ec294b7aa7fcb0f3ddc4fdec864234646",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"brianium/paratest": "^7.3.1",
|
"brianium/paratest": "^7.3.1",
|
||||||
"nunomaduro/collision": "^7.10.0|^8.1.1",
|
"nunomaduro/collision": "^7.10.0|^8.3.0",
|
||||||
"nunomaduro/termwind": "^1.15.1|^2.0.1",
|
"nunomaduro/termwind": "^1.15.1|^2.0.1",
|
||||||
"pestphp/pest-plugin": "^2.1.1",
|
"pestphp/pest-plugin": "^2.1.1",
|
||||||
"pestphp/pest-plugin-arch": "^2.7.0",
|
"pestphp/pest-plugin-arch": "^2.7.0",
|
||||||
"php": "^8.1.0",
|
"php": "^8.1.0",
|
||||||
"phpunit/phpunit": "^10.5.13"
|
"phpunit/phpunit": "^10.5.17"
|
||||||
},
|
},
|
||||||
"conflict": {
|
"conflict": {
|
||||||
"phpunit/phpunit": ">10.5.13",
|
"phpunit/phpunit": ">10.5.17",
|
||||||
"sebastian/exporter": "<5.1.0",
|
"sebastian/exporter": "<5.1.0",
|
||||||
"webmozart/assert": "<1.11.0"
|
"webmozart/assert": "<1.11.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"pestphp/pest-dev-tools": "^2.16.0",
|
"pestphp/pest-dev-tools": "^2.16.0",
|
||||||
"pestphp/pest-plugin-type-coverage": "^2.8.1",
|
"pestphp/pest-plugin-type-coverage": "^2.8.5",
|
||||||
"symfony/process": "^6.4.0|^7.0.4"
|
"symfony/process": "^6.4.0|^7.1.3"
|
||||||
},
|
},
|
||||||
"bin": [
|
"bin": [
|
||||||
"bin/pest"
|
"bin/pest"
|
||||||
@@ -3055,7 +3055,7 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/pestphp/pest/issues",
|
"issues": "https://github.com/pestphp/pest/issues",
|
||||||
"source": "https://github.com/pestphp/pest/tree/v2.34.4"
|
"source": "https://github.com/pestphp/pest/tree/v2.35.0"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -3067,7 +3067,7 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-03-14T19:44:18+00:00"
|
"time": "2024-08-02T10:57:29+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "pestphp/pest-plugin",
|
"name": "pestphp/pest-plugin",
|
||||||
@@ -3390,12 +3390,12 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/phpDocumentor/ReflectionDocBlock.git",
|
"url": "https://github.com/phpDocumentor/ReflectionDocBlock.git",
|
||||||
"reference": "aa53f8d4374d1f5bd3fc598548d6272cb5d9bf39"
|
"reference": "60741fe3871f40e44ca10a28ce85d08b7ed841cd"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/aa53f8d4374d1f5bd3fc598548d6272cb5d9bf39",
|
"url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/60741fe3871f40e44ca10a28ce85d08b7ed841cd",
|
||||||
"reference": "aa53f8d4374d1f5bd3fc598548d6272cb5d9bf39",
|
"reference": "60741fe3871f40e44ca10a28ce85d08b7ed841cd",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -3447,7 +3447,7 @@
|
|||||||
"issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues",
|
"issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues",
|
||||||
"source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.x"
|
"source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.x"
|
||||||
},
|
},
|
||||||
"time": "2024-05-21T06:14:15+00:00"
|
"time": "2024-08-14T20:00:37+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpdocumentor/type-resolver",
|
"name": "phpdocumentor/type-resolver",
|
||||||
@@ -3880,16 +3880,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/phpunit",
|
"name": "phpunit/phpunit",
|
||||||
"version": "10.5.13",
|
"version": "10.5.17",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||||
"reference": "20a63fc1c6db29b15da3bd02d4b6cf59900088a7"
|
"reference": "c1f736a473d21957ead7e94fcc029f571895abf5"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/20a63fc1c6db29b15da3bd02d4b6cf59900088a7",
|
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c1f736a473d21957ead7e94fcc029f571895abf5",
|
||||||
"reference": "20a63fc1c6db29b15da3bd02d4b6cf59900088a7",
|
"reference": "c1f736a473d21957ead7e94fcc029f571895abf5",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -3961,7 +3961,7 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.13"
|
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.17"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -3977,7 +3977,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-03-12T15:37:41+00:00"
|
"time": "2024-04-05T04:39:01+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "psr/log",
|
"name": "psr/log",
|
||||||
@@ -4959,12 +4959,12 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/symfony/console.git",
|
"url": "https://github.com/symfony/console.git",
|
||||||
"reference": "3c43d7ec3eb45e0eac993e86bee2905bfee5246b"
|
"reference": "cc2dd55c49e04b10cec4aa6e47e099a2d1656845"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/symfony/console/zipball/3c43d7ec3eb45e0eac993e86bee2905bfee5246b",
|
"url": "https://api.github.com/repos/symfony/console/zipball/cc2dd55c49e04b10cec4aa6e47e099a2d1656845",
|
||||||
"reference": "3c43d7ec3eb45e0eac993e86bee2905bfee5246b",
|
"reference": "cc2dd55c49e04b10cec4aa6e47e099a2d1656845",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -5044,7 +5044,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2024-08-13T14:29:53+00:00"
|
"time": "2024-08-15T22:49:18+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/deprecation-contracts",
|
"name": "symfony/deprecation-contracts",
|
||||||
@@ -5759,7 +5759,8 @@
|
|||||||
"aliases": [],
|
"aliases": [],
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"stability-flags": {
|
"stability-flags": {
|
||||||
"laravel/pint": 20
|
"laravel/pint": 20,
|
||||||
|
"larapack/dd": 20
|
||||||
},
|
},
|
||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
|
|
||||||
bootstrap="vendor/autoload.php"
|
bootstrap="vendor/autoload.php"
|
||||||
colors="true"
|
colors="true"
|
||||||
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
|
||||||
>
|
>
|
||||||
<testsuites>
|
<source>
|
||||||
<testsuite name="Unit">
|
|
||||||
<directory suffix="Test.php">./tests/Unit</directory>
|
|
||||||
</testsuite>
|
|
||||||
</testsuites>
|
|
||||||
<source>
|
|
||||||
<include>
|
<include>
|
||||||
<directory suffix=".php">./src</directory>
|
<directory suffix=".php">./src</directory>
|
||||||
</include>
|
</include>
|
||||||
</source>
|
</source>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Laravel Cart Test Suite">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"preset": "laravel",
|
"preset": "laravel",
|
||||||
"rules": {
|
"rules": {
|
||||||
"concat_space": {
|
"concat_space": {
|
||||||
"spacing": "one"
|
"spacing": "one"
|
||||||
},
|
},
|
||||||
"class_attributes_separation": false,
|
"class_attributes_separation": false,
|
||||||
"ordered_imports": {
|
"ordered_imports": {
|
||||||
"sort_algorithm": "length"
|
"sort_algorithm": "length"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-31
@@ -131,20 +131,20 @@ public function has($itemId)
|
|||||||
* add item to the cart, it can be an array or multi dimensional array
|
* add item to the cart, it can be an array or multi dimensional array
|
||||||
*
|
*
|
||||||
* @param string|array $id
|
* @param string|array $id
|
||||||
* @param string $name
|
|
||||||
* @param float $price
|
|
||||||
* @param int $quantity
|
|
||||||
* @param array $attributes
|
|
||||||
* @param CartCondition|array $conditions
|
|
||||||
* @param string $associatedModel
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
* @throws InvalidItemException
|
* @throws InvalidItemException
|
||||||
*/
|
*/
|
||||||
public function add($id, $name = null, $price = null, $quantity = null, $attributes = [], $conditions = [], $associatedModel = null)
|
public function add(
|
||||||
{
|
string|int|array $id,
|
||||||
// if the first argument is an array,
|
?string $name = null,
|
||||||
// we will need to call add again
|
?float $price = null,
|
||||||
|
int|float|null $quantity = null,
|
||||||
|
array $attributes = [],
|
||||||
|
array|CartCondition $conditions = [],
|
||||||
|
?string $associatedModel = null
|
||||||
|
): self {
|
||||||
|
// check if its an array of items
|
||||||
if (is_array($id)) {
|
if (is_array($id)) {
|
||||||
// the first argument is an array, now we will need to check if it is a multi dimensional
|
// the first argument is an array, now we will need to check if it is a multi dimensional
|
||||||
// array, if so, we will iterate through each item and call add again
|
// array, if so, we will iterate through each item and call add again
|
||||||
@@ -192,10 +192,10 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
|
|||||||
$item = $this->validate($data);
|
$item = $this->validate($data);
|
||||||
|
|
||||||
// get the cart
|
// get the cart
|
||||||
$cart = $this->getContent();
|
$inCart = $this->get($id);
|
||||||
|
|
||||||
// if the item is already in the cart we will just update it
|
// if the item is already in the cart we will just update it
|
||||||
if ($cart->has($id)) {
|
if ($inCart) {
|
||||||
$this->update($id, $item);
|
$this->update($id, $item);
|
||||||
} else {
|
} else {
|
||||||
$this->addItem($id, $item);
|
$this->addItem($id, $item);
|
||||||
@@ -207,15 +207,9 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* update a cart
|
* Update a cart item with whatever data is passed in
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* the $data will be an associative array, you don't need to pass all the data, only the key value
|
|
||||||
* of the item you want to update on it
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public function update($id, $data)
|
public function update(int|string $id, array $data): bool
|
||||||
{
|
{
|
||||||
if ($this->fireEvent('updating', $data) === false) {
|
if ($this->fireEvent('updating', $data) === false) {
|
||||||
return false;
|
return false;
|
||||||
@@ -344,14 +338,11 @@ public function clear()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add a condition on the cart
|
* Adds a condition to the cart
|
||||||
*
|
|
||||||
* @param CartCondition|array $condition
|
|
||||||
* @return $this
|
|
||||||
*
|
*
|
||||||
* @throws InvalidConditionException
|
* @throws InvalidConditionException
|
||||||
*/
|
*/
|
||||||
public function condition(CartCondition $condition)
|
public function condition(array|CartCondition $condition): self
|
||||||
{
|
{
|
||||||
if (is_array($condition)) {
|
if (is_array($condition)) {
|
||||||
foreach ($condition as $c) {
|
foreach ($condition as $c) {
|
||||||
@@ -524,13 +515,9 @@ public function clearItemConditions($itemId)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* clears all conditions on a cart,
|
* Clear cart conditions (this does not clear item conditions)
|
||||||
* this does not remove conditions that has been added specifically to an item/product.
|
|
||||||
* If you wish to remove a specific condition to a product, you may use the method: removeItemCondition($itemId, $conditionName)
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function clearCartConditions()
|
public function clearCartConditions(): void
|
||||||
{
|
{
|
||||||
$this->session->put(
|
$this->session->put(
|
||||||
$this->sessionKeyCartConditions,
|
$this->sessionKeyCartConditions,
|
||||||
@@ -538,6 +525,20 @@ public function clearCartConditions()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all conditions from all items and the cart
|
||||||
|
*/
|
||||||
|
public function clearAllConditions(): void
|
||||||
|
{
|
||||||
|
$cart = $this->getContent();
|
||||||
|
|
||||||
|
$cart->each(function ($item) {
|
||||||
|
$this->clearItemConditions($item->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->clearCartConditions();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get cart sub total without conditions
|
* get cart sub total without conditions
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -25,15 +25,10 @@ public static function roundMode($config): int
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if array is multi dimensional array
|
* Check if array is multi dimensional array
|
||||||
* This will only check the first element of the array if it is still an array
|
* This will only check if the first element of the array is an array
|
||||||
* to decide that it is a multi dimensional, if you want to check the array strictly
|
|
||||||
* with all on its element, flag the second argument as true
|
|
||||||
*
|
|
||||||
* @param bool $recursive
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public static function isMultiArray($array, $recursive = false)
|
public static function isMultiArray(array $array, bool $recursive = false): bool
|
||||||
{
|
{
|
||||||
if ($recursive) {
|
if ($recursive) {
|
||||||
return (count($array) == count($array, COUNT_RECURSIVE)) ? false : true;
|
return (count($array) == count($array, COUNT_RECURSIVE)) ? false : true;
|
||||||
@@ -45,8 +40,9 @@ public static function isMultiArray($array, $recursive = false)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -791,6 +791,40 @@
|
|||||||
expect($couponDiscount->getCalculatedValue())->toEqual(200.0, 'Coupon Discount value should be 200.0');
|
expect($couponDiscount->getCalculatedValue())->toEqual(200.0, 'Coupon Discount value should be 200.0');
|
||||||
expect($giftCard->getCalculatedValue())->toEqual(0, 'Gift Card value should be 0');
|
expect($giftCard->getCalculatedValue())->toEqual(0, 'Gift Card value should be 0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('can be 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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$item = [
|
||||||
|
'id' => 1,
|
||||||
|
'name' => 'Backpack',
|
||||||
|
'price' => 168.72,
|
||||||
|
'quantity' => 1,
|
||||||
|
'attributes' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->cart->add($item);
|
||||||
|
|
||||||
|
$this->cart->condition([$siteWideDiscount, $giftCard]);
|
||||||
|
|
||||||
|
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
|
||||||
|
|
||||||
|
$this->cart->clearCartConditions();
|
||||||
|
|
||||||
|
expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('item level conditions', function () {
|
describe('item level conditions', function () {
|
||||||
@@ -890,113 +924,72 @@
|
|||||||
expect($this->cart->getTotal())->toEqual(411.77, 'Cart total with 1 item should be 411.77');
|
expect($this->cart->getTotal())->toEqual(411.77, 'Cart total with 1 item should be 411.77');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('add item condition', function () {
|
test('can add item condition to an item that already has conditions', function () {
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
'id' => 456,
|
'id' => 1,
|
||||||
'name' => 'Sample Item 1',
|
'name' => 'Children\'s Bookcase',
|
||||||
'price' => 67.99,
|
'price' => 67.99,
|
||||||
'quantity' => 1,
|
'quantity' => 1,
|
||||||
'attributes' => [],
|
'attributes' => [],
|
||||||
],
|
],
|
||||||
[
|
|
||||||
'id' => 568,
|
|
||||||
'name' => 'Sample Item 2',
|
|
||||||
'price' => 69.25,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'id' => 856,
|
|
||||||
'name' => 'Sample Item 3',
|
|
||||||
'price' => 50.25,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->cart->add($items);
|
$this->cart->add($items);
|
||||||
|
|
||||||
$itemCondition2 = new CartCondition([
|
$discount = new CartCondition([
|
||||||
'name' => 'Item Gift Pack 25.00',
|
'name' => '$25 Off',
|
||||||
'type' => 'promo',
|
'type' => 'promo',
|
||||||
'value' => '-25',
|
'value' => '-25',
|
||||||
]);
|
]);
|
||||||
$coupon101 = new CartCondition([
|
|
||||||
'name' => 'COUPON 101',
|
$childrenDiscount = new CartCondition([
|
||||||
|
'name' => '5% Off Childrens Items',
|
||||||
'type' => 'coupon',
|
'type' => 'coupon',
|
||||||
'value' => '-5%',
|
'value' => '-5%',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$item = [
|
$item = [
|
||||||
'id' => 456,
|
'id' => 2,
|
||||||
'name' => 'Sample Item 1',
|
'name' => 'Children\'s Pack of Books',
|
||||||
'price' => 100,
|
'price' => 31.22,
|
||||||
'quantity' => 1,
|
'quantity' => 1,
|
||||||
'attributes' => [],
|
'attributes' => [],
|
||||||
'conditions' => [$itemCondition2],
|
'conditions' => [$discount],
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->cart->add($item);
|
$this->cart->add($item);
|
||||||
|
|
||||||
// let's prove first we have 1 condition on this item
|
// let's prove first we have 1 condition on this item
|
||||||
expect($this->cart->get($item['id'])['conditions'])->toHaveCount(1, 'Item should have 1 condition');
|
expect($this->cart->get(2)['conditions'])->toHaveCount(1, 'Item should have 1 condition');
|
||||||
|
|
||||||
// now let's insert a condition on an existing item on the cart
|
// now let's insert a condition on an existing item on the cart
|
||||||
$this->cart->addItemCondition($item['id'], $coupon101);
|
$this->cart->addItemCondition(2, $childrenDiscount);
|
||||||
|
|
||||||
expect($this->cart->get($item['id'])['conditions'])->toHaveCount(2, 'Item should have 2 conditions');
|
expect($this->cart->get(2)['conditions'])->toHaveCount(2, 'Item should have 2 conditions');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('add item condition restrict negative price', function () {
|
test('will not take the price below 0', function () {
|
||||||
$items = [
|
$item = [
|
||||||
[
|
'id' => 1,
|
||||||
'id' => 456,
|
'name' => 'Picture Frame',
|
||||||
'name' => 'Sample Item 1',
|
'price' => 22.50,
|
||||||
'price' => 67.99,
|
'quantity' => 1,
|
||||||
'quantity' => 1,
|
'attributes' => [],
|
||||||
'attributes' => [],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'id' => 568,
|
|
||||||
'name' => 'Sample Item 2',
|
|
||||||
'price' => 69.25,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'id' => 856,
|
|
||||||
'name' => 'Sample Item 3',
|
|
||||||
'price' => 50.25,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->cart->add($items);
|
|
||||||
|
|
||||||
$condition = new CartCondition([
|
$condition = new CartCondition([
|
||||||
'name' => 'Substract amount but prevent negative value',
|
'name' => '$25 Discount',
|
||||||
'type' => 'promo',
|
'type' => 'promo',
|
||||||
'value' => '-25',
|
'value' => '-25',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$item = [
|
|
||||||
'id' => 789,
|
|
||||||
'name' => 'Sample Item 1',
|
|
||||||
'price' => 20,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
'conditions' => [
|
|
||||||
$condition,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->cart->add($item);
|
$this->cart->add($item);
|
||||||
|
$this->cart->addItemCondition(1, $condition);
|
||||||
|
|
||||||
// Since the product price is 20 and the condition reduces it by 25,
|
// Since the product price is 20 and the condition reduces it by 25,
|
||||||
// check that the item's price has been prevented from dropping below zero.
|
// check that the item's price has been prevented from dropping below zero.
|
||||||
expect($this->cart->get($item['id'])->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('get cart condition by condition name', function () {
|
// test('get cart condition by condition name', function () {
|
||||||
@@ -1475,3 +1468,48 @@
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -477,8 +477,6 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('sub total when item quantity is updated by reduced', function () {
|
test('sub total when item quantity is updated by reduced', function () {
|
||||||
// $this->cart->setConfig(['format_numbers' => true, 'decimals' => 2, 'dec_point' => '.', 'thousands_sep' => ',']);
|
|
||||||
|
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
'id' => 456,
|
'id' => 456,
|
||||||
|
|||||||
Reference in New Issue
Block a user