Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yii2mod/yii2-cart
Yii2 shopping cart
https://github.com/yii2mod/yii2-cart
yii2 yii2-cart yii2-extension yii2-shopping-cart
Last synced: 3 days ago
JSON representation
Yii2 shopping cart
- Host: GitHub
- URL: https://github.com/yii2mod/yii2-cart
- Owner: yii2mod
- License: mit
- Created: 2014-12-05T14:34:57.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2021-03-09T08:13:24.000Z (over 3 years ago)
- Last Synced: 2024-10-30T01:51:36.815Z (17 days ago)
- Topics: yii2, yii2-cart, yii2-extension, yii2-shopping-cart
- Language: PHP
- Size: 50.8 KB
- Stars: 119
- Watchers: 29
- Forks: 47
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Yii2 Shopping Cart Extension
This extension adds shopping cart for Yii framework 2.0
[![Latest Stable Version](https://poser.pugx.org/yii2mod/yii2-cart/v/stable)](https://packagist.org/packages/yii2mod/yii2-cart) [![Total Downloads](https://poser.pugx.org/yii2mod/yii2-cart/downloads)](https://packagist.org/packages/yii2mod/yii2-cart) [![License](https://poser.pugx.org/yii2mod/yii2-cart/license)](https://packagist.org/packages/yii2mod/yii2-cart)
[![Build Status](https://travis-ci.org/yii2mod/yii2-cart.svg?branch=master)](https://travis-ci.org/yii2mod/yii2-cart)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yii2mod/yii2-cart/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yii2mod/yii2-cart/?branch=master)## Support us
Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod).
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist yii2mod/yii2-cart "*"
```or add
```
"yii2mod/yii2-cart": "*"
```to the require section of your `composer.json` file.
### Configuration
1) Configure the ```cart``` component:
```php
return [
//....
'components' => [
'cart' => [
'class' => 'yii2mod\cart\Cart',
// you can change default storage class as following:
'storageClass' => [
'class' => 'yii2mod\cart\storage\DatabaseStorage',
// you can also override some properties
'deleteIfEmpty' => true
]
],
]
];
```
2) Create the Product Model that implements an `CartItemInterface`:
```php
class ProductModel extends ActiveRecord implements CartItemInterface
{public function getPrice()
{
return $this->price;
}public function getLabel()
{
return $this->name;
}public function getUniqueId()
{
return $this->id;
}
}
```> If you use the yii2mod\cart\storage\DatabaseStorage as ```storageClass``` then you need to apply the following migration:
```php
php yii migrate --migrationPath=@vendor/yii2mod/yii2-cart/migrations
```### Using the shopping cart
Operations with the shopping cart are very straightforward when using a models that implement one of the two cart interfaces.
The cart object can be accessed under `\Yii::$app->cart` and can be overridden in configuration if you need to customize it.
```php
// access the cart from "cart" subcomponent
$cart = \Yii::$app->cart;// Product is an AR model implementing CartProductInterface
$product = Product::findOne(1);// add an item to the cart
$cart->add($product);// returns the sum of all 'vat' attributes (or return values of getVat()) from all models in the cart.
$totalVat = $cart->getAttributeTotal('vat');// clear the cart
$cart->clear();```
#### View Cart Items
You can use the `CartGrid` widget for generate table with cart items as following:
```php
[
'id',
'label',
'price'
]
]); ?>```
#### Items in the cart
Products/items that are added to the cart are serialized/unserialized when saving and loading data from cart storage.
If you are using Active Record models as products/discounts, make sure that you are omitting any unnecessary references from
the serialized data to keep it compact.```php
// get all items from the cart
$items = $cart->getItems();// get only products
$items = $cart->getItems(Cart::ITEM_PRODUCT);// loop through cart items
foreach ($items as $item) {
// access any attribute/method from the model
var_dump($item->getAttributes());// remove an item from the cart by its ID
$cart->remove($item->uniqueId)
}
```#### Get Number of Products in Cart
You can use the `getCount` to get count as this example:
```php
// get count of all products in cart:
$items = $cart->getCount();// get count of Specific Item Type:
$items = $cart->getCount(Cart::ITEM_PRODUCT);
```