mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
Add items with no price
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wearepixel/laravel-cart",
|
"name": "wearepixel/laravel-cart",
|
||||||
"version": "1.0.4",
|
"version": "1.0.5",
|
||||||
"description": "A cart implementation for Laravel",
|
"description": "A cart implementation for Laravel",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel",
|
"laravel",
|
||||||
|
|||||||
+6
-3
@@ -164,6 +164,7 @@ public function add(
|
|||||||
$this->add(
|
$this->add(
|
||||||
$id['id'],
|
$id['id'],
|
||||||
$id['name'],
|
$id['name'],
|
||||||
|
// isset($id['price']) ? $id['price'] : null,
|
||||||
$id['price'],
|
$id['price'],
|
||||||
$id['quantity'],
|
$id['quantity'],
|
||||||
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], []),
|
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], []),
|
||||||
@@ -698,15 +699,17 @@ protected function validate($item)
|
|||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
'id' => 'required',
|
'id' => 'required',
|
||||||
'price' => 'required|numeric',
|
|
||||||
'quantity' => 'required|numeric|min:0.1',
|
|
||||||
'name' => 'required',
|
'name' => 'required',
|
||||||
|
'quantity' => 'required|numeric|min:0.1',
|
||||||
];
|
];
|
||||||
|
|
||||||
$validator = CartItemValidator::make($item, $rules);
|
$validator = CartItemValidator::make($item, $rules);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
throw new InvalidItemException($validator->messages()->first());
|
// if the error is due to quantity
|
||||||
|
if ($validator->errors()->first('quantity') === 'The quantity must be at least 0.1.') {
|
||||||
|
throw new InvalidItemException('The quantity must be at least 0.1.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class Helpers
|
|||||||
/**
|
/**
|
||||||
* Normalize prices
|
* Normalize prices
|
||||||
*/
|
*/
|
||||||
public static function normalizePrice($price): float
|
public static function normalizePrice($price): ?float
|
||||||
{
|
{
|
||||||
return (is_string($price)) ? floatval($price) : $price;
|
return (is_string($price)) ? floatval($price) : $price;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,19 @@
|
|||||||
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('can add an item an array with no price', function () {
|
||||||
|
$this->cart->add([
|
||||||
|
'id' => 455,
|
||||||
|
'name' => 'Sample Item',
|
||||||
|
'quantity' => 2,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||||
|
expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should be 1');
|
||||||
|
expect($this->cart->getContent()->first()['id'])->toEqual(455, 'Item added has ID of 455 so first content ID should be 455');
|
||||||
|
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||||
|
})->only();
|
||||||
|
|
||||||
test('can add an item without attributes', function () {
|
test('can add an item without attributes', function () {
|
||||||
$item = [
|
$item = [
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user