mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
489b2723d5 | ||
|
|
d3f673d52f |
@@ -13,7 +13,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
matrix:
|
matrix:
|
||||||
php: ['8.2', '8.3', '8.4']
|
php: ['8.1', '8.2', '8.3']
|
||||||
stability: [prefer-stable]
|
stability: [prefer-stable]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) Joel Male <joel@joelmale.com>
|
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
|
||||||
|
|||||||
@@ -1,80 +1,49 @@
|
|||||||
<a name="top"></a>
|
[](https://packagist.org/packages/wearepixel/laravel-cart)
|
||||||
[](https://joelmale.com)
|
[](https://github.com/wearepixel/laravel-cart/actions?query=workflow%3ATests+branch%3Amaster)
|
||||||
[](https://packagist.org/packages/joelwmale/laravel-cart)
|
[](https://packagist.org/packages/wearepixel/laravel-cart)
|
||||||
[](https://github.com/joelwmale/laravel-cart/actions?query=workflow%3ATests+branch%3Amaster)
|
[](https://packagist.org/packages/wearepixel/laravel-cart)
|
||||||
[](https://packagist.org/packages/joelwmale/laravel-cart)
|
|
||||||
[](#)
|
|
||||||
[](https://packagist.org/packages/joelwmale/laravel-cart)
|
|
||||||
[](#-license)
|
|
||||||
|
|
||||||
A Cart Implementation for Laravel.
|
A Cart Implementation for Laravel.
|
||||||
|
|
||||||
Supported Laravel Versions: 10, 11, and 12.
|
Supported Laravel Versions: 9, 10, 11
|
||||||
|
|
||||||
For Laravel 9.0 and below, please use version [1.0](https://github.com/joelwmale/laravel-cart/releases/tag/1.0.12)
|
## Quick Demo
|
||||||
|
|
||||||
## Table of Contents
|
|
||||||
- [Getting Started](#-getting-started)
|
|
||||||
- [Demo](#-deni)
|
|
||||||
- [Documentation](#-documentation)
|
|
||||||
- [Configuration](#configuration)
|
|
||||||
- [Basic Usage](#basic-usage)
|
|
||||||
- [Conditions](#conditions)
|
|
||||||
- [Cart Items](#cart-items)
|
|
||||||
- [Database Support](#database-support)
|
|
||||||
- [Events](#events)
|
|
||||||
- [Credits](#-credits)
|
|
||||||
- [License](#-license)
|
|
||||||
|
|
||||||
## 🚀 Getting Started
|
|
||||||
### 🔥 Installing
|
|
||||||
|
|
||||||
Install the package through [Composer](http://getcomposer.org/).
|
|
||||||
|
|
||||||
`composer require joelwmale/laravel-cart`
|
|
||||||
|
|
||||||
## 🧑🍳 Demo
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Add an item to the cart
|
// Add an item to the cart
|
||||||
\Cart::add(
|
\Cart::add(1, 'Product 1', 19.99, 2, ['size' => 'large']);
|
||||||
1, // any unique id
|
|
||||||
'Product 1', // product name
|
|
||||||
19.99, // product price
|
|
||||||
2, // quantity
|
|
||||||
['size' => 'large'] // an array of extra attributes
|
|
||||||
);
|
|
||||||
|
|
||||||
// get the entire cart
|
// Get the cart contents
|
||||||
$cartContents = \Cart::getContent();
|
$cartContents = \Cart::getContent();
|
||||||
|
|
||||||
// update an item already in the cart
|
// Update item quantity by its id
|
||||||
\Cart::update(
|
\Cart::update(1, ['quantity' => 3]);
|
||||||
1, // the same unique id that was used to add the item
|
|
||||||
['quantity' => 3] // the quantity to update
|
|
||||||
);
|
|
||||||
|
|
||||||
// remove the item by its id
|
// Remove an item from the cart by its id
|
||||||
\Cart::remove(1);
|
\Cart::remove(1);
|
||||||
|
|
||||||
// get the total of the cart
|
// Get cart total
|
||||||
$total = Cart::getTotal();
|
\$total = Cart::getTotal();
|
||||||
|
|
||||||
// clear it all when you've finished (like when you've stored the order)
|
// Clear the entire cart
|
||||||
\Cart::clear();
|
\Cart::clear();
|
||||||
```
|
```
|
||||||
|
|
||||||
## 📚 Documentation
|
## Installing
|
||||||
|
|
||||||
### ⚙️ Configuration
|
Install the package through [Composer](http://getcomposer.org/).
|
||||||
|
|
||||||
|
`composer require "wearepixel/laravel-cart"`
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
You can publish the configuration file to customize various options.
|
You can publish the configuration file to customize various options.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
php artisan vendor:publish --provider="Joelwmale\Cart\CartServiceProvider" --tag="config"
|
php artisan vendor:publish --provider="Wearepixel\Cart\CartServiceProvider" --tag="config"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Formatting Numbers
|
### Formatting Numbers
|
||||||
|
|
||||||
The package by default does not use round to format numbers, and instead returns the number using floatval().
|
The package by default does not use round to format numbers, and instead returns the number using floatval().
|
||||||
|
|
||||||
@@ -87,7 +56,7 @@ #### Formatting Numbers
|
|||||||
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
|
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Decimals
|
### Decimals
|
||||||
|
|
||||||
You can customize the number of decimals in the configuration file.
|
You can customize the number of decimals in the configuration file.
|
||||||
|
|
||||||
@@ -97,7 +66,7 @@ #### Decimals
|
|||||||
'decimals' => env('LARAVEL_CART_DECIMALS', 2),
|
'decimals' => env('LARAVEL_CART_DECIMALS', 2),
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Round Mode
|
### Round Mode
|
||||||
|
|
||||||
The package uses the `round` function to round the prices. You can customize the rounding mode in the configuration file.
|
The package uses the `round` function to round the prices. You can customize the rounding mode in the configuration file.
|
||||||
|
|
||||||
@@ -107,8 +76,21 @@ #### Round Mode
|
|||||||
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
|
'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
### Basic Usage
|
- [Basic Usage](#basic-usage)
|
||||||
|
- [Conditions](#conditions)
|
||||||
|
- [Items](#items)
|
||||||
|
- [Associating Models](#associating-models)
|
||||||
|
- [Instances](#instances)
|
||||||
|
- [Exceptions](#exceptions)
|
||||||
|
- [Events](#events)
|
||||||
|
- [Format Response](#format)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Using Different Storage](#storage)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
The cart has a default sessionKey that holds the cart data and stores it in the session.
|
The cart has a default sessionKey that holds the cart data and stores it in the session.
|
||||||
|
|
||||||
@@ -127,7 +109,7 @@ ### Basic Usage
|
|||||||
\Cart::session(User::first()->id)->add(1, 'Product 1', 19.99, 2, ['size' => 'large']);
|
\Cart::session(User::first()->id)->add(1, 'Product 1', 19.99, 2, ['size' => 'large']);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Adding to the cart: **Cart::add()**
|
### Adding to the cart: **Cart::add()**
|
||||||
|
|
||||||
There are a few ways to add items to the cart.
|
There are a few ways to add items to the cart.
|
||||||
|
|
||||||
@@ -198,7 +180,7 @@ #### Adding to the cart: **Cart::add()**
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Updating an item on a cart: **Cart::update()**
|
### Updating an item on a cart: **Cart::update()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::update(
|
Cart::update(
|
||||||
@@ -237,14 +219,14 @@ #### Updating an item on a cart: **Cart::update()**
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Removing an item on a cart: **Cart::remove()**
|
### Removing an item on a cart: **Cart::remove()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Remove an item from the cart by its id
|
// Remove an item from the cart by its id
|
||||||
Cart::remove(456);
|
Cart::remove(456);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Getting an item on a cart: **Cart::get()**
|
### Getting an item on a cart: **Cart::get()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Get an item from the cart by its id
|
// Get an item from the cart by its id
|
||||||
@@ -254,7 +236,7 @@ #### Getting an item on a cart: **Cart::get()**
|
|||||||
$summedPrice = Cart::get($itemId)->getPriceSum();
|
$summedPrice = Cart::get($itemId)->getPriceSum();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Getting the cart content: **Cart::getContent()**
|
### Getting the cart content: **Cart::getContent()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Returns a collection of the cart's contents
|
// Returns a collection of the cart's contents
|
||||||
@@ -274,13 +256,13 @@ #### Getting the cart content: **Cart::getContent()**
|
|||||||
$cartTotalQuantity = Cart::getTotalQuantity();
|
$cartTotalQuantity = Cart::getTotalQuantity();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Cart subtotal: **Cart::getSubTotal()**
|
### Cart subtotal: **Cart::getSubTotal()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$subTotal = Cart::getSubTotal();
|
$subTotal = Cart::getSubTotal();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Cart subtotal without conditions: **Cart::getSubTotalWithoutConditions()**
|
### Cart subtotal without conditions: **Cart::getSubTotalWithoutConditions()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$subTotalWithoutConditions = Cart::getSubTotalWithoutConditions();
|
$subTotalWithoutConditions = Cart::getSubTotalWithoutConditions();
|
||||||
@@ -292,29 +274,19 @@ #### Cart subtotal without conditions: **Cart::getSubTotalWithoutConditions()**
|
|||||||
$total = Cart::getTotal();
|
$total = Cart::getTotal();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Check if cart is empty: **Cart::isEmpty()**
|
### Check if cart is empty: **Cart::isEmpty()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::isEmpty();
|
Cart::isEmpty();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Clearing the Cart: **Cart::clear()**
|
### Clearing the Cart: **Cart::clear()**
|
||||||
|
|
||||||
This clears all items and conditions from the cart.
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::clear();
|
Cart::clear();
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Clearing the cart items only: **Cart::clearItems()**
|
## Conditions
|
||||||
|
|
||||||
This clears all items, but keeps the conditions (useful for when you want to keep the conditions but remove the items)
|
|
||||||
|
|
||||||
```php
|
|
||||||
Cart::clearItems();
|
|
||||||
```
|
|
||||||
|
|
||||||
### Conditions
|
|
||||||
|
|
||||||
Conditions are very useful for adding things like discounts, taxes, shipping, etc.
|
Conditions are very useful for adding things like discounts, taxes, shipping, etc.
|
||||||
|
|
||||||
@@ -326,13 +298,13 @@ ### Conditions
|
|||||||
|
|
||||||
You can also provide an `order` to cart conditions which tells the cart in what order to apply the conditions. Item level conditions do not support the `order` parameter.
|
You can also provide an `order` to cart conditions which tells the cart in what order to apply the conditions. Item level conditions do not support the `order` parameter.
|
||||||
|
|
||||||
#### Conditions on the cart
|
### Conditions on the cart
|
||||||
|
|
||||||
##### Adding a condition to the cart: **Cart::condition()**
|
#### Adding a condition to the cart: **Cart::condition()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Add a single condition to the cart
|
// Add a single condition to the cart
|
||||||
$condition = new \Joelwmale\Cart\CartCondition([
|
$condition = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'Tax: 10%',
|
'name' => 'Tax: 10%',
|
||||||
'type' => 'tax',
|
'type' => 'tax',
|
||||||
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
||||||
@@ -345,7 +317,7 @@ ##### Adding a condition to the cart: **Cart::condition()**
|
|||||||
Cart::condition($condition);
|
Cart::condition($condition);
|
||||||
|
|
||||||
// Add multiple conditions
|
// Add multiple conditions
|
||||||
$tax = new \Joelwmale\Cart\CartCondition([
|
$tax = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'Tax: 10%',
|
'name' => 'Tax: 10%',
|
||||||
'type' => 'tax',
|
'type' => 'tax',
|
||||||
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
||||||
@@ -353,7 +325,7 @@ ##### Adding a condition to the cart: **Cart::condition()**
|
|||||||
'order' => 2
|
'order' => 2
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$shipping = new \Joelwmale\Cart\CartCondition([
|
$shipping = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'Shipping: $15',
|
'name' => 'Shipping: $15',
|
||||||
'type' => 'shipping',
|
'type' => 'shipping',
|
||||||
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
'target' => 'subtotal', // this condition will be applied to cart's subtotal when getSubTotal() is called.
|
||||||
@@ -368,7 +340,7 @@ ##### Adding a condition to the cart: **Cart::condition()**
|
|||||||
Cart::condition([$tax, $shipping]);
|
Cart::condition([$tax, $shipping]);
|
||||||
|
|
||||||
// add condition to only apply on totals, not in subtotal
|
// add condition to only apply on totals, not in subtotal
|
||||||
$shipping = new \Joelwmale\Cart\CartCondition([
|
$shipping = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'Express Shipping $15',
|
'name' => 'Express Shipping $15',
|
||||||
'type' => 'shipping',
|
'type' => 'shipping',
|
||||||
'target' => 'total',
|
'target' => 'total',
|
||||||
@@ -379,7 +351,7 @@ ##### Adding a condition to the cart: **Cart::condition()**
|
|||||||
Cart::condition($shipping);
|
Cart::condition($shipping);
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Getting conditions on the cart: **Cart::getConditions()**
|
#### Getting conditions on the cart: **Cart::getConditions()**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// To get all applied conditions on a cart, use below:
|
// To get all applied conditions on a cart, use below:
|
||||||
@@ -398,7 +370,7 @@ ##### Getting conditions on the cart: **Cart::getConditions()**
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Getting conditions on the cart as an array: **Cart::getConditions(array: true)**
|
#### 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.
|
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.
|
||||||
|
|
||||||
@@ -417,7 +389,7 @@ ##### Getting conditions on the cart as an array: **Cart::getConditions(array: t
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Getting conditions by name: **Cart::getCondition($conditionName)**
|
#### Getting conditions by name: **Cart::getCondition($conditionName)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$condition = Cart::getCondition('GST');
|
$condition = Cart::getCondition('GST');
|
||||||
@@ -431,7 +403,7 @@ ##### Getting conditions by name: **Cart::getCondition($conditionName)**
|
|||||||
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
|
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Getting active conditions on the cart: **Cart::getConditions(active: true)**
|
#### Getting active conditions on the cart: **Cart::getConditions(active: true)**
|
||||||
|
|
||||||
You can get only active conditions by passsing `active: true` to the `getConditions()` method.
|
You can get only active conditions by passsing `active: true` to the `getConditions()` method.
|
||||||
|
|
||||||
@@ -467,14 +439,14 @@ ##### Getting active conditions on the cart: **Cart::getConditions(active: true)
|
|||||||
// will return no conditions if the subtotal is $210
|
// will return no conditions if the subtotal is $210
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Calculating condition value
|
### Calculating condition value
|
||||||
|
|
||||||
There are 2 ways to calculate the value of a condition:
|
There are 2 ways to calculate the value of a condition:
|
||||||
|
|
||||||
1. Using the `getCalculatedValue` method on the condition instance
|
1. Using the `getCalculatedValue` method on the condition instance
|
||||||
2. Using the `getCalculatedValueForCondition` method on the cart instance and passing the condition name
|
2. Using the `getCalculatedValueForCondition` method on the cart instance and passing the condition name
|
||||||
|
|
||||||
##### Using the `getCalculatedValue` method on the condition instance
|
#### Using the `getCalculatedValue` method on the condition instance
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$subTotal = Cart::getSubTotal();
|
$subTotal = Cart::getSubTotal();
|
||||||
@@ -482,7 +454,7 @@ ##### Using the `getCalculatedValue` method on the condition instance
|
|||||||
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
|
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Using the `getCalculatedValueForCondition` method on the cart instance
|
#### Using the `getCalculatedValueForCondition` method on the cart instance
|
||||||
|
|
||||||
This method automatically calculates the value of a condition by it's name based on the order of the conditions.
|
This method automatically calculates the value of a condition by it's name based on the order of the conditions.
|
||||||
|
|
||||||
@@ -515,7 +487,7 @@ ##### Using the `getCalculatedValueForCondition` method on the cart instance
|
|||||||
Cart::getCalculatedValueForCondition('Gift Card'); // returns 0 as the coupon discount is applied first and brings the subtotal to 0
|
Cart::getCalculatedValueForCondition('Gift Card'); // returns 0 as the coupon discount is applied first and brings the subtotal to 0
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Adding conditions that activate once a minimum value is met
|
### Adding conditions that activate once a minimum value is met
|
||||||
|
|
||||||
You can add a `minimum` amount required for a condition to activate.
|
You can add a `minimum` amount required for a condition to activate.
|
||||||
|
|
||||||
@@ -534,7 +506,7 @@ ##### Adding conditions that activate once a minimum value is met
|
|||||||
Cart::condition($tenPercentOff)
|
Cart::condition($tenPercentOff)
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Adding conditions that only activate up to a maximum value
|
### Adding conditions that only activate up to a maximum value
|
||||||
|
|
||||||
You can add a `maximum` amount required for a condition to activate.
|
You can add a `maximum` amount required for a condition to activate.
|
||||||
|
|
||||||
@@ -553,14 +525,14 @@ ##### Adding conditions that only activate up to a maximum value
|
|||||||
Cart::condition($shipping)
|
Cart::condition($shipping)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Conditions on items
|
### Condition on items
|
||||||
|
|
||||||
Item conditions are useful if you have discounts to be applied specifically on an item and not on the whole cart value.
|
Item conditions are useful if you have discounts to be applied specifically on an item and not on the whole cart value.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
|
||||||
// lets create first our condition instance
|
// lets create first our condition instance
|
||||||
$saleCondition = new \Joelwmale\Cart\CartCondition([
|
$saleCondition = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => '50% Off',
|
'name' => '50% Off',
|
||||||
'type' => 'tax',
|
'type' => 'tax',
|
||||||
'value' => '-50%',
|
'value' => '-50%',
|
||||||
@@ -580,7 +552,7 @@ #### Conditions on items
|
|||||||
Cart::add($product);
|
Cart::add($product);
|
||||||
|
|
||||||
// You can of course also do multiple conditions on an item
|
// You can of course also do multiple conditions on an item
|
||||||
$saleCondition = new \Joelwmale\Cart\CartCondition([
|
$saleCondition = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'SALE 5%',
|
'name' => 'SALE 5%',
|
||||||
'type' => 'sale',
|
'type' => 'sale',
|
||||||
'value' => '-5%',
|
'value' => '-5%',
|
||||||
@@ -614,7 +586,7 @@ #### Conditions on items
|
|||||||
$cartSubTotal = Cart::getSubTotal();
|
$cartSubTotal = Cart::getSubTotal();
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Add a condition to an existing item on the cart: **Cart::addItemCondition($productId, $itemCondition)**
|
#### Add a condition to an existing item on the cart: **Cart::addItemCondition($productId, $itemCondition)**
|
||||||
|
|
||||||
Adding Condition to an existing Item on the cart is simple as well.
|
Adding Condition to an existing Item on the cart is simple as well.
|
||||||
|
|
||||||
@@ -628,7 +600,7 @@ ##### Add a condition to an existing item on the cart: **Cart::addItemCondition(
|
|||||||
Cart::addItemCondition(456, $condition);
|
Cart::addItemCondition(456, $condition);
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Clearing Cart Conditions: **Cart::clearCartConditions()**
|
### Clearing Cart Conditions: **Cart::clearCartConditions()**
|
||||||
|
|
||||||
This clears all cart level conditions, and does not affect item level conditions.
|
This clears all cart level conditions, and does not affect item level conditions.
|
||||||
|
|
||||||
@@ -642,25 +614,25 @@ #### Clearing Cart Conditions: **Cart::clearCartConditions()**
|
|||||||
Cart::clearAllConditions()
|
Cart::clearAllConditions()
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Remove a specific cart condtion: **Cart::removeCartCondition($conditionName)**
|
### Remove a specific cart condtion: **Cart::removeCartCondition($conditionName)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::removeCartCondition('Summer Sale 5%')
|
Cart::removeCartCondition('Summer Sale 5%')
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Remove a specific item condition: **Cart::removeItemCondition($itemId, $conditionName)**
|
### Remove a specific item condition: **Cart::removeItemCondition($itemId, $conditionName)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::removeItemCondition(456, 'SALE 5%')
|
Cart::removeItemCondition(456, 'SALE 5%')
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Clear all item conditions: **Cart::clearItemConditions($itemId)**
|
### Clear all item conditions: **Cart::clearItemConditions($itemId)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::clearItemConditions(456)
|
Cart::clearItemConditions(456)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Get conditions by type: **Cart::getConditionsByType($type)**
|
### Get conditions by type: **Cart::getConditionsByType($type)**
|
||||||
|
|
||||||
This returns all conditions that has been added to the cart by the type specified.
|
This returns all conditions that has been added to the cart by the type specified.
|
||||||
|
|
||||||
@@ -668,13 +640,13 @@ #### Get conditions by type: **Cart::getConditionsByType($type)**
|
|||||||
$tax = Cart::getConditionsByType('tax');
|
$tax = Cart::getConditionsByType('tax');
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Remove conditions by type: **Cart::removeConditionsByType($type)**
|
### Remove conditions by type: **Cart::removeConditionsByType($type)**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::removeConditionsByType('tax');
|
Cart::removeConditionsByType('tax');
|
||||||
```
|
```
|
||||||
|
|
||||||
### Cart Items
|
## Cart Items
|
||||||
|
|
||||||
The method **Cart::getContent()** returns a collection of items.
|
The method **Cart::getContent()** returns a collection of items.
|
||||||
|
|
||||||
@@ -696,153 +668,8 @@ ### Cart Items
|
|||||||
$item->getPriceSumWithConditions();
|
$item->getPriceSumWithConditions();
|
||||||
```
|
```
|
||||||
|
|
||||||
### Storage Options
|
## Credits
|
||||||
|
|
||||||
By default the cart is stored in the session, but there are times you may want to store the cart in the database.
|
Huge credits to the original maintainer of the package, located at [darryldecode/laravelshoppingcart](https://github.com/darryldecode/laravelshoppingcart)
|
||||||
|
|
||||||
For instance, you may want to store the cart in the database so that the cart can be retrieved even after the user logs out or closes the browser, or you may want to add cart timeouts and support for multiple computers.
|
We have forked this package to update it to Laravel 11 and to add some features and support for newer PHP versions.
|
||||||
|
|
||||||
#### Session
|
|
||||||
|
|
||||||
The cart is stored in the session by default, using Laravel's in-built SessionManager.
|
|
||||||
|
|
||||||
#### Database Support
|
|
||||||
|
|
||||||
To get started, you'll need to create a new model for your Cart, the package requires only 3 columns, but you're free to extend this as you wish and add more columns.
|
|
||||||
|
|
||||||
You can utilise the [events](#events) provided by the package to store additional information alongside the cart.
|
|
||||||
|
|
||||||
```php
|
|
||||||
$table->string('session_id'); // this handles the session id of the cart
|
|
||||||
$table->text('items'); // this will store the cart items
|
|
||||||
$table->text('conditions'); // this will store the cart level conditions
|
|
||||||
```
|
|
||||||
|
|
||||||
Then add some json casts to your model and fillable columns:
|
|
||||||
|
|
||||||
```php
|
|
||||||
protected $guarded = [];
|
|
||||||
|
|
||||||
protected $casts = [
|
|
||||||
'items' => 'array',
|
|
||||||
'conditions' => 'array',
|
|
||||||
];
|
|
||||||
```
|
|
||||||
|
|
||||||
Then update the configuration file to use the database driver:
|
|
||||||
|
|
||||||
```php
|
|
||||||
'driver' => 'database',
|
|
||||||
|
|
||||||
'storage' => [
|
|
||||||
'session',
|
|
||||||
'database' => [
|
|
||||||
'model' => \App\Models\Cart::class, // your model here
|
|
||||||
'id' => 'session_id',
|
|
||||||
'items' => 'items',
|
|
||||||
'conditions' => 'conditions',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
```
|
|
||||||
|
|
||||||
### Events
|
|
||||||
|
|
||||||
The package provides a few events that you can listen to in order to manipulate the cart or take actions based on cart events.
|
|
||||||
|
|
||||||
#### LaravelCart.Created
|
|
||||||
|
|
||||||
This fires every time a cart is instantiated (i.e every time \Cart::add() is called)
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Added', function () {
|
|
||||||
// cart was created
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Adding
|
|
||||||
|
|
||||||
This fires every time an item is being added to the cart
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Adding', function ($item) {
|
|
||||||
// item is being added
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Added
|
|
||||||
|
|
||||||
This fires every time an item is successfully added to the cart
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Added', function ($item) {
|
|
||||||
// item was added
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Updating
|
|
||||||
|
|
||||||
This fires every time an item is being updated
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Updating', function ($item) {
|
|
||||||
// item is being updated
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Updated
|
|
||||||
|
|
||||||
This fires every time an item is successfully updated
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Updated', function ($item) {
|
|
||||||
// item was updated
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Removing
|
|
||||||
|
|
||||||
This fires every time an item is being removed
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Removing', function ($item) {
|
|
||||||
// item is being removed
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Removed
|
|
||||||
|
|
||||||
This fires every time an item is successfully removed
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Removed', function ($item) {
|
|
||||||
// item was removed
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Clearing
|
|
||||||
|
|
||||||
This fires every time the cart is being cleared
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Clearing', function () {
|
|
||||||
// cart is being cleared
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### LaravelCart.Cleared
|
|
||||||
|
|
||||||
This fires every time the cart is successfully cleared
|
|
||||||
|
|
||||||
```
|
|
||||||
Event::listen('LaravelCart.Cleared', function () {
|
|
||||||
// cart was cleared
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🫡 Credits
|
|
||||||
|
|
||||||
This package was orignally created by [darryldecode](https://github.com/darryldecode) but has since seen almost no updates. I have decided to take the old package and transform it into a new package with new features and updates.
|
|
||||||
|
|
||||||
## 📓 License
|
|
||||||
|
|
||||||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
|
||||||
+14
-21
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "joelwmale/laravel-cart",
|
"name": "wearepixel/laravel-cart",
|
||||||
"version": "1.2.0",
|
"version": "1.0.8",
|
||||||
"description": "A cart implementation for Laravel",
|
"description": "A cart implementation for Laravel",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel",
|
"laravel",
|
||||||
@@ -15,41 +15,34 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Joel Male",
|
"name": "Joel Male",
|
||||||
"email": "joel@joelmale.com"
|
"email": "joel@wearepixel.com.au"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.2",
|
"php": ">=8.1",
|
||||||
"illuminate/support": "^10.0|^11.0|^12.0",
|
"illuminate/support": "^9.0|^10.0|^11.0",
|
||||||
"illuminate/auth": "^10.0|^11.0|^12.0",
|
"illuminate/validation": "^9.0|^10.0|^11.0",
|
||||||
"illuminate/container": "^10.0|^11.0|^12.0",
|
"illuminate/translation": "^9.0|^10.0|^11.0"
|
||||||
"illuminate/database": "^10.0|^11.0|^12.0",
|
|
||||||
"illuminate/validation": "^10.0|^11.0|^12.0"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"mockery/mockery": "^1.6",
|
"mockery/mockery": "^1.4.2",
|
||||||
"laravel/pint": "^1.21",
|
"laravel/pint": "^1.17",
|
||||||
"pestphp/pest": "^3.0",
|
"pestphp/pest": "2.35.1",
|
||||||
"larapack/dd": "dev-master"
|
"larapack/dd": "1.1"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Joelwmale\\Cart\\": "src/"
|
"Wearepixel\\Cart\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
|
||||||
"psr-4": {
|
|
||||||
"Tests\\": "tests/"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
"Joelwmale\\Cart\\CartServiceProvider"
|
"Wearepixel\\Cart\\CartServiceProvider"
|
||||||
],
|
],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"Cart": "Joelwmale\\Cart\\Facades\\CartFacade"
|
"Cart": "Wearepixel\\Cart\\Facades\\CartFacade"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Generated
+1299
-3429
File diff suppressed because it is too large
Load Diff
+8
-11
@@ -14,18 +14,15 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* ---------------------------------------------------------------
|
* ---------------------------------------------------------------
|
||||||
* Storage
|
* Persistence
|
||||||
* ---------------------------------------------------------------
|
* ---------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
'driver' => 'session',
|
'storage' => null,
|
||||||
|
|
||||||
'storage' => [
|
/*
|
||||||
'session',
|
* ---------------------------------------------------------------
|
||||||
'database' => [
|
* Events
|
||||||
'model' => '',
|
* ---------------------------------------------------------------
|
||||||
'id' => '',
|
*/
|
||||||
'items' => '',
|
'events' => null,
|
||||||
'conditions' => '',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB |
@@ -14,7 +14,4 @@
|
|||||||
<directory>tests</directory>
|
<directory>tests</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
<php>
|
|
||||||
<env name="DB_CONNECTION" value="testing"/>
|
|
||||||
</php>
|
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
+96
-93
@@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Joelwmale\Cart\Helpers\Helpers;
|
use Wearepixel\Cart\Helpers\Helpers;
|
||||||
use Joelwmale\Cart\Validators\CartItemValidator;
|
use Wearepixel\Cart\Validators\CartItemValidator;
|
||||||
use Joelwmale\Cart\Exceptions\InvalidItemException;
|
use Wearepixel\Cart\Exceptions\InvalidItemException;
|
||||||
use Joelwmale\Cart\Exceptions\UnknownModelException;
|
use Wearepixel\Cart\Exceptions\UnknownModelException;
|
||||||
use Joelwmale\Cart\Exceptions\InvalidConditionException;
|
use Wearepixel\Cart\Exceptions\InvalidConditionException;
|
||||||
|
|
||||||
class Cart
|
class Cart
|
||||||
{
|
{
|
||||||
@@ -15,6 +15,10 @@ class Cart
|
|||||||
|
|
||||||
protected $instanceName;
|
protected $instanceName;
|
||||||
|
|
||||||
|
protected $sessionKey;
|
||||||
|
protected $sessionKeyCartItems;
|
||||||
|
protected $sessionKeyCartConditions;
|
||||||
|
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
protected $currentItemId;
|
protected $currentItemId;
|
||||||
@@ -22,38 +26,39 @@ class Cart
|
|||||||
protected $decimals;
|
protected $decimals;
|
||||||
protected $decPoint;
|
protected $decPoint;
|
||||||
|
|
||||||
public function __construct($sessionStorage, $events, $instanceName, $sessionKey, $config)
|
public function __construct($session, $events, $instanceName, $session_key, $config)
|
||||||
{
|
{
|
||||||
$this->validateConfig($config);
|
|
||||||
|
|
||||||
$this->events = $events;
|
$this->events = $events;
|
||||||
$this->session = new CartSession($sessionStorage, $sessionKey, $config);
|
$this->session = $session;
|
||||||
$this->instanceName = $instanceName;
|
$this->instanceName = $instanceName;
|
||||||
|
$this->sessionKey = $session_key;
|
||||||
|
$this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
|
||||||
|
$this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->currentItemId = null;
|
$this->currentItemId = null;
|
||||||
|
|
||||||
$this->fireEvent('Created');
|
$this->fireEvent('created');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateConfig($config)
|
/**
|
||||||
|
* sets the session key
|
||||||
|
*
|
||||||
|
* @param string $sessionKey the session key or identifier
|
||||||
|
* @return $this|bool
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function session($sessionKey)
|
||||||
{
|
{
|
||||||
if ($config['driver'] == 'database') {
|
if (! $sessionKey) {
|
||||||
if (! isset($config['storage']['database']['model'])) {
|
throw new \Exception('Session key is required.');
|
||||||
throw new \Exception('You need to set a model for the database driver in the cart configuration.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! isset($config['storage']['database']['id'])) {
|
|
||||||
throw new \Exception('You need to set an id (for the session) for the database driver in the cart configuration.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! isset($config['storage']['database']['items'])) {
|
|
||||||
throw new \Exception('You need to set an items key for the database driver in the cart configuration.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! isset($config['storage']['database']['conditions'])) {
|
|
||||||
throw new \Exception('You need to set a conditions key for the database driver in the cart configuration.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->sessionKey = $sessionKey;
|
||||||
|
$this->sessionKeyCartItems = $this->sessionKey . '_cart_items';
|
||||||
|
$this->sessionKeyCartConditions = $this->sessionKey . '_cart_conditions';
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,7 +168,7 @@ public function add(
|
|||||||
*/
|
*/
|
||||||
public function update(int|string $id, array $data): bool
|
public function update(int|string $id, array $data): bool
|
||||||
{
|
{
|
||||||
if ($this->fireEvent('Updating', 'item', $data) === false) {
|
if ($this->fireEvent('updating', $data) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +201,7 @@ public function update(int|string $id, array $data): bool
|
|||||||
|
|
||||||
$this->save($cart);
|
$this->save($cart);
|
||||||
|
|
||||||
$this->fireEvent('Updated', 'item', $item);
|
$this->fireEvent('updated', $item);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -222,11 +227,11 @@ public function setConfig($config)
|
|||||||
public function addItemCondition($productId, $itemCondition)
|
public function addItemCondition($productId, $itemCondition)
|
||||||
{
|
{
|
||||||
if ($product = $this->get($productId)) {
|
if ($product = $this->get($productId)) {
|
||||||
$conditionInstance = '\\Joelwmale\\Cart\\CartCondition';
|
$conditionInstance = '\\Wearepixel\\Cart\\CartCondition';
|
||||||
|
|
||||||
if ($itemCondition instanceof $conditionInstance) {
|
if ($itemCondition instanceof $conditionInstance) {
|
||||||
// we need to copy first to a temporary variable to hold the conditions
|
// we need to copy first to a temporary variable to hold the conditions
|
||||||
// to avoid hitting this error "Indirect modification of overloaded element of Joelwmale\Cart\ItemCollection has no effect"
|
// to avoid hitting this error "Indirect modification of overloaded element of Wearepixel\Cart\ItemCollection has no effect"
|
||||||
// this is due to laravel Collection instance that implements Array Access
|
// this is due to laravel Collection instance that implements Array Access
|
||||||
// // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
|
// // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
|
||||||
$itemConditionTempHolder = $product['conditions'];
|
$itemConditionTempHolder = $product['conditions'];
|
||||||
@@ -255,7 +260,7 @@ public function remove($id)
|
|||||||
{
|
{
|
||||||
$cart = $this->getContent();
|
$cart = $this->getContent();
|
||||||
|
|
||||||
if ($this->fireEvent('Removing', 'item', $id) === false) {
|
if ($this->fireEvent('removing', $id) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,33 +268,28 @@ public function remove($id)
|
|||||||
|
|
||||||
$this->save($cart);
|
$this->save($cart);
|
||||||
|
|
||||||
$this->fireEvent('Removed', 'item', $id);
|
$this->fireEvent('removed', $id);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear cart
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function clear()
|
public function clear()
|
||||||
{
|
{
|
||||||
if ($this->fireEvent('Clearing') === false) {
|
if ($this->fireEvent('clearing') === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->session->clear();
|
$this->session->put(
|
||||||
|
$this->sessionKeyCartItems,
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
$this->fireEvent('Cleared');
|
$this->fireEvent('cleared');
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clearItems()
|
|
||||||
{
|
|
||||||
if ($this->fireEvent('Clearing') === false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->session->clearItems();
|
|
||||||
|
|
||||||
$this->fireEvent('Cleared');
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -313,7 +313,7 @@ public function condition(array|CartCondition $condition): self
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (! $condition instanceof CartCondition) {
|
if (! $condition instanceof CartCondition) {
|
||||||
throw new InvalidConditionException('Argument 1 must be an instance of \'Joelwmale\Cart\CartCondition\'');
|
throw new InvalidConditionException('Argument 1 must be an instance of \'Wearepixel\Cart\CartCondition\'');
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditions = $this->getConditions();
|
$conditions = $this->getConditions();
|
||||||
@@ -326,9 +326,7 @@ public function condition(array|CartCondition $condition): self
|
|||||||
|
|
||||||
$conditions->put($condition->getName(), $condition);
|
$conditions->put($condition->getName(), $condition);
|
||||||
|
|
||||||
$conditions = $conditions->reject(function ($condition) {
|
$conditions = $conditions->sortBy(function ($condition, $key) {
|
||||||
return empty($condition);
|
|
||||||
})->sortBy(function ($condition, $key) {
|
|
||||||
return $condition->getOrder();
|
return $condition->getOrder();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -344,14 +342,9 @@ public function condition(array|CartCondition $condition): self
|
|||||||
*/
|
*/
|
||||||
public function getConditions(bool $array = false, bool $active = false): CartConditionCollection|array
|
public function getConditions(bool $array = false, bool $active = false): CartConditionCollection|array
|
||||||
{
|
{
|
||||||
$conditions = new CartConditionCollection($this->session->getConditions());
|
$conditions = new CartConditionCollection(
|
||||||
|
$this->session->get($this->sessionKeyCartConditions)
|
||||||
$conditions = $conditions->reject(function ($condition) {
|
);
|
||||||
return empty($condition);
|
|
||||||
})
|
|
||||||
->transform(function ($condition) {
|
|
||||||
return $condition instanceof CartCondition ? $condition : new CartCondition($condition);
|
|
||||||
});
|
|
||||||
|
|
||||||
if ($active) {
|
if ($active) {
|
||||||
return $conditions->filter(function (CartCondition $condition) {
|
return $conditions->filter(function (CartCondition $condition) {
|
||||||
@@ -413,6 +406,9 @@ public function removeCartCondition($conditionName)
|
|||||||
|
|
||||||
$conditions->pull($conditionName);
|
$conditions->pull($conditionName);
|
||||||
|
|
||||||
|
// reset indexes
|
||||||
|
$conditions = $conditions->values();
|
||||||
|
|
||||||
$this->saveConditions($conditions);
|
$this->saveConditions($conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,7 +451,7 @@ public function removeItemCondition($itemId, $conditionName)
|
|||||||
// on the given condition name the user wants to remove, if so,
|
// on the given condition name the user wants to remove, if so,
|
||||||
// lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
|
// lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
|
||||||
else {
|
else {
|
||||||
$conditionInstance = 'Joelwmale\\Cart\\CartCondition';
|
$conditionInstance = 'Wearepixel\\Cart\\CartCondition';
|
||||||
|
|
||||||
if ($item['conditions'] instanceof $conditionInstance) {
|
if ($item['conditions'] instanceof $conditionInstance) {
|
||||||
if ($tempConditionsHolder->getName() == $conditionName) {
|
if ($tempConditionsHolder->getName() == $conditionName) {
|
||||||
@@ -465,6 +461,9 @@ public function removeItemCondition($itemId, $conditionName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset indexes
|
||||||
|
$item['conditions'] = array_values($item['conditions']);
|
||||||
|
|
||||||
$this->update($itemId, [
|
$this->update($itemId, [
|
||||||
'conditions' => $item['conditions'],
|
'conditions' => $item['conditions'],
|
||||||
]);
|
]);
|
||||||
@@ -495,7 +494,10 @@ public function clearItemConditions($itemId)
|
|||||||
*/
|
*/
|
||||||
public function clearCartConditions(): void
|
public function clearCartConditions(): void
|
||||||
{
|
{
|
||||||
$this->session->putConditions([]);
|
$this->session->put(
|
||||||
|
$this->sessionKeyCartConditions,
|
||||||
|
[]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -529,10 +531,14 @@ public function getSubTotalWithoutConditions($formatted = true)
|
|||||||
return Helpers::formatValue(floatval($sum), $formatted, $this->config);
|
return Helpers::formatValue(floatval($sum), $formatted, $this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get cart sub total
|
||||||
|
*
|
||||||
|
* @param bool $formatted
|
||||||
|
*/
|
||||||
public function getSubTotal($formatted = true): int|float|string
|
public function getSubTotal($formatted = true): int|float|string
|
||||||
{
|
{
|
||||||
$subTotal = 0.00;
|
$subTotal = 0.00;
|
||||||
|
|
||||||
// add all the items together with conditions applied
|
// add all the items together with conditions applied
|
||||||
$subTotalSum = $this->getContent()->sum(function (ItemCollection $item) {
|
$subTotalSum = $this->getContent()->sum(function (ItemCollection $item) {
|
||||||
return $item->getPriceSumWithConditions(false);
|
return $item->getPriceSumWithConditions(false);
|
||||||
@@ -598,14 +604,10 @@ public function getTotal(): int|float
|
|||||||
return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config);
|
return Helpers::formatValue($subTotal, $this->config['format_numbers'], $this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
$index = 0;
|
$conditionsForTotal->each(function (CartCondition $condition) use ($subTotal, &$total) {
|
||||||
|
$toBeCalculated = $total > 0 ? $total : $subTotal;
|
||||||
$conditionsForTotal->each(function (CartCondition $condition) use ($subTotal, &$total, &$index) {
|
|
||||||
$toBeCalculated = ($index > 0) ? $total : $subTotal;
|
|
||||||
|
|
||||||
$total = $condition->applyCondition($toBeCalculated);
|
$total = $condition->applyCondition($toBeCalculated);
|
||||||
|
|
||||||
$index++;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Helpers::formatValue($total, $this->config['format_numbers'], $this->config);
|
return Helpers::formatValue($total, $this->config['format_numbers'], $this->config);
|
||||||
@@ -653,16 +655,24 @@ public function getTotalQuantity()
|
|||||||
return $count;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getContent(): CartCollection
|
/**
|
||||||
|
* get the cart
|
||||||
|
*
|
||||||
|
* @return CartCollection
|
||||||
|
*/
|
||||||
|
public function getContent()
|
||||||
{
|
{
|
||||||
return (new CartCollection($this->session->getItems()))->transform(function ($item) {
|
return (new CartCollection($this->session->get($this->sessionKeyCartItems)))->reject(function ($item) {
|
||||||
return $item instanceof ItemCollection ? $item : new ItemCollection($item, $this->config);
|
|
||||||
})->reject(function ($item) {
|
|
||||||
return ! ($item instanceof ItemCollection);
|
return ! ($item instanceof ItemCollection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty($test = null)
|
/**
|
||||||
|
* check if cart is empty
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isEmpty()
|
||||||
{
|
{
|
||||||
return $this->getContent()->isEmpty();
|
return $this->getContent()->isEmpty();
|
||||||
}
|
}
|
||||||
@@ -708,7 +718,7 @@ protected function validate($item)
|
|||||||
*/
|
*/
|
||||||
protected function addItem($id, $item)
|
protected function addItem($id, $item)
|
||||||
{
|
{
|
||||||
if ($this->fireEvent('Adding', 'item', $item) === false) {
|
if ($this->fireEvent('adding', $item) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,7 +728,7 @@ protected function addItem($id, $item)
|
|||||||
|
|
||||||
$this->save($cart);
|
$this->save($cart);
|
||||||
|
|
||||||
$this->fireEvent('Added', 'item', $item);
|
$this->fireEvent('added', $item);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -730,7 +740,7 @@ protected function addItem($id, $item)
|
|||||||
*/
|
*/
|
||||||
protected function save($cart)
|
protected function save($cart)
|
||||||
{
|
{
|
||||||
$this->session->putItems($cart);
|
$this->session->put($this->sessionKeyCartItems, $cart);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -738,7 +748,7 @@ protected function save($cart)
|
|||||||
*/
|
*/
|
||||||
protected function saveConditions($conditions)
|
protected function saveConditions($conditions)
|
||||||
{
|
{
|
||||||
$this->session->putConditions($conditions);
|
$this->session->put($this->sessionKeyCartConditions, $conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -756,7 +766,7 @@ protected function itemHasConditions($item)
|
|||||||
return count($item['conditions']) > 0;
|
return count($item['conditions']) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditionInstance = 'Joelwmale\\Cart\\CartCondition';
|
$conditionInstance = 'Wearepixel\\Cart\\CartCondition';
|
||||||
|
|
||||||
if ($item['conditions'] instanceof $conditionInstance) {
|
if ($item['conditions'] instanceof $conditionInstance) {
|
||||||
return true;
|
return true;
|
||||||
@@ -809,19 +819,12 @@ public function setDecimals($decimals)
|
|||||||
$this->decimals = $decimals;
|
$this->decimals = $decimals;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fireEvent($name, $type = null, $data = null)
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function fireEvent($name, $value = [])
|
||||||
{
|
{
|
||||||
$eventData = [
|
return $this->events->dispatch($this->getInstanceName() . '.' . $name, array_values([$value, $this]), true);
|
||||||
'session_key' => $this->session->sessionKey,
|
|
||||||
'session_driver' => $this->session->driver,
|
|
||||||
'session_model' => $this->session->getSessionModel(),
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($type) {
|
|
||||||
$eventData[$type] = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->events->dispatch('LaravelCart.' . $name, array_values([$eventData, $this]), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Joelwmale\Cart\Helpers\Helpers;
|
use Wearepixel\Cart\Helpers\Helpers;
|
||||||
use Joelwmale\Cart\Validators\CartConditionValidator;
|
use Wearepixel\Cart\Validators\CartConditionValidator;
|
||||||
use Joelwmale\Cart\Exceptions\InvalidConditionException;
|
use Wearepixel\Cart\Exceptions\InvalidConditionException;
|
||||||
|
|
||||||
class CartCondition
|
class CartCondition
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
@@ -35,19 +35,24 @@ public function register()
|
|||||||
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'cart');
|
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'cart');
|
||||||
|
|
||||||
$this->app->singleton('cart', function ($app) {
|
$this->app->singleton('cart', function ($app) {
|
||||||
$config = config('cart');
|
$storageClass = config('cart.storage');
|
||||||
$events = $app['events'];
|
$eventsClass = config('cart.events');
|
||||||
|
|
||||||
$storage = $config['driver'] === 'database'
|
$storage = $storageClass ? new $storageClass : $app['session'];
|
||||||
? new $config['storage']['database']['model']
|
$events = $eventsClass ? new $eventsClass : $app['events'];
|
||||||
: $app['session'];
|
$instanceName = 'cart';
|
||||||
|
|
||||||
|
// default session or cart identifier. This will be overridden when calling Cart::session($sessionKey)->add() etc..
|
||||||
|
// like when adding a cart for a specific user name. Session Key can be string or maybe a unique identifier to bind a cart
|
||||||
|
// to a specific user, this can also be a user ID
|
||||||
|
$session_key = '4yTlTDKu3oJOfzD';
|
||||||
|
|
||||||
return new Cart(
|
return new Cart(
|
||||||
$storage,
|
$storage,
|
||||||
$events,
|
$events,
|
||||||
'cart',
|
$instanceName,
|
||||||
session()->getId(),
|
$session_key,
|
||||||
$config
|
config('cart')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
|
||||||
|
|
||||||
class CartSession
|
|
||||||
{
|
|
||||||
private $customStorage;
|
|
||||||
|
|
||||||
public $sessionKey;
|
|
||||||
public $driver;
|
|
||||||
|
|
||||||
private $session;
|
|
||||||
|
|
||||||
private $sessionId = null;
|
|
||||||
private $itemsKey = null;
|
|
||||||
private $conditionsKey = null;
|
|
||||||
|
|
||||||
public function __construct($session, $sessionKey, $config)
|
|
||||||
{
|
|
||||||
$this->sessionKey = $sessionKey;
|
|
||||||
$this->driver = $config['driver'];
|
|
||||||
|
|
||||||
if ($this->driver === 'session') {
|
|
||||||
$this->customStorage = false;
|
|
||||||
$this->session = $session;
|
|
||||||
$this->itemsKey = $this->sessionKey . '_cart_items';
|
|
||||||
$this->conditionsKey = $this->sessionKey . '_cart_conditions';
|
|
||||||
} elseif ($this->driver === 'database') {
|
|
||||||
$this->customStorage = true;
|
|
||||||
|
|
||||||
$this->sessionId = $config['storage']['database']['id'];
|
|
||||||
$this->itemsKey = $config['storage']['database']['items'];
|
|
||||||
$this->conditionsKey = $config['storage']['database']['conditions'];
|
|
||||||
|
|
||||||
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
|
|
||||||
|
|
||||||
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
|
|
||||||
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isInternalSession($session)
|
|
||||||
{
|
|
||||||
return in_array(is_object($session) ? get_class($session) : $session, [
|
|
||||||
'Illuminate\Session\SessionManager',
|
|
||||||
'Joelwmale\Cart\Tests\Helpers\SessionMock',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function has($key)
|
|
||||||
{
|
|
||||||
return $this->session->has($key) ?? $this->session;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getItems()
|
|
||||||
{
|
|
||||||
return $this->driver == 'session' ? $this->session->get($this->itemsKey) : $this->session[$this->itemsKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getConditions()
|
|
||||||
{
|
|
||||||
return $this->driver == 'session' ? $this->session->get($this->conditionsKey) : $this->session[$this->conditionsKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function putItems($value)
|
|
||||||
{
|
|
||||||
if ($this->driver == 'session') {
|
|
||||||
return $this->session->put($this->itemsKey, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert any conditions to arrays
|
|
||||||
$value = is_array($value) ? $value : $value->toArray();
|
|
||||||
|
|
||||||
foreach ($value as $key => $item) {
|
|
||||||
if ($item['conditions']) {
|
|
||||||
if (is_array($item['conditions'])) {
|
|
||||||
$value[$key]['conditions'] = collect($item['conditions'])->map(function ($condition) {
|
|
||||||
return $condition->toArray();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
$value[$key]['conditions'] = $item['conditions']->toArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->session[$this->itemsKey] = $value;
|
|
||||||
$this->session->save();
|
|
||||||
|
|
||||||
return $this->session;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function putConditions($value)
|
|
||||||
{
|
|
||||||
if ($this->driver == 'session') {
|
|
||||||
return $this->session->put($this->conditionsKey, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty($value)) {
|
|
||||||
$value = $value->mapWithKeys(function ($condition) {
|
|
||||||
return [$condition->getName() => $condition->toArray()];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->session[$this->conditionsKey] = $value;
|
|
||||||
$this->session->save();
|
|
||||||
|
|
||||||
return $this->session;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clearItems()
|
|
||||||
{
|
|
||||||
if ($this->driver == 'session') {
|
|
||||||
$this->session->forget($this->itemsKey);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->session[$this->itemsKey] = [];
|
|
||||||
|
|
||||||
return $this->session->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clear()
|
|
||||||
{
|
|
||||||
if ($this->driver == 'session') {
|
|
||||||
$this->session->forget($this->itemsKey);
|
|
||||||
$this->session->forget($this->conditionsKey);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$deleted = $this->session->delete();
|
|
||||||
|
|
||||||
$this->session = [
|
|
||||||
$this->itemsKey => [],
|
|
||||||
$this->conditionsKey => [],
|
|
||||||
];
|
|
||||||
|
|
||||||
return $deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSessionModel()
|
|
||||||
{
|
|
||||||
return $this->session;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Exceptions;
|
namespace Wearepixel\Cart\Exceptions;
|
||||||
|
|
||||||
class InvalidConditionException extends \Exception {}
|
class InvalidConditionException extends \Exception {}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Exceptions;
|
namespace Wearepixel\Cart\Exceptions;
|
||||||
|
|
||||||
class InvalidItemException extends \Exception {}
|
class InvalidItemException extends \Exception {}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Exceptions;
|
namespace Wearepixel\Cart\Exceptions;
|
||||||
|
|
||||||
class UnknownModelException extends \Exception {}
|
class UnknownModelException extends \Exception {}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Facades;
|
namespace Wearepixel\Cart\Facades;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Helpers;
|
namespace Wearepixel\Cart\Helpers;
|
||||||
|
|
||||||
class Helpers
|
class Helpers
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
|||||||
+7
-37
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart;
|
namespace Wearepixel\Cart;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Joelwmale\Cart\Helpers\Helpers;
|
use Wearepixel\Cart\Helpers\Helpers;
|
||||||
|
|
||||||
class ItemCollection extends Collection
|
class ItemCollection extends Collection
|
||||||
{
|
{
|
||||||
@@ -72,7 +72,7 @@ public function hasConditions()
|
|||||||
if (is_array($this['conditions'])) {
|
if (is_array($this['conditions'])) {
|
||||||
return count($this['conditions']) > 0;
|
return count($this['conditions']) > 0;
|
||||||
}
|
}
|
||||||
$conditionInstance = 'Joelwmale\\Cart\\CartCondition';
|
$conditionInstance = 'Wearepixel\\Cart\\CartCondition';
|
||||||
if ($this['conditions'] instanceof $conditionInstance) {
|
if ($this['conditions'] instanceof $conditionInstance) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -91,37 +91,7 @@ public function getConditions()
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$conditionsArray = [];
|
return $this['conditions'];
|
||||||
|
|
||||||
$conditions = $this['conditions'];
|
|
||||||
|
|
||||||
// check if we're already an array of CartConditions
|
|
||||||
foreach ($conditions as $key => $condition) {
|
|
||||||
if ($condition instanceof CartCondition) {
|
|
||||||
return $conditions;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$hasSubArray = false;
|
|
||||||
|
|
||||||
foreach ($conditions as $key => $condition) {
|
|
||||||
if (is_array($condition) && ! $condition instanceof CartCondition) {
|
|
||||||
$hasSubArray = true;
|
|
||||||
$conditionsArray[] = new CartCondition($condition);
|
|
||||||
} elseif ($condition instanceof CartCondition) {
|
|
||||||
$conditionsArray[] = $condition;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! $hasSubArray) {
|
|
||||||
if ($conditions instanceof CartCondition) {
|
|
||||||
return $conditions;
|
|
||||||
}
|
|
||||||
|
|
||||||
$conditionsArray[] = new CartCondition($conditions);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $conditionsArray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,14 +108,14 @@ public function getPriceWithConditions($formatted = true)
|
|||||||
$processed = 0;
|
$processed = 0;
|
||||||
|
|
||||||
if ($this->hasConditions()) {
|
if ($this->hasConditions()) {
|
||||||
if (is_array($this->getConditions())) {
|
if (is_array($this->conditions)) {
|
||||||
foreach ($this->getConditions() as $condition) {
|
foreach ($this->conditions as $condition) {
|
||||||
($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
|
($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
|
||||||
$newPrice = $condition->applyCondition($toBeCalculated);
|
$newPrice = $condition->applyCondition($toBeCalculated);
|
||||||
$processed++;
|
$processed++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$newPrice = $this->getConditions()->applyCondition($originalPrice);
|
$newPrice = $this['conditions']->applyCondition($originalPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Helpers::formatValue($newPrice, $formatted, $this->config);
|
return Helpers::formatValue($newPrice, $formatted, $this->config);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Validators;
|
namespace Wearepixel\Cart\Validators;
|
||||||
|
|
||||||
class CartConditionValidator extends Validator {}
|
class CartConditionValidator extends Validator {}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Validators;
|
namespace Wearepixel\Cart\Validators;
|
||||||
|
|
||||||
class CartItemValidator extends Validator {}
|
class CartItemValidator extends Validator {}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Validators;
|
namespace Wearepixel\Cart\Validators;
|
||||||
|
|
||||||
use Illuminate\Validation\Factory;
|
use Illuminate\Validation\Factory;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
|||||||
@@ -4,5 +4,6 @@
|
|||||||
'format_numbers' => true,
|
'format_numbers' => true,
|
||||||
'decimals' => 2,
|
'decimals' => 2,
|
||||||
'dec_point' => '.',
|
'dec_point' => '.',
|
||||||
'driver' => 'session',
|
'thousands_sep' => ',',
|
||||||
|
'use_cents' => false,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
return [
|
|
||||||
'format_numbers' => true,
|
|
||||||
'decimals' => 2,
|
|
||||||
'round_mode' => 'up',
|
|
||||||
'driver' => 'database',
|
|
||||||
'storage' => [
|
|
||||||
'session',
|
|
||||||
'database' => [
|
|
||||||
'model' => \Joelwmale\Cart\Tests\Helpers\MockCartModel::class,
|
|
||||||
'id' => 'session_id',
|
|
||||||
'items' => 'items',
|
|
||||||
'conditions' => 'conditions',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
@@ -4,5 +4,4 @@
|
|||||||
'format_numbers' => true,
|
'format_numbers' => true,
|
||||||
'decimals' => 2,
|
'decimals' => 2,
|
||||||
'round_mode' => 'up',
|
'round_mode' => 'up',
|
||||||
'driver' => 'session',
|
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -4,5 +4,5 @@
|
|||||||
'format_numbers' => false,
|
'format_numbers' => false,
|
||||||
'decimals' => 3,
|
'decimals' => 3,
|
||||||
'dec_point' => ',',
|
'dec_point' => ',',
|
||||||
'driver' => 'session',
|
'thousands_sep' => '.',
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Tests\Helpers;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
class MockCartModel extends Model
|
|
||||||
{
|
|
||||||
protected $fillable = [
|
|
||||||
'session_id',
|
|
||||||
'items',
|
|
||||||
'conditions',
|
|
||||||
];
|
|
||||||
|
|
||||||
public $timestamps = false;
|
|
||||||
|
|
||||||
protected $casts = [
|
|
||||||
'items' => 'array',
|
|
||||||
'conditions' => 'array',
|
|
||||||
];
|
|
||||||
|
|
||||||
protected $table = 'carts';
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Tests\Helpers;
|
namespace Wearepixel\Cart\Tests\Helpers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Database\ModelIdentifier;
|
use Illuminate\Contracts\Database\ModelIdentifier;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Joelwmale\Cart\Tests\Helpers;
|
namespace Wearepixel\Cart\Tests\Helpers;
|
||||||
|
|
||||||
class SessionMock
|
class SessionMock
|
||||||
{
|
{
|
||||||
@@ -20,9 +20,4 @@ public function put($key, $value)
|
|||||||
{
|
{
|
||||||
$this->session[$key] = $value;
|
$this->session[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function forget($key)
|
|
||||||
{
|
|
||||||
unset($this->session[$key]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-2
@@ -1,5 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Tests\TestCase;
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Test Case
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
|
||||||
|
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
|
||||||
|
| need to change it using the "pest()" function to bind a different classes or traits.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
uses(TestCase::class)->in(__DIR__);
|
// pest()->extend(Tests\TestCase::class)->in('Feature');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Expectations
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| When you're writing tests, you often need to check that values meet certain conditions. The
|
||||||
|
| "expect()" function gives you access to a set of "expectations" methods that you can use
|
||||||
|
| to assert different things. Of course, you may extend the Expectation API at any time.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
expect()->extend('toBeOne', function () {
|
||||||
|
return $this->toBe(1);
|
||||||
|
});
|
||||||
|
|||||||
+2
-44
@@ -1,52 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
use Illuminate\Events\Dispatcher;
|
|
||||||
use Illuminate\Container\Container;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Database\Capsule\Manager as DB;
|
|
||||||
use PHPUnit\Framework\TestCase as BaseTestCase;
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
||||||
|
|
||||||
class TestCase extends BaseTestCase
|
abstract class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
protected DB $db;
|
//
|
||||||
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
// Create a Laravel container
|
|
||||||
$container = Container::getInstance();
|
|
||||||
$dispatcher = new Dispatcher;
|
|
||||||
|
|
||||||
// Setup Capsule (Eloquent ORM)
|
|
||||||
$this->db = new DB($container);
|
|
||||||
$this->db->addConnection([
|
|
||||||
'driver' => 'sqlite',
|
|
||||||
'database' => ':memory:',
|
|
||||||
'prefix' => '',
|
|
||||||
]);
|
|
||||||
$this->db->setAsGlobal();
|
|
||||||
$this->db->bootEloquent();
|
|
||||||
$this->db->getConnection()->setEventDispatcher($dispatcher);
|
|
||||||
|
|
||||||
// Run migrations (optional, for testing models)
|
|
||||||
$this->migrateDatabase();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function migrateDatabase()
|
|
||||||
{
|
|
||||||
$schema = $this->db->schema();
|
|
||||||
|
|
||||||
$schema->create('carts', function (Blueprint $table) {
|
|
||||||
$table->id();
|
|
||||||
$table->string('session_id');
|
|
||||||
$table->text('items');
|
|
||||||
$table->text('conditions');
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\CartCondition;
|
use Wearepixel\Cart\CartCondition;
|
||||||
use Joelwmale\Cart\CartConditionCollection;
|
use Wearepixel\Cart\CartConditionCollection;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
@@ -119,7 +119,7 @@
|
|||||||
|
|
||||||
// now we should have only 1 condition left on that item
|
// 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())->toHaveCount(1, 'Item should have one condition left');
|
||||||
expect($this->cart->getConditions()->first()->getName())->toEqual('Mother\'s Day Gift Pack');
|
expect($this->cart->getConditions()[0]->getName())->toEqual('Mother\'s Day Gift Pack');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1065,7 +1065,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
||||||
|
|
||||||
$this->cart->clearItems();
|
$this->cart->clear();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -1099,7 +1099,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
||||||
|
|
||||||
$this->cart->clearItems();
|
$this->cart->clear();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -1201,7 +1201,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getConditions(active: true))->toHaveCount(0, 'Cart should have 0 conditions');
|
expect($this->cart->getConditions(active: true))->toHaveCount(0, 'Cart should have 0 conditions');
|
||||||
|
|
||||||
$this->cart->clearItems();
|
$this->cart->clear();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -1310,7 +1310,7 @@
|
|||||||
|
|
||||||
// now we should have only 1 condition left on that item
|
// 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'])->toHaveCount(1, 'Item should have one condition left');
|
||||||
expect($this->cart->get(1)['conditions'][1]->getName())->toEqual('Mother\'s Day Gift Pack');
|
expect($this->cart->get(1)['conditions'][0]->getName())->toEqual('Mother\'s Day Gift Pack');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can add item with multiple negative item conditions', function () {
|
test('can add item with multiple negative item conditions', function () {
|
||||||
|
|||||||
+10
-10
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\Tests\Helpers\MockProduct;
|
use Wearepixel\Cart\Tests\Helpers\MockProduct;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
@@ -149,7 +149,7 @@
|
|||||||
|
|
||||||
$item = $this->cart->get(1);
|
$item = $this->cart->get(1);
|
||||||
|
|
||||||
expect($item->attributes)->toBeInstanceOf('Joelwmale\\Cart\\ItemAttributeCollection');
|
expect($item->attributes)->toBeInstanceOf('Wearepixel\\Cart\\ItemAttributeCollection');
|
||||||
|
|
||||||
// now lets update the item with its new attributes
|
// now lets update the item with its new attributes
|
||||||
// when we get that item from cart, it should still be an instance of ItemAttributeCollection
|
// when we get that item from cart, it should still be an instance of ItemAttributeCollection
|
||||||
@@ -162,7 +162,7 @@
|
|||||||
|
|
||||||
$this->cart->update(1, $updatedItem);
|
$this->cart->update(1, $updatedItem);
|
||||||
|
|
||||||
expect($item->attributes)->toBeInstanceOf('Joelwmale\\Cart\\ItemAttributeCollection');
|
expect($item->attributes)->toBeInstanceOf('Wearepixel\\Cart\\ItemAttributeCollection');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('item orders are preserved by the order they are added', function () {
|
test('item orders are preserved by the order they are added', function () {
|
||||||
@@ -298,19 +298,19 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should throw an exception when no quantity is provided', function () {
|
test('should throw an exception when no quantity is provided', function () {
|
||||||
$this->expectException('Joelwmale\Cart\Exceptions\InvalidItemException');
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
$this->expectExceptionMessage('The quantity must be at least 0.1.');
|
$this->expectExceptionMessage('The quantity must be at least 0.1.');
|
||||||
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw an exception when no id is provided', function () {
|
test('should throw an exception when no id is provided', function () {
|
||||||
$this->expectException('Joelwmale\Cart\Exceptions\InvalidItemException');
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
$this->expectExceptionMessage('The id field is required.');
|
$this->expectExceptionMessage('The id field is required.');
|
||||||
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw an exception when no name is provided', function () {
|
test('should throw an exception when no name is provided', function () {
|
||||||
$this->expectException('Joelwmale\Cart\Exceptions\InvalidItemException');
|
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||||
$this->expectExceptionMessage('The name field is required.');
|
$this->expectExceptionMessage('The name field is required.');
|
||||||
$this->cart->add(523, '', 100.99, 2, []);
|
$this->cart->add(523, '', 100.99, 2, []);
|
||||||
});
|
});
|
||||||
@@ -646,7 +646,7 @@
|
|||||||
expect($this->cart->getContent()->count())->toEqual(1, 'Cart should have 1 item on it');
|
expect($this->cart->getContent()->count())->toEqual(1, 'Cart should have 1 item on it');
|
||||||
expect($this->cart->getContent()->first()['id'])->toEqual(456, 'The first content must have ID of 456');
|
expect($this->cart->getContent()->first()['id'])->toEqual(456, 'The first content must have ID of 456');
|
||||||
expect($this->cart->getContent()->first()['name'])->toEqual('Sample Item', 'The first content must have name of "Sample Item"');
|
expect($this->cart->getContent()->first()['name'])->toEqual('Sample Item', 'The first content must have name of "Sample Item"');
|
||||||
expect($addedItem->model)->toBeInstanceOf('Joelwmale\Cart\Tests\Helpers\MockProduct');
|
expect($addedItem->model)->toBeInstanceOf('Wearepixel\Cart\Tests\Helpers\MockProduct');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cart can add items with multidimensional array with associated model', function () {
|
test('cart can add items with multidimensional array with associated model', function () {
|
||||||
@@ -681,7 +681,7 @@
|
|||||||
|
|
||||||
$content = $this->cart->getContent();
|
$content = $this->cart->getContent();
|
||||||
foreach ($content as $item) {
|
foreach ($content as $item) {
|
||||||
expect($item->model)->toBeInstanceOf('Joelwmale\Cart\Tests\Helpers\MockProduct');
|
expect($item->model)->toBeInstanceOf('Wearepixel\Cart\Tests\Helpers\MockProduct');
|
||||||
}
|
}
|
||||||
|
|
||||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {});
|
beforeEach(function () {});
|
||||||
|
|
||||||
@@ -9,9 +9,9 @@
|
|||||||
Mockery::close();
|
Mockery::close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('event cart Created', function () {
|
test('event cart created', function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
|
|
||||||
$cart = new Cart(
|
$cart = new Cart(
|
||||||
new SessionMock,
|
new SessionMock,
|
||||||
@@ -26,9 +26,9 @@
|
|||||||
|
|
||||||
test('event cart adding', function () {
|
test('event cart adding', function () {
|
||||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Adding', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.adding', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Added', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.added', Mockery::type('array'), true);
|
||||||
|
|
||||||
$cart = new Cart(
|
$cart = new Cart(
|
||||||
new SessionMock,
|
new SessionMock,
|
||||||
@@ -45,9 +45,9 @@
|
|||||||
|
|
||||||
test('event cart adding multiple times', function () {
|
test('event cart adding multiple times', function () {
|
||||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(2)->with('LaravelCart.Adding', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(2)->with('cart.adding', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(2)->with('LaravelCart.Added', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(2)->with('cart.added', Mockery::type('array'), true);
|
||||||
|
|
||||||
$cart = new Cart(
|
$cart = new Cart(
|
||||||
new SessionMock,
|
new SessionMock,
|
||||||
@@ -65,9 +65,9 @@
|
|||||||
|
|
||||||
test('event cart adding multiple times scenario two', function () {
|
test('event cart adding multiple times scenario two', function () {
|
||||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Adding', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Added', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||||
|
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
@@ -108,11 +108,11 @@
|
|||||||
|
|
||||||
test('event cart remove item', function () {
|
test('event cart remove item', function () {
|
||||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Adding', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Added', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(1)->with('LaravelCart.Removing', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(1)->with('cart.removing', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(1)->with('LaravelCart.Removed', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(1)->with('cart.removed', Mockery::type('array'), true);
|
||||||
|
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
@@ -155,11 +155,11 @@
|
|||||||
|
|
||||||
test('event cart clear', function () {
|
test('event cart clear', function () {
|
||||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Created', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Adding', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->times(3)->with('LaravelCart.Added', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Clearing', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.clearing', Mockery::type('array'), true);
|
||||||
$events->shouldReceive('dispatch')->once()->with('LaravelCart.Cleared', Mockery::type('array'), true);
|
$events->shouldReceive('dispatch')->once()->with('cart.cleared', Mockery::type('array'), true);
|
||||||
|
|
||||||
$items = [
|
$items = [
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
|
|||||||
@@ -1,126 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
|
||||||
use Joelwmale\Cart\CartCondition;
|
|
||||||
use Joelwmale\Cart\Tests\Helpers\MockCartModel;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
|
||||||
$events->shouldReceive('dispatch');
|
|
||||||
|
|
||||||
$storage = new MockCartModel;
|
|
||||||
|
|
||||||
$this->cart = new Cart(
|
|
||||||
$storage,
|
|
||||||
$events,
|
|
||||||
'cart',
|
|
||||||
'SAMPLESESSIONKEY',
|
|
||||||
require (__DIR__ . '/../Helpers/ConfigDatabaseMock.php')
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function () {
|
|
||||||
Mockery::close();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('database cart', function () {
|
|
||||||
test('can add an item', function () {
|
|
||||||
$this->cart->add(1, 'Sample Item', 100.99, 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(1, 'Item added has ID of 1 so first content ID should be 1');
|
|
||||||
expect($this->cart->getContent()->first()['price'])->toEqual(100.99, 'Item added has price of 100.99 so first content price should be 100.99');
|
|
||||||
|
|
||||||
expect(MockCartModel::all()->count())->toEqual(1, 'The carts database table should have 1 row');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can add and remove an item', function () {
|
|
||||||
$this->cart->add(1, 'Sample Item', 100.99, 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(1, 'Item added has ID of 1 so first content ID should be 1');
|
|
||||||
expect($this->cart->getContent()->first()['price'])->toEqual(100.99, 'Item added has price of 100.99 so first content price should be 100.99');
|
|
||||||
|
|
||||||
expect(MockCartModel::all()->count())->toEqual(1, 'The carts database table should have 1 row');
|
|
||||||
|
|
||||||
$this->cart->remove(1);
|
|
||||||
|
|
||||||
$cartModel = MockCartModel::first();
|
|
||||||
|
|
||||||
expect($this->cart->isEmpty())->toBeTrue('Cart should be empty');
|
|
||||||
expect($this->cart->getContent()->count())->toEqual(0, 'Cart content should be 0');
|
|
||||||
expect($cartModel->items)->toEqual([], 'The carts database table should have 0 row');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can add an item with an item level condition', function () {
|
|
||||||
$saleCondition = new CartCondition([
|
|
||||||
'name' => '5% Discount',
|
|
||||||
'type' => 'sale',
|
|
||||||
'value' => '-5%',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$item = [
|
|
||||||
'id' => 1,
|
|
||||||
'name' => 'Chicken Curry',
|
|
||||||
'price' => 18.95,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
'conditions' => $saleCondition,
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->cart->add($item);
|
|
||||||
|
|
||||||
expect($this->cart->get(1)->getPriceSum())->toEqual(18.95);
|
|
||||||
expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(18);
|
|
||||||
expect($this->cart->getSubTotal())->toEqual(18, 'Cart should have subtotal of 18');
|
|
||||||
expect($this->cart->getTotal())->toEqual(18, 'Cart should have total of 18');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can add an item with multiple item level conditions', function () {
|
|
||||||
$saleCondition = new CartCondition([
|
|
||||||
'name' => '5% Discount',
|
|
||||||
'type' => 'sale',
|
|
||||||
'value' => '-5%',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$gstCondition = new CartCondition([
|
|
||||||
'name' => 'GST',
|
|
||||||
'type' => 'gst',
|
|
||||||
'value' => '2.5%',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$item = [
|
|
||||||
'id' => 1,
|
|
||||||
'name' => 'Chicken Curry',
|
|
||||||
'price' => 18.95,
|
|
||||||
'quantity' => 1,
|
|
||||||
'attributes' => [],
|
|
||||||
'conditions' => [$saleCondition, $gstCondition],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->cart->add($item);
|
|
||||||
|
|
||||||
expect($this->cart->get(1)->getPriceSum())->toEqual(18.95);
|
|
||||||
expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(18.45);
|
|
||||||
expect($this->cart->getSubTotal())->toEqual(18.45, 'Cart should have subtotal of 18.45');
|
|
||||||
expect($this->cart->getTotal())->toEqual(18.45, 'Cart should have total of 18.45');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can clear a simple cart', function () {
|
|
||||||
$this->cart->add(1, 'Sample Item', 100.99, 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(1, 'Item added has ID of 1 so first content ID should be 1');
|
|
||||||
expect($this->cart->getContent()->first()['price'])->toEqual(100.99, 'Item added has price of 100.99 so first content price should be 100.99');
|
|
||||||
expect(MockCartModel::all()->count())->toEqual(1, 'The carts database table should have 1 row');
|
|
||||||
|
|
||||||
$this->cart->clear();
|
|
||||||
|
|
||||||
expect($this->cart->isEmpty())->toBeTrue('Cart should be empty');
|
|
||||||
expect($this->cart->getContent()->count())->toEqual(0, 'Cart content should be 0');
|
|
||||||
expect(MockCartModel::all()->count())->toEqual(0, 'The carts database table should have 0 rows');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\CartCondition;
|
use Wearepixel\Cart\CartCondition;
|
||||||
use Joelwmale\Cart\Tests\Helpers\MockProduct;
|
use Wearepixel\Cart\Tests\Helpers\MockProduct;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
@@ -47,7 +47,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('item get conditions with conditions', function () {
|
test('item get conditions with conditions', function () {
|
||||||
$itemCondition1 = new \Joelwmale\Cart\CartCondition([
|
$itemCondition1 = new \Wearepixel\Cart\CartCondition([
|
||||||
'name' => 'SALE 5%',
|
'name' => 'SALE 5%',
|
||||||
'type' => 'sale',
|
'type' => 'sale',
|
||||||
'target' => 'item',
|
'target' => 'item',
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('it will throw an exception when a non existing model is being associated', function () {
|
test('it will throw an exception when a non existing model is being associated', function () {
|
||||||
$this->expectException(\Joelwmale\Cart\Exceptions\UnknownModelException::class);
|
$this->expectException(\Wearepixel\Cart\Exceptions\UnknownModelException::class);
|
||||||
$this->expectExceptionMessage('The supplied model SomeModel does not exist.');
|
$this->expectExceptionMessage('The supplied model SomeModel does not exist.');
|
||||||
|
|
||||||
$this->cart->add(1, 'Test item', 1, 10.00)->associate('SomeModel');
|
$this->cart->add(1, 'Test item', 1, 10.00)->associate('SomeModel');
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Joelwmale\Cart\Cart;
|
use Wearepixel\Cart\Cart;
|
||||||
use Joelwmale\Cart\Tests\Helpers\SessionMock;
|
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||||
|
|||||||
Reference in New Issue
Block a user