• Stars
    star
    223
  • Rank 177,897 (Top 4 %)
  • Language
    PHP
  • License
    BSD 3-Clause "New...
  • Created over 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Shopping cart extension for yii2

Shopping cart for Yii 2

This extension adds shopping cart for Yii framework 2.0

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"omnilight/yii2-shopping-cart": "*"

to the require section of your composer.json.

How to use

In your model:

class Product extends ActiveRecord implements CartPositionInterface
{
    use CartPositionTrait;

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

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

In your controller:

public function actionAddToCart($id)
{
    $cart = Yii::$app->cart;

    $model = Product::findOne($id);
    if ($model) {
        $cart->put($model, 1);
        return $this->redirect(['cart-view']);
    }
    throw new NotFoundHttpException();
}

Also you can use cart as global application component:

[
    'components' => [
        'cart' => [
            'class' => 'yz\shoppingcart\ShoppingCart',
            'cartId' => 'my_application_cart',
        ]
    ]
]

And use it in the following way:

\Yii::$app->cart->put($cartPosition, 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 the original model that you want to use as cart position is too heavy to be stored in the session, you can create a separate class implementing CartPositionInterface, and original model can implement CartPositionProviderInterface:

// app\models\Product.php

class Product extends ActiveRecord implements CartPositionProviderInterface
{
    public function getCartPosition()
    {
        return \Yii::createObject([
            'class' => 'app\models\ProductCartPosition',
            'id' => $this->id,
        ]);
    }
}

// app\models\ProductCartPosition.php

class ProductCartPosition extends Object implements CartPositionInterface
{
    /**
     * @var Product
     */
    protected $_product;

    public $id;

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

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

    /**
     * @return Product
    */
    public function getProduct()
    {
        if ($this->_product === null) {
            $this->_product = Product::findOne($this->id);
        }
        return $this->_product;
    }
}

This way gives us ability to create separate cart positions for the same product, that differs only on some property, for example price or color:

// app\models\ProductCartPosition.php

class ProductCartPosition extends Object implements CartPositionInterface
{
    public $id;
    public $price;
    public $color;

    //...
    public function getId()
    {
        return md5(serialize([$this->id, $this->price, $this->color]));
    }

    //...
}

Using discounts

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

  1. Define discount class as a subclass of yz\shoppingcart\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 positions, than it is possible to attach discount to the cart position itself:

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

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

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

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

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

More Repositories

1

yii2-scheduling

Scheduling extension for Yii2 framework
PHP
315
star
2

yii2-sypexgeo

Yii2 extension for Sypex Geo API (http://sypexgeo.net)
PHP
18
star
3

yii2-vuejs

Vue.js library for Yii2 framework
PHP
17
star
4

yii2-tokens

Provides classes for token validations for Yii2 framework
PHP
10
star
5

yii2-datetime

Date and time behaviors and helpers for Yii2 framework
PHP
10
star
6

yii2-momentjs

Moment.js asset for Yii2 framework
PHP
9
star
7

yii2-phonenumbers

Provides phone number validation and behaviour using giggsey/libphonenumber-for-php
PHP
7
star
8

yii2-finance

Finance components and models
PHP
6
star
9

yii2-event-listeners

Event listeners support for Yii2 framework
PHP
5
star
10

yii2-thumbnails

Thumbnails helper for Yii2 framework
PHP
5
star
11

yii2-highcharts

Highcharts widget for Yii2 framework
PHP
3
star
12

yii2-sms

SMS extension for Yii2
PHP
3
star
13

yii2-sweetalert

SweetAlert asset for Yii2 framework
PHP
3
star
14

yii2-angular

Angular js asset for Yii2 framework
PHP
3
star
15

yii2-files

Base model and other stuff for managing files in Yii2
PHP
2
star
16

yz2-admin-export

Extension for yz2-admin that allows exporting of the data in the queue
PHP
2
star
17

yii2-fancybox

FancyBox asset for Yii2 framework
PHP
2
star
18

yz2

Yz2 extension for Yii2 Framework to speed up and simplify application development
PHP
2
star
19

logger-sidecar

Sidecar container for k8s
Shell
2
star
20

yz2-app-standard

Standard application based on Yii2 and Yz2
PHP
2
star
21

yz2-admin

Administation panel for Yz2 extension
PHP
2
star
22

yii2-slick-carousel

Slick carousel asset for Yii2 framework
PHP
2
star
23

yii2-bootstrap-daterangepicker

Date range picker component for Bootstrap for Yii2 framework
PHP
2
star
24

yii2-captcha

Captcha for Yii2 framework that give you ability to configure it's code generation algorithm
PHP
2
star
25

omnilight.github.io

Heart of agaletskiy.com
HTML
2
star
26

smarty-plugins

Smarty plugins that could be used with Yii framework
PHP
1
star
27

icons

Yii extension for managing icons based on Font Awesome
PHP
1
star
28

yz-admin

Administration panel for Yz Engine
PHP
1
star
29

yii2-many-many-relation

Behavior for Yii2 that simplify managing many-many relations via forms
PHP
1
star