Git Product home page Git Product logo

yii2-cart's Introduction

Shopping cart for Yii 2

This extension is improvisation of omnilight/yii2-shopping-cart. It's add shopping cart systems for Yii framework 2.0. It have feature for save to some medium, they are session (default), cookie, localStorage, database, and multiple storage.

What's is the meaning of the multiple storage? It's feature that can handle two storage where it will save cart data to storage 1 if user is guest, and save to storage 2 if user is logged user.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist hscstudio/yii2-cart "*"

or add

"hscstudio/yii2-cart": "*"

to the require section of your composer.json.

If You plan to save cart data into database, so You should create table cart.

CREATE TABLE `cart` (
  `id` varchar(255) NOT NULL,
  `user_id` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `value` text NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `cart`
  ADD PRIMARY KEY (`id`);

or use migration

yii migrate --migrationPath=@hscstudio/cart/migrations

How to use

In your model:

class Product extends ActiveRecord implements ItemInterface
{
    use ItemTrait;

    public function getPrice()
    {
        return $this->price;
    }

    public function getId()
    {
        return $this->id;
    }
}

In your controller:

	public function actionCreate($id)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->create($product);
            $this->redirect(['index']);
        }
    }
    public function actionIndex()
    {
        $cart = \Yii::$app->cart;
        $products = $cart->getItems();
        $total = $cart->getCost();
        return $this->render('index', [
            'products' => $products,
            'total' => $total,
        ]);
    }
    public function actionDelete($id)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->delete($product);
            $this->redirect(['index']);
        }
    }
    public function actionUpdate($id, $quantity)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->update($product, $quantity);
            $this->redirect(['index']);
        }
    }
	
	public function actionCheckout(){
		\Yii::$app->cart->checkOut(false);
		$this->redirect(['index']);
	}

Also you can use cart as global application component:

[
    'components' => [
        'cart' => [
			'class' => 'hscstudio\cart\Cart',
		],
    ]
]

Possible values of storage are

  • hscstudio\cart\CookieStorage
  • hscstudio\cart\SessionStorage
  • hscstudio\cart\LocalStorage
  • hscstudio\cart\DatabaseStorage
  • hscstudio\cart\MultipleStorage

Example configuration for MultipleStorage.

[
    'components' => [
        'cart' => [
			'class' => 'hscstudio\cart\Cart',
			'storage' => [
				'class' => 'hscstudio\cart\MultipleStorage',
				'storages' => [
					['class' => 'hscstudio\cart\SessionStorage'],
					[
						'class' => 'hscstudio\cart\DatabaseStorage',
						'table' => 'cart',
					],
				],
			]
		],
    ]
]

If You use Multiple Storage, so You should add bootstrap in configuration file:

    'bootstrap' => [
		...
		'hscstudio\cart\CartBootstrap'
	],

Or You can create and use Your own storageClass, it's should extends abstract class of hscstudio\cart\Storage. It is look like :

<?php

namespace app\foo;

use hscstudio\cart\Storage;

class ExampleStorage extends Storage
{
	public function read(Cart $cart)
	{
		// read cart data
	}
	
	public function write(Cart $cart)
	{
		// write cart data
	}
	
	public function lock($drop, Cart $cart)
	{
		// lock cart data, only for db
	}
}

And use it in the following way:

\Yii::$app->cart->create($product, 1);

In order to get number of items in the cart:

$itemsCount = \Yii::$app->cart->getCount();

In order to get total cost of items in the cart:

$total = \Yii::$app->cart->getCost();

If user have finished, and do checkout, so wen use following code

\Yii::$app->cart->removeAll(); // will remove data
// or 
\Yii::$app->cart->checkOut(); // will remove data
// or
\Yii::$app->cart->checkOut(false); // will keep data, only update status to 1 and regenerate session ID

Using discounts

Discounts are implemented as behaviors that could attached to the cart or it's items. To use them, follow this steps:

  1. Define discount class as a subclass of hscstudio\cart\DiscountBehavior
// app/components/MyDiscount.php

class MyDiscount extends DiscountBehavior
{
    /**
     * @param CostCalculationEvent $event
     */
    public function onCostCalculation($event)
    {
        // Some discount logic, for example
        $event->discountValue = 100;
    }
}
  1. Add this behavior to the cart:
$cart->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);

If discount is suitable not for the whole cart, but for the individual item, than it is possible to attach discount to the cart position itself:

$cart->getItemById($itemId)->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);

Note, that the same behavior could be used for both cart and item classes.

  1. To get total cost with discount applied:
$total = \Yii::$app->cart->getCost(true);
  1. During the calculation the following events are triggered:
  • Cart::EVENT_COST_CALCULATION once per calculation.
  • ItemInterface::EVENT_COST_CALCULATION for each item in the cart.

You can also subscribe on this events to perform discount calculation:

$cart->on(Cart::EVENT_COST_CALCULATION, function ($event) {
    $event->discountValue = 100;
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.