0

This is my code in actionCheckout():

public function actionCheckout() {
    $proInfo = Yii::app()->session['cart'];
    if (Yii::app()->user->isGuest) {
        $model = new Payment;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['Payment'])) {
            $model->attributes = $_POST['Payment'];
            $model->thoigian = date('Y-m-d H:i:s');
            if ($model->save()) {
                foreach ($proInfo as $key => $value) {
                    $order = new Order;
                    $order->id_sp = $key;
                    $order->soluong = $value;
                    $product = Product::model()->findByPK($key);
                    if (empty($product->khuyenmai)) {
                        $price = $product->gia_sp;
                    } else {
                        $price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
                    }
                    $order->thanhtien = $price * $value;
                    $order->id_payment = $model->id_hoadon;
                    $order->save();
                }
                unset(Yii::app()->session['cart']);
                $this->redirect(Yii::app()->request->baseUrl . '/cart/success/' . $model->id_hoadon);
            }
        }

        $this->render('checkout', array(
            'cart' => $proInfo,
            'model' => $model,
        ));
    } else {
        $model = new Payment;
        $user = User::model()->findByPk(Yii::app()->user->id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['Payment'])) {
            $model->attributes = $_POST['Payment'];
            $model->id_user = Yii::app()->user->id;
            $model->hoten = $user->hoten;
            $model->email = $user->email;
            $model->dienthoai = $user->dienthoai;
            $model->diachi = $user->dia_chi;
            $model->thoigian = date('Y-m-d H:i:s');
            $model->xuly = "Chưa xử lý";
            if ($model->save()) {
                foreach ($proInfo as $key => $value) {
                    $order = new Order;
                    $order->id_sp = $key;
                    $order->soluong = $value;
                    $product = Product::model()->findByPK($key);
                    if (empty($product->khuyenmai)) {
                        $price = $product->gia_sp;
                    } else {
                        $price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
                    }
                    $order->thanhtien = $price * $value;
                    $order->id_payment = $model->id_hoadon;
                    $order->save();
                }
                unset(Yii::app()->session['cart']);
                $this->redirect(Yii::app()->request->baseUrl . '/cart/success/' . $model->id_hoadon);
            }
        }

        $this->render('checkout', array(
            'user' => $user,
            'cart' => $proInfo,
            'model' => $model,
        ));
    }
}

And this is my "checkout.php" file in views:

<link rel="stylesheet" type="text/css" href="<?= Yii::app()->request->baseUrl ?>/css/cart.css"/>    
<?php
$this->breadcrumbs = array(
    'Giỏ hàng' => array('cart/index'),
    'Thanh toán'
);
$this->pageTitle = "Thanh Toán";
$items = 0;
$total = 0;
$i = 0;
?>
<h1>Thanh toán</h1>
<?php
if (empty($cart)) {
    echo "<p>Bạn chưa có sản phẩm nào trong giỏ hàng</p>";
} else {
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'payment-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation' => false,
    ));
    ?>

    <div class="payment">
        <?php
        if (!Yii::app()->user->isGuest) {
            ?>
            <div class="paymentCustomer">
                <h3>Thông tin thanh toán</h3>
                <div style="padding: 10px; clear:both; float:left">Họ tên:<?= Yii::app()->user->name ?></div>
                <div style="padding: 10px; clear:both; float:left">Email:<?= $user->email ?></div>
                <div style="padding: 10px; clear:both; float:left">Địa chỉ:<?= $user->dia_chi ?></div>
                <div style="padding: 10px; clear:both; float:left">Điện thoại:<?= $user->dienthoai ?></div>
            </div>
            <?php
        } else {
            ?>

            <div class="paymentCustomer">
                <h3>Thông tin thanh toán</h3>
                <div class="inputUser"><div>Họ tên <span>*</span></div><?php echo $form->textField($model, 'hoten', array('class' => "inputText loginText")); ?></div>
                <?php echo $form->error($model, 'hoten'); ?>
                <div class="inputUser"><div>Email <span>*</span></div><?php echo $form->textField($model, 'email', array('class' => "inputText loginText")); ?></div>
                <?php echo $form->error($model, 'email'); ?>
                <div class="inputUser"><div>Địa chỉ <span>*</span></div><?php echo $form->textField($model, 'diachi', array('class' => "inputText loginText")); ?></div>
                <?php echo $form->error($model, 'diachi'); ?>
                <div class="inputUser"><div>Điện thoại <span>*</span></div><?php echo $form->textField($model, 'dienthoai', array('class' => "inputText loginText")); ?></div>
                <?php echo $form->error($model, 'dienthoai'); ?>
            </div>


            <?php
        }
        ?>
        <div class="paymentCustomer">
            <h3>Hình thức thanh toán</h3>
            <p class="paymentYour">
                <?php
                echo $form->radioButtonList($model, 'phuongthuc', array(
                    'Tại Cửa hàng' => 'Thanh toán tại cửa hàng',
                    'Khi Nhận hàng' => 'Thanh toán khi nhận được hàng',
                    'Tài khoản Ngân hàng' => 'Thanh toán qua tài khoản Ngân hàng'
                        ), array("class" => "myCheck"));
                ?>
                <?php echo $form->error($model, 'phuongthuc'); ?>
            </p>
            <h3 style="margin: 50px 0 5px 0;">Mã khuyến mãi</h3>
            <?php echo $form->textField($model, 'coupon', array('class' => "inputText loginText")); ?>
            <?php echo $form->error($model, 'coupon'); ?>
        </div>
    </div>
    <div class="cartInfo">
        <div class="headCart">
            <div class="cartField1">Mã sản phẩm</div>
            <div class="cartField2">Tên sản phẩm</div>
            <div class="cartField3">Đơn giá</div>
            <div class="cartField3">Số lượng</div>
            <div class="cartField3">Thành tiền</div>
        </div>


        <?php
        foreach ($cart as $key => $value) {
            $product = Product::model()->findByPK($key);
            if (empty($product->khuyenmai)) {
                $price = $product->gia_sp;
            } else {
                $price = ceil($product->gia_sp * (100 - $product->khuyenmai) / 100 / 1000) * 1000;
            }
            ?>
            <div class="bodyCart">
                <div class="cartField1" data-label="Mã sản phẩm"><?= $product->ma_sp ?></div>
                <div class="cartField2" data-label="Tên sản phẩm"><span><?= $product->ten_sp ?></span></div>
                <div class="cartField3" data-label="Đơn giá"><?= $price . "đ" ?></div>
                <div class="cartField3" data-label="Số lượng"><?= $value ?></div>
                <div class="cartField3" data-label="Thành tiền"><?= $price * $value . "đ" ?></div>
            </div>
            <?php
            $items +=$value;
            $total +=$price * $value;
        }
        ?>
        <div class="footCart">
            <div class="cartField1">Tổng cộng</div>
            <div class="cartField2"></div>
            <div class="cartField3"><?= $items ?></div>
            <div class="cartField3"><?= $total . "đ" ?></div>
        </div>
    </div>
    <div>
        <a href="<?= Yii::app()->request->urlReferrer ?>">
            <input type="button" class="cartLeftButton button" value="Quay trở lại"/>
        </a>
        <?php echo CHtml::submitButton('Xác nhận và gửi đơn hàng', array("class" => "cartRightButton button")); ?>
    </div>
    <?php
    $this->endWidget();
}
?>

But when submit that form it not direct to success page. I have try to insert die("abc") to each line in Controller and find out that it not run from the line

if($model->save()){....

How can I fix that now?

Drakes
  • 23,254
  • 3
  • 51
  • 94
Viet Phan
  • 1,999
  • 3
  • 23
  • 40
  • Which `if ($model->save())` line will it not run from? There are two. – Drakes Apr 03 '15 at 04:40
  • both line not work, bro. it almost the same, except that if the client is a user, $model=>id_user will be ID of that user, else it will be null – Viet Phan Apr 03 '15 at 04:54
  • anyone here? please help, it suddenly not work from yesterday – Viet Phan Apr 03 '15 at 05:11
  • Try to add `echo CActiveForm::validate($model); Yii::app()->die();` when it's `Yii::app()->request->isAjaxRequest` and enable ajax validation. Check console's network tab for response. – Justinas Apr 03 '15 at 05:13
  • it worked for User, but still not work for guest, come on, so close – Viet Phan Apr 03 '15 at 05:33
  • are you still there? i need to give to client today :( – Viet Phan Apr 03 '15 at 06:10
  • the CActiveRecords' save() method ALWAYS returns something. If it just dies silently and does not return anything, the problem is in your model class. Can you post the Payment class? I'm mainly interested in the onBeforeValidate, onBeforeSave, onAfterValidate, onAfterSave methods (if you have overridden any of them) – akos Apr 04 '15 at 00:39

0 Answers0