function showCart(cart){
$('#cart .modal-body').html(cart);
$('#cart').modal();
}
//add to cart
$('.add-to-cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'),
qty = $('#qty').val();
$.ajax({
url: '/cart/add.html',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
public function actionAdd($id){
$qty = (int)Yii::$app->request->get('qty');
$qty = !$qty ? 1 : $qty;
$product = Products::findOne($id);
if(empty($product)) return false;
$session =Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->addToCart($product, $qty);
$this->layout = false;
return $this->render('cart-modal', compact('session', 'product'));
}
<div class="indicator indicator--trigger--click">
<a href="<?= \yii\helpers\Url::to(['cart/cart'])?>" class="indicator__button">
<span class="indicator__area">
<svg width="20px" height="20px">
<use xlink:href="/images/sprite.svg#cart-20"></use>
</svg>
<? if ($_SESSION['cart.qty']): ?>
<span class="indicator__value"><?= $_SESSION['cart.qty'] ?></span>
<? endif; ?>
</span>
</a>
<div class="indicator__dropdown">
<!-- .dropcart -->
<div class="dropcart dropcart--style--dropdown">
<div class="dropcart__body">
<div class="dropcart__products-list">
<? if ($_SESSION['cart']): ?>
<? foreach ($_SESSION['cart'] as $item): ?>
<div class="dropcart__product">
<div class="product-image dropcart__product-image">
<a href="<?= \yii\helpers\Url::to(['product/view', 'alias' => $item['alias']]) ?>"
class="product-image__body">
<img class="product-image__img"
src="/images/products/<?= $item['img'] ?>" alt="">
</a>
</div>
<div class="dropcart__product-info">
<div class="dropcart__product-name">
<a href="<?= \yii\helpers\Url::to(['product/view', 'alias' => $item['alias']]) ?>">
<?= $item['name'] ?>
</a>
</div>
<div class="dropcart__product-meta">
<span class="dropcart__product-quantity"><?= $item['qty'] ?></span>
×
<span class="dropcart__product-price"><?= $item['price'] ?>
сум</span>
</div>
</div>
<button data-id="<?= $item['product_id']?>" type="button"
class="dropcart__product-remove btn btn-light btn-sm btn-svg-icon del-item">
<svg width="10px" height="10px">
<use xlink:href="/images/sprite.svg#cross-10"></use>
</svg>
</button>
</div>
<? endforeach; ?>
<? else: ?>
<h5 align="center">Корзина пуста</h5>
<? endif; ?>
</div>
<? if ($_SESSION['cart']): ?>
<div class="dropcart__totals">
<table>
<tr>
<th>Промежуточный итог</th>
<td><?= $_SESSION['cart.sum'] ?> сум</td>
</tr>
<tr>
<th>Доставка</th>
<td>200000 сум</td>
</tr>
<tr>
<th>Итог</th>
<td><?= $_SESSION['cart.sum'] ?> сум</td>
</tr>
</table>
</div>
<div class="dropcart__buttons">
<button class="btn btn-secondary clear-cart">Очистить</button>
<a class="btn btn-primary" href="<?= \yii\helpers\Url::to(['cart/cart'])?>">Купить</a>
</div>
<? endif; ?>
</div>
</div><!-- .dropcart / end --></div>
</div>
<div class="input-number">
<input class="form-control input-number__input qty" data-qty="<?= $item['qty']?>" type="number" min="1" value="<?=$item['qty']?>">
<div class="input-number__add"></div>
<div class="input-number__sub"></div>
</div>
<button data-id="<?= $item['product_id']?>" class="btn btn-primary update-cart">
✔
</button>
//update cart
$('.update-cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'),
qty = $(this).data('qty');
console.log(id, qty);
$.ajax({
url: '/cart/update.html',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
<div class="product-card__buttons">
<button class="btn btn-primary product-card__addtocart add-to-cart" type="button">
В корзину
</button>
<button data-product_id="<?= $recommended_product['product_id'] ?>" class="btn btn-secondary product-card__addtocart product-card__addtocart--list add-to-cart"
type="button">В корзину
</button>
<button data-product_id="<?= $recommended_product['product_id'] ?>" class="btn btn-light btn-svg-icon btn-svg-icon--fake-svg product-card__wishlist"
type="button">
<svg width="16px" height="16px">
<use xlink:href="/images/sprite.svg#wishlist-16"></use>
</svg>
<span class="fake-svg-icon fake-svg-icon--wishlist-16"></span>
</button>
</div>
$('.add-to-cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('product_id');
$.ajax({
url: '/cart/add.html',
data: {id: 'id'},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
console.log(res);
//showCart(res);
},
error: function(){
alert('Error!');
}
});
});
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'class'=>'frontend\components\LangUrlManager',
'suffix' => '.html',
'rules' => [
'/' => 'site/index',
'contact' => 'site/contact',
'delivery' => 'site/delivery',
'plywood' => 'site/plywood',
'plates' => 'site/plates',
'product-detail/<id:[\w_\/-]+>' => 'site/product-detail',
],
],
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mail.ru',
'username' => 'euska.tilaeva@mail.ru',
'password' => '******',
'port' => '465',
'encryption' => 'ssl',
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'csrfParam' => '_csrf-backend',
'baseUrl' => '/admin',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'clients' => 'site/',
'client-view/<id:[\w_\/-]+>' => 'site/view',
'client-create' => 'site/create',
'client-update/<id:[\w_\/-]+>' => 'site/update',
'ads' => 'ads/',
'ads-view/<id:[\w_\/-]+>' => 'ads/view',
'ads-create' => 'ads/create',
'ads-update/<id:[\w_\/-]+>' => 'ads/update',
],
],
'controllerMap' => [
'elfinder' => [
'class' => 'mihaildev\elfinder\PathController',
'access' => ['@'],
'root' => [
'path' => 'upload/global',
'name' => 'Global'
],
]
],
],
'params' => $params,
];