2017 © Pedro Peláez
 

yii2-extension yii2-cart

Shopping cart for Yii2

image

devanych/yii2-cart

Shopping cart for Yii2

  • Monday, July 2, 2018
  • by devanych
  • Repository
  • 1 Watchers
  • 1 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Yii2 shopping cart

This extension adds shopping cart for Yii framework 2.0, (*1)

Guide with a detailed description in Russian language here., (*2)

Installation

The preferred way to install this extension is through Composer, (*3)

Either run, (*4)

php composer.phar require devanych/yii2-cart "*"

or add, (*5)

devanych/yii2-cart: "*"

to the require section of your composer.json file., (*6)

Configuration

Configure the cart component (default values are shown):, (*7)

return [
    //...
    'components' => [
        //...
        'cart' => [
            'class' => 'devanych\cart\Cart',
            'storageClass' => 'devanych\cart\storage\SessionStorage',
            'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
            'params' => [
                'key' => 'cart',
                'expire' => 604800,
                'productClass' => 'app\model\Product',
                'productFieldId' => 'id',
                'productFieldPrice' => 'price',
            ],
        ],
    ]
    //...
];

In addition to devanych\cart\storage\SessionStorage, there is also devanych\cart\storage\CookieStorage and devanych\cart\storage\DbSessionStorage. It is possible to create your own storage, you need to implement the interface devanych\cart\storage\StorageInterface., (*8)

DbSessionStorage uses SessionStorage for unauthorized users and database for authorized., (*9)

If you use the devanych\cart\storage\DbSessionStorage as storageClass then you need to apply the following migration:, (*10)

php yii migrate --migrationPath=@vendor/devanych/yii2-cart/migrations

devanych\cart\calculators\SimpleCalculator produces the usual calculation of the total cost and total quantity of items in the cart. If you need to make a calculation with discounts or something else, you can create your own calculator by implementing the interface devanych\cart\calculators\CalculatorInterface., (*11)

Setting up the params array:, (*12)

  • key - For Session and Cookie., (*13)

  • expire - Cookie life time., (*14)

  • productClass - Product class is an ActiveRecord model., (*15)

  • productFieldId - Name of the product model id field., (*16)

  • productFieldPrice - Name of the product model price field., (*17)

Supporting multiple shopping carts to same website:

//...
'cart' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\SessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'cart',
        'expire' => 604800,
        'productClass' => 'app\model\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
'favorite' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\DbSessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'favorite',
        'expire' => 604800,
        'productClass' => 'app\models\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
//...

Usage

You can get the shopping cart component anywhere in the app using Yii::$app->cart., (*18)

Using cart:, (*19)

// Product is an AR model
$product = Product::findOne(1);

// Get component of the cart
$cart = \Yii::$app->cart;

// Add an item to the cart
$cart->add($product, $quantity);

// Adding item quantity in the cart
$cart->plus($product->id, $quantity);

// Change item quantity in the cart
$cart->change($product->id, $quantity);

// Removes an items from the cart
$cart->remove($product->id);

// Removes all items from the cart
$cart->clear();

// Get all items from the cart
$cart->getItems();

// Get an item from the cart
$cart->getItem($product->id);

// Get ids array all items from the cart
$cart->getItemIds();

// Get total cost all items from the cart
$cart->getTotalCost();

// Get total count all items from the cart
$cart->getTotalCount();

Using cart items:

// Product is an AR model
$product = Product::findOne(1);

// Get component of the cart
$cart = \Yii::$app->cart;

// Get an item from the cart
$item = $cart->getItem($product->id);

// Get the id of the item
$item->getId();

// Get the price of the item
$item->getPrice();

// Get the product, AR model
$item->getProduct();

// Get the cost of the item
$item->getCost();

// Get the quantity of the item
$item->getQuantity();

// Set the quantity of the item
$item->setQuantity($quantity);

By using method getProduct(), you have access to all the properties and methods of the product., (*20)

$product = $item->getProduct();

echo $product->name;

The Versions

02/07 2018

dev-master

9999999-dev

Shopping cart for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 yii2-extension yii2-cart yii2-shopping-cart

02/07 2018

v1.2.1

1.2.1.0

Shopping cart for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 yii2-extension yii2-cart yii2-shopping-cart

26/06 2018

v1.1.1

1.1.1.0

Shopping cart for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 yii2-extension yii2-cart yii2-shopping-cart

08/06 2018

v1.1

1.1.0.0

Shopping cart for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii2 yii2-extension yii2-cart yii2-shopping-cart

06/06 2018

v1.0

1.0.0.0

Shopping cart for Yii2

  Sources   Download

BSD-3-Clause

The Requires

 

yii2 yii2-extension yii2-cart yii2-shopping-cart