1. Go to this page and download the library: Download asyou99/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/ */
asyou99 / yii2-cart example snippets
class Product extends ActiveRecord implements ItemInterface
{
use ItemTrait;
public function getPrice()
{
return $this->price;
}
public function getId()
{
return $this->id;
}
}
public function actionCreate($id)
{
$product = Product::findOne($id);
if ($product) {
\Yii::$app->cart->create($product);
$this->redirect(['index']);
}
}
public function actionIndex()
{
$cart = \Yii::$app->cart;
$products = $cart->getItems();
$total = $cart->getCost();
return $this->render('index', [
'products' => $products,
'total' => $total,
]);
}
public function actionDelete($id)
{
$product = Product::findOne($id);
if ($product) {
\Yii::$app->cart->delete($product);
$this->redirect(['index']);
}
}
public function actionUpdate($id, $quantity)
{
$product = Product::findOne($id);
if ($product) {
\Yii::$app->cart->update($product, $quantity);
$this->redirect(['index']);
}
}
public function actionCheckout(){
\Yii::$app->cart->checkOut(false);
$this->redirect(['index']);
}
\Yii::$app->cart->removeAll(); // will remove data
// or
\Yii::$app->cart->checkOut(); // will remove data
// or
\Yii::$app->cart->checkOut(false); // will keep data, only update status to 1 and regenerate session ID
// app/components/MyDiscount.php
class MyDiscount extends DiscountBehavior
{
/**
* @param CostCalculationEvent $event
*/
public function onCostCalculation($event)
{
// Some discount logic, for example
$event->discountValue = 100;
}
}
$cart->on(Cart::EVENT_COST_CALCULATION, function ($event) {
$event->discountValue = 100;
});
namespace app\foo;
use asyou99\cart\Storage;
class ExampleStorage extends Storage
{
public function read(Cart $cart)
{
// read cart data
}
public function write(Cart $cart)
{
// write cart data
}
public function lock($drop, Cart $cart)
{
// lock cart data, only for db
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.