2017 © Pedro Peláez
 

yii2-extension yii2-cart

Yii2 Shopping cart

image

yii2mod/yii2-cart

Yii2 Shopping cart

  • Friday, March 9, 2018
  • by disem
  • Repository
  • 28 Watchers
  • 92 Stars
  • 4,041 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 39 Forks
  • 1 Open issues
  • 8 Versions
  • 6 % Grown

The README.md

, (*1)

Yii2 Shopping Cart Extension


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

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality, (*3)

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*4)

Installation

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

Either run, (*6)

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

or add, (*7)

"yii2mod/yii2-cart": "*"

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

Configuration

1) Configure the cart component:, (*9)

return [
    //....
    'components' => [
        'cart' => [
            'class' => 'yii2mod\cart\Cart',
            // you can change default storage class as following:
            'storageClass' => [
                'class' => 'yii2mod\cart\storage\DatabaseStorage',
                // you can also override some properties 
                'deleteIfEmpty' => true
            ]
        ],
    ]
];

2) Create the Product Model that implements an CartItemInterface:, (*10)

class ProductModel extends ActiveRecord implements CartItemInterface
{

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

    public function getLabel()
    {
        return $this->name;
    }

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

If you use the yii2mod\cart\storage\DatabaseStorage as storageClass then you need to apply the following migration:, (*11)

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

Using the shopping cart

Operations with the shopping cart are very straightforward when using a models that implement one of the two cart interfaces. The cart object can be accessed under \Yii::$app->cart and can be overridden in configuration if you need to customize it., (*12)

// access the cart from "cart" subcomponent
$cart = \Yii::$app->cart;

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

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

// returns the sum of all 'vat' attributes (or return values of getVat()) from all models in the cart.
$totalVat = $cart->getAttributeTotal('vat');

// clear the cart
$cart->clear();

View Cart Items

You can use the CartGrid widget for generate table with cart items as following:, (*13)

 [
        'id',
        'label',
        'price'
    ]
]); ?>

Items in the cart

Products/items that are added to the cart are serialized/unserialized when saving and loading data from cart storage. If you are using Active Record models as products/discounts, make sure that you are omitting any unnecessary references from the serialized data to keep it compact., (*14)

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

// get only products
$items = $cart->getItems(Cart::ITEM_PRODUCT);

// loop through cart items
foreach ($items as $item) {
    // access any attribute/method from the model
    var_dump($item->getAttributes());

    // remove an item from the cart by its ID
    $cart->remove($item->uniqueId)
}

Get Number of Products in Cart

You can use the getCount to get count as this example:, (*15)

// get count of all products in cart:
$items = $cart->getCount();

// get count of Specific Item Type:
$items = $cart->getCount(Cart::ITEM_PRODUCT);

The Versions

09/03 2018

dev-master

9999999-dev

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Igor Chepurnoy
by Igor Chepurnoi

22/11 2016

1.3

1.3.0.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Igor Chepurnoy

11/11 2016

1.2.1

1.2.1.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

09/11 2016

1.2

1.2.0.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

05/07 2016

1.1

1.1.0.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

17/05 2016

1.0.2

1.0.2.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

16/05 2016

1.0.1

1.0.1.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

 

13/05 2016

1.0

1.0.0.0

Yii2 Shopping cart

  Sources   Download

MIT

The Requires

  • php >=5.4.0