I would like to achieve the shopping cart functionality through Yii2-pjax, browse product click on the "Add to cart", if not logged in, login dialog appears correctly registered in the appropriate DIV by pjax show the number of purchase, if you have logged in, increase the number of directly after the show. I do not know how, please help.
Add the number of product form
_quality.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
$js = <<<JS
$(’#product-quality-detail’).on(‘beforeSubmit’, function(e) {
var form = $(this);
if(form.find('.has-error').length) {
return false;
}
$.ajax({
url: form.attr('action'),
type: 'post',
data: form.serialize(),
success: function(response) {
//$.pjax.reload({method:'POST',container:'#shopping-cart'});
}
});
return false;
}).on(‘submit’, function(e){
e.preventDefault();
$.pjax.reload({method:'POST', container:'#shopping-cart'});
});
JS;
$this->registerJs($js);
?>
<div id="add-to-cart-form">
<?php Pjax::begin(['id' => 'product-quality', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['container' => '#shopping-cart']]);?>
<?php $form = ActiveForm::begin(['id' => 'product-quality-detail', 'options' => ['data-pjax' => true]]); ?>
<?= $form->field($model, 'productId')->hiddenInput()->label(false) ?>
<?= $form->field($model, 'quality')->textInput(['maxlength' => 10, 'class' => 'width:20px;'])->label(false) ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('appProduct', 'ADD_TO_SHOPPING_CART'), ['class' => 'btn btn-primary'])?>
</div>
<?php ActiveForm::end();?>
<?php Pjax::end();?>
</div>
_login.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\captcha\Captcha;
use yii\captcha\CaptchaAsset;
use yii\widgets\Pjax;
CaptchaAsset::register($this);
$this->registerJs(
'$("document").ready(function() {
$("#member-login").on("pjax:end", function(){
$.pjax.reload({container:"#shopping-cart"});
});
});'
);
?>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static">
<div class="modal-dialog" role="document" style="width:300px; margin:0px auto;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel"><?= Yii::t('appUser', 'MEMBER_LOGIN_TITLE') ?></h4>
</div>
<div class="modal-body">
<?php Pjax::begin(['id' => 'member-login']);?>
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
'fieldConfig' => [
'template' => '<div class="input-group">{label}{input}</div>',
'labelOptions' => ['style' => 'width:70px;'],
'inputOptions' => ['style' => 'width:160px;']
],
'options' => ['data-pjax' => true]
]);
echo $form->field($login, 'username');
echo $form->field($login, 'passwd')->passwordInput();
echo $form->field($login, 'captcha')->widget(Captcha::classname(),[
'captchaAction' => '/site/captcha',
'template' => '{input}{image}',
'imageOptions' => ['title'=> Yii::t('appUser', 'VALIDATE_CODE_TIP'),'style' => 'display:inline-block; width:80px; height:25px;cursor:pointer;'],
'options' => ['class' => 'form-control', 'style' => 'display:inline-block; width:80px;', 'required' => 'required'],
]);
?>
<div class="form-group">
<?= Html::hiddenInput('preQuality',$preQuality) ?>
<?= Html::submitButton(Yii::t('appUser', 'MEMBER_LOGIN'), ['class' => 'btn btn-primary', 'name' => 'login-button'])?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
</div>
</div>
</div>
<?php
$this->registerJs(
'$("document").ready(function() {
$("#myModal").modal("show");
});'
);
?>
show shopping cart number
<div id="shopping-cart-detail">
<?php Pjax::begin(['id' => 'shopping-cart']);?>
<?php if ($login !== null): ?>
<?= $quality ?>
<?= $this->render('_login', ['login' => $login, 'preQuality' => $preQuality]) ?>
<?php else: ?>
<?= $quality ?>
<?php endif;?>
<?php Pjax::end();?>
</div>
controller:
$model = new AddToCartForm();
$model->productId = $id;
$model->quality = 1;
$login = null;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$post = Yii::$app->request->post();
$quality = $post['_csrf'];
} elseif (Yii::$app->request->isAjax) {
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$post = Yii::$app->request->post();
$quality = $post['_csrf'];
} else {
$quality = 2;
}
} elseif (Yii::$app->request->isPjax) {
$quality = 3;
} else {
$quality = 4;
}
return $this->render('detail',[
'product' => $product,
'images' => $images,
'regions' => $regions,
'related' => $related,
'properties' => $properties,
'propertyTypes' => $propertyTypes,
'model' => $model,
'quality' => $quality,
'preQuality' => 0,
'login' => $login
]);
Submit form not receive data, or the data can not be displayed in # shopping-cart.