1. Go to this page and download the library: Download yii2mod/yii2-cart library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2mod / yii2-cart example snippets
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
]
],
]
];
class ProductModel extends ActiveRecord implements CartItemInterface
{
public function getPrice()
{
return $this->price;
}
public function getLabel()
{
return $this->name;
}
public function getUniqueId()
{
return $this->id;
}
}
// 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();
echo \yii2mod\cart\widgets\CartGrid::widget([
// Some widget property maybe need to change.
'cartColumns' => [
'id',
'label',
'price'
]
]);
// 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 count of all products in cart:
$items = $cart->getCount();
// get count of Specific Item Type:
$items = $cart->getCount(Cart::ITEM_PRODUCT);
php composer.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.