public function actionView(){
$session = Yii::$app->session;
$session->open();
$this->setMeta('Корзина');
$order = new Order();
if( $order->load(Yii::$app->request->post()) ){
$order->qty = $session['cart.qty'];
$order->sum = $session['cart.sum'];
if($order->save()){
$this->saveOrderItems($session['cart'], $order->id);
Yii::$app->session->setFlash('success', 'Ваш заказ принят. Менеджер вскоре свяжется с Вами.');
Yii::$app->mailer->compose('order', ['session' => $session])
->setFrom(['spirin.costia@yandex.ru'])
->setTo($order->email)
->setSubject('Заказ')
->setTextBody('Текст сообщения')
->send();
$email = $oder->email;echo"$email";
Yii::$app->mailer->compose('order', ['session' => $session])
->setFrom(['spirin.costia@yandex.ru'])
->setTo('spirin.costia@yandex.ru')
->setSubject('Заказ')
->send();
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->refresh();
}else{
Yii::$app->session->setFlash('error', 'Ошибка оформления заказа');
}
}
return $this->render('view', compact('session', 'order',['order' => $order]));
}
<?php
use yii\helpers\Html;
?>
<div class="table-responsive">
<table style="width: 100%; border: 1px solid #ddd; border-collapse: collapse;">
<thead>
<tr style="background: #f9f9f9;">
<th style="padding: 8px; border: 1px solid #ddd;">Наименование</th>
<th style="padding: 8px; border: 1px solid #ddd;">Кол-во</th>
<th style="padding: 8px; border: 1px solid #ddd;">Цена</th>
<th style="padding: 8px; border: 1px solid #ddd;">Сумма</th>
</tr>
</thead>
<tbody>
<?php foreach($session['cart'] as $id => $item):?>
<tr>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['name']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['qty']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['price']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['qty'] * $item['price']?></td>
</tr>
<?php endforeach?>
<tr>
<td colspan="3" style="padding: 8px; border: 1px solid #ddd;">Итого: </td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $session['cart.qty']?></td>
</tr>
<tr>
<td colspan="3" style="padding: 8px; border: 1px solid #ddd;">На сумму: </td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $session['cart.sum']?></td>
</tr>
</tbody>
<thead>
<tr style="background: #f9f9f9;">
<th style="padding: 8px; border: 1px solid #ddd;">Телефон</th>
<th style="padding: 8px; border: 1px solid #ddd;">Емаил</th>
</tr>
</thead>
<tbody>
<?php
//if ($order->load(Yii::$app->request->post()) && $order->validate()) {
// выполнение кода, если данные введены правильно
// иначе будет показа ошибка (Yii выводит ошибки автоматически)
// $email=$order->email; // используется вместо $_POST['name']
//}
?>
<tr>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $order->email?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?php echo"$email"; ?></td>
</tr>
<?php //endforeach?>
</tbody>
</table>
</div>
в папке views/cart/view.php:
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
?>
<div class="container">
<?php
$email = $oder->email;echo"$email";
if (isset($order->addres)) {
$email = $order->addres;
echo"$email";
} else {
$Err.="Ошибка передачи данных с поля id!<br>";
echo"ошибка";
};
?>
<?php if( Yii::$app->session->hasFlash('success') ): ?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo Yii::$app->session->getFlash('success'); ?>
</div>
<?php endif;?>
<?php if( Yii::$app->session->hasFlash('error') ): ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo Yii::$app->session->getFlash('error'); ?>
</div>
<?php endif;?>
<?php if(!empty($session['cart'])): ?>
<div class="table-responsive">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Фото</th>
<th>Наименование</th>
<th>Кол-во</th>
<th>Цена</th>
<th>Сумма</th>
<th><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<?php foreach($session['cart'] as $id => $item):?>
<tr>
<td><?= \yii\helpers\Html::img("@web/images/products/{$item['img']}", ['alt' => $item['name'], 'height' => 50]) ?></td>
<td><a href="<?= Url::to(['product/view', 'id' => $id])?>"><?= $item['name']?></a></td>
<td><?= $item['qty']?></td>
<td><?= $item['price']?></td>
<td><?= $item['qty'] * $item['price']?></td>
<td><span data-id="<?= $id?>" class="glyphicon glyphicon-remove text-danger del-item" aria-hidden="true"></span></td>
</tr>
<?php endforeach?>
<tr>
<td colspan="5">Итого: </td>
<td><?= $session['cart.qty']?></td>
</tr>
<tr>
<td colspan="5">На сумму: </td>
<td><?= $session['cart.sum']?></td>
</tr>
</tbody>
</table>
</div>
<hr/>
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
]) ?>
<?= $form->field($order, 'name')?>
<?= $form->field($order, 'email') ?>
<?= $form->field($order, 'phone')?>
<?= $form->field($order, 'address')?>
<?= Html::submitButton('Заказать', ['class' => 'btn btn-success'])?>
<?php ActiveForm::end()?>
<?php else: ?>
<h3>Корзина пуста</h3>
<?php endif;?>
</div>
Наименование | Кол-во | Цена | Сумма |
---|---|---|---|
<?= $item['name']?> | <?= $item['qty']?> | <?= $item['price']?> | <?= $item['qty'] * $item['price']?> |
Итого: | <?= $session['cart.qty']?> | ||
На сумму: | <?= $session['cart.sum']?> |