vsevolod-ryzhov/yii2-cart

Yii2 cart component

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v0.9 2020-07-19 08:50 UTC

This package is auto-updated.

Last update: 2025-03-19 19:14:03 UTC


README

Yii2 cart component for eCommerce web applications

Installation

Via Composer

composer require vsevolod-ryzhov/yii2-cart

Usage

Use CombinedStorage if you want to use all provided functionality.

  1. To use session as cart storage:
// setup Cart component using included SessionStorage class
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;

        $container->setSingleton(Cart::class, [], [
            new SessionStorage('cart', $app->session), new BaseCost()
        ]);
    }
}

// Product class must implement ProductInterface
class Product implements ProductInterface
{
    public function getId()
    {
        return $this->id;
    }

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

// inject component in your controller
class CartController extends Controller
{
    /**
     * @var Cart
     */
    private $cart;

    public function __construct($id, $module, Cart $cart, $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->cart = $cart;
    }

    public function actionAdd($id, $count = 1)
    {
        $product = Product::findOne($id);
        $this->cart->add($product,  $count);
    }
}
  1. To use database storage (for authenticated users) and session storage (for guests):
// create converter class which extends included AbstractProductConverter
// this class must convert your Product model class to CartItem class
// this class is used in Init class on DatabaseStorage initialization
class ProductToCartConverter extends AbstractProductConverter
{
    public function convertProductToCartItem($product, int $quantity): CartItem
    {
        /* @var $product Product */
        return new CartItem($product, $quantity);
    }
}
// setup Cart component using included SessionStorage class
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;
        if ($app->user->isGuest) {
            $container->setSingleton(Cart::class, [], [
                new SessionStorage('cart', $app->session), new BaseCost()
            ]);
        } else {
            // you can specify database options for DatabaseStorage class (cartItemsTable & etc)
            $container->setSingleton(Cart::class, [], [
                new DatabaseStorage($app->user->id, $app->db, new ProductsQuery(Product::class), new ProductToCartConverter), new BaseCost()
            ]);
        }
    }
}

// Query class for Product model must extends included AbstractProductQuery
class ProductsQuery extends AbstractProductQuery
{
    /**
     * @return ProductsQuery
     */
    public function canBuy(): ActiveQuery
    {
        // query product which user can buy
        return $this->active()->available();
    }
}
  1. To use CookieStorage:
// use ProductToCartConverter from previews sample
class Init implements BootstrapInterface
{
    $container->setSingleton(Cart::class, [], [
        new CookieStorage(
            new CookieStorageSettings('cart', 3600),
            $app->request->cookies,
            $app->response->cookies,
            new ProductsQuery(Product::class),
            new ProductToCartConverter
        ),
        new BaseCost()
    ]);
}
  1. CombinedStorage (CookieStorage + DatabaseStorage):
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;
        $container->setSingleton(Cart::class, [], [
            new CombinedStorage(
                $app->user,
                $app->db,
                new CookieStorageSettings('cart', 3600),
                $app->request->cookies,
                $app->response->cookies,
                new ProductsQuery(Product::class),
                new ProductToCartConverter
            ),
            new BaseCost()
        ]);
    }
}