2017 © Pedro Peláez
 

yii2-extension yii2-shopping-cart

Yii2 extension that adds shopping cart functions

image

omnilight/yii2-shopping-cart

Yii2 extension that adds shopping cart functions

  • Tuesday, June 12, 2018
  • by omnilight
  • Repository
  • 38 Watchers
  • 207 Stars
  • 35,316 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 106 Forks
  • 4 Open issues
  • 10 Versions
  • 5 % Grown

The README.md

Shopping cart for Yii 2

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

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

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

or add, (*4)

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

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

How to use

In your model:, (*6)

class Product extends ActiveRecord implements CartPositionInterface
{
    use CartPositionTrait;

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

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

In your controller:, (*7)

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:, (*8)

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

And use it in the following way:, (*9)

\Yii::$app->cart->put($cartPosition, 1);

In order to get number of items in the cart:, (*10)

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

In order to get total cost of items in the cart:, (*11)

$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:, (*12)

// 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:, (*13)

// 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:, (*14)

  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:, (*15)

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

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

  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., (*17)

You can also subscribe on this events to perform discount calculation:, (*18)

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

The Versions

12/06 2018

dev-master

9999999-dev

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

12/06 2018

1.3.0

1.3.0.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

08/09 2015

1.2.2

1.2.2.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

18/06 2015

1.2.1

1.2.1.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

06/04 2015

1.2.0

1.2.0.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

09/12 2014

1.1.0

1.1.0.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

01/12 2014

1.0.3

1.0.3.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

28/11 2014

1.0.2

1.0.2.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

20/10 2014

1.0.1

1.0.1.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart

19/10 2014

1.0.0

1.0.0.0

Yii2 extension that adds shopping cart functions

  Sources   Download

The Requires

 

by Pavel Agalecky

yii shopping cart