Yii2-Cart
Cart extensions for Yii 2
Refactory library from Codeginiter Cart Class v3.0dev, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist marciocamello/yii2-mcms-cart "*"
or add, (*4)
"marciocamello/yii2-mcms-cart": "*"
to the require section of your composer.json
file., (*5)
Usage
Adding an Item to The Cart
use mcms\cart\Cart;
$cart = new Cart();
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$cart->insert($data);
Adding Multiple Items to The Cart
use mcms\cart\Cart;
$cart = new Cart();
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
),
array(
'id' => 'sku_567ZYX',
'qty' => 1,
'price' => 9.95,
'name' => 'Coffee Mug'
),
array(
'id' => 'sku_965QRS',
'qty' => 1,
'price' => 29.95,
'name' => 'Shot Glass'
)
);
$cart->insert($data);
Displaying the Cart
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use mcms\cart\Cart;
$cart = new Cart();
if($_POST)
{
$cart->update($_POST);
Yii::$app->controller->refresh();
}
'cart-form',
'options' => ['class' => 'form-horizontal'],
]) ?>
QTY |
Item Description |
Item Price |
Sub-Total |
contents() as $items): ?>
'3', 'size' => '5']); ?> |
has_options($items['rowid']) == TRUE): ?>
product_options($items['rowid']) as $option_name => $option_value): ?>
:
, (*6)
|
format_number($items['price']); ?> |
$format_number($items['subtotal']); ?> |
|
Total |
$format_number($cart->total()); ?> |