CartController.php 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\app\appserver\modules\Checkout\controllers;
use fecshop\app\appserver\modules\AppserverController;
use Yii;
/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class CartController extends AppserverController
{
    public $enableCsrfValidation = false;
    public function actionIndex()
    {
        $data = $this->getBlock()->getLastData();
        return $this->render($this->action->id, $data);
    }
    /**
     * 把产品加入到购物车.
     */
    public function actionAdd()
    {
        //echo 1;exit;
        $custom_option = Yii::$app->request->get('custom_option');
        $product_id = Yii::$app->request->get('product_id');
        $qty = Yii::$app->request->get('qty');
        //$custom_option  = \Yii::$service->helper->htmlEncode($custom_option);
        $product_id = \Yii::$service->helper->htmlEncode($product_id);
        $qty = \Yii::$service->helper->htmlEncode($qty);
        $qty = abs(ceil((int) $qty));
        $return = [];
        $code = 400;
        if ($qty && $product_id) {
            if ($custom_option) {
                $custom_option_sku = json_decode($custom_option, true);
            }
            if (empty($custom_option_sku)) {
                $custom_option_sku = null;
            }
            $item = [
                'product_id' => $product_id,
                'qty'        =>  $qty,
                'custom_option_sku' => $custom_option_sku,
            ];
            $innerTransaction = Yii::$app->db->beginTransaction();
            try {
                $addToCart = Yii::$service->cart->addProductToCart($item);
                if ($addToCart) {
                    $return = [
                        'status' => 'success',
                        'items_count' => Yii::$service->cart->quote->getCartItemCount(),
                    ];
                    $code = 200;
                    $innerTransaction->commit();
                } else {
                    $errors = Yii::$service->helper->errors->get(',');
                    $return = [
                        'status' => 'fail',
                        'content'=> $errors,
                        //'items_count' => Yii::$service->cart->quote->getCartItemCount(),
                    ];
                    $code = 400;
                    $innerTransaction->rollBack();
                }
            } catch (Exception $e) {
                $innerTransaction->rollBack();
            }
        }
        
        return [
            'code'      => $code ,
            'content'   => $return ,
        ];
    }
    /**
     * 购物车中添加优惠券.
     */
    public function actionAddcoupon()
    {
        if (Yii::$app->user->isGuest) {
            // 记忆一下登录成功返回购物车页面
            $cartUrl = Yii::$service->url->getUrl('checkout/cart');
            Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl);
            echo json_encode([
                'status' => 'fail',
                'content'=> 'nologin',
            ]);
            exit;
        }
        $coupon_code = trim(Yii::$app->request->post('coupon_code'));
        $coupon_code = \Yii::$service->helper->htmlEncode($coupon_code);
        if ($coupon_code) {
            $innerTransaction = Yii::$app->db->beginTransaction();
            try {
                if (Yii::$service->cart->coupon->addCoupon($coupon_code)) {
                    $innerTransaction->commit();
                } else {
                    $innerTransaction->rollBack();
                }
            } catch (Exception $e) {
                $innerTransaction->rollBack();
            }
            $error_arr = Yii::$service->helper->errors->get(true);
            if (!empty($error_arr)) {
                $error_str = implode(',', $error_arr);
                echo json_encode([
                    'status' => 'fail',
                    'content'=> $error_str,
                ]);
                exit;
            } else {
                echo json_encode([
                    'status' => 'success',
                    'content'=> 'add coupon success',
                ]);
                exit;
            }
        } else {
            echo json_encode([
                'status' => 'fail',
                'content'=> 'coupon is empty',
            ]);
            exit;
        }
    }
    /**
     * 购物车中取消优惠券.
     */
    public function actionCancelcoupon()
    {
        if (Yii::$app->user->isGuest) {
            // 记忆一下登录成功返回购物车页面
            $cartUrl = Yii::$service->url->getUrl('checkout/cart');
            Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl);
            echo json_encode([
                'status' => 'fail',
                'content'=> 'nologin',
            ]);
            exit;
        }
        $coupon_code = trim(Yii::$app->request->post('coupon_code'));
        if ($coupon_code) {
            $innerTransaction = Yii::$app->db->beginTransaction();
            try {
                $cancelStatus = Yii::$service->cart->coupon->cancelCoupon($coupon_code);
                if (!$cancelStatus) {
                    echo json_encode([
                        'status' => 'fail',
                        'content'=> 'coupon is not exist;',
                    ]);
                    $innerTransaction->rollBack();
                    exit;
                }
                $error_arr = Yii::$service->helper->errors->get(true);
                if (!empty($error_arr)) {
                    $error_str = implode(',', $error_arr);
                    echo json_encode([
                        'status' => 'fail',
                        'content'=> $error_str,
                    ]);
                    $innerTransaction->rollBack();
                    exit;
                } else {
                    echo json_encode([
                        'status' => 'success',
                        'content'=> 'cacle coupon success',
                    ]);
                    $innerTransaction->commit();
                    exit;
                }
            } catch (Exception $e) {
                $innerTransaction->rollBack();
            }
        } else {
            echo json_encode([
                'status' => 'fail',
                'content'=> 'coupon is empty',
            ]);
            exit;
        }
    }
    public function actionUpdateinfo()
    {
        $item_id = Yii::$app->request->get('item_id');
        $up_type = Yii::$app->request->get('up_type');
        $innerTransaction = Yii::$app->db->beginTransaction();
        try {
            if ($up_type == 'add_one') {
                $status = Yii::$service->cart->addOneItem($item_id);
            } elseif ($up_type == 'less_one') {
                $status = Yii::$service->cart->lessOneItem($item_id);
            } elseif ($up_type == 'remove') {
                $status = Yii::$service->cart->removeItem($item_id);
            }
            if ($status) {
                echo json_encode([
                    'status' => 'success',
                ]);
                $innerTransaction->commit();
            } else {
                echo json_encode([
                    'status' => 'fail',
                ]);
                $innerTransaction->rollBack();
            }
        } catch (Exception $e) {
            $innerTransaction->rollBack();
        }
    }
}