CartController.php 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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;
T
Terry 已提交
19
    
20 21
    public function actionIndex()
    {
T
Terry 已提交
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
        $currency_info = Yii::$service->page->currency->getCurrencyInfo();

        return [
            'code' => 200,
            'cart_info' => $this->getCartInfo(),
            'currency' => $currency_info,
        ];
    }

    /** @return data example
     *	[
     *				'coupon_code' 	=> $coupon_code,
     *				'grand_total' 	=> $grand_total,
     *				'shipping_cost' => $shippingCost,
     *				'coupon_cost' 	=> $couponCost,
     *				'product_total' => $product_total,
     *				'products' 		=> $products,
     *	]
     *			上面的products数组的个数如下:
     *			$products[] = [
     *					    'item_id' => $one['item_id'],
     *						'product_id' 		=> $product_id ,
     *						'qty' 				=> $qty ,
     *						'custom_option_sku' => $custom_option_sku ,
     *						'product_price' 	=> $product_price ,
     *						'product_row_price' => $product_row_price ,
     *						'product_name'		=> $product_one['name'],
     *						'product_url'		=> $product_one['url_key'],
     *						'product_image'		=> $product_one['image'],
     *						'custom_option'		=> $product_one['custom_option'],
     *						'spu_options' 		=> $productSpuOptions,
     *				];
     */
    public function getCartInfo()
    {
        $cart_info = Yii::$service->cart->getCartInfo();

        if (isset($cart_info['products']) && is_array($cart_info['products'])) {
            foreach ($cart_info['products'] as $k=>$product_one) {
                // 设置名字,得到当前store的语言名字。
                $cart_info['products'][$k]['name'] = Yii::$service->store->getStoreAttrVal($product_one['product_name'], 'name');
                // 设置图片
                if (isset($product_one['product_image']['main']['image'])) {
                    $productImg = $product_one['product_image']['main']['image'];
                    $cart_info['products'][$k]['img_url'] = Yii::$service->product->image->getResize($productImg,[150,150],false);
                }
                // 产品的url
                $cart_info['products'][$k]['url'] = '/catalog/product/'.$product_one['product_id'];

                $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : '';
                $custom_option_sku = $product_one['custom_option_sku'];
                // 将在产品页面选择的颜色尺码等属性显示出来。
                $custom_option_info_arr = $this->getProductOptions($product_one);
                $cart_info['products'][$k]['custom_option_info'] = $custom_option_info_arr;
                // 设置相应的custom option 对应的图片
                $custom_option_image = isset($custom_option[$custom_option_sku]['image']) ? $custom_option[$custom_option_sku]['image'] : '';
                if ($custom_option_image) {
                    $cart_info['products'][$k]['img_url'] = Yii::$service->product->image->getResize($custom_option_image,[150,150],false);
                }
            }
        }

        return $cart_info;
    }

    /**
     * 将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
     */
    public function getProductOptions($product_one)
    {
        $custom_option_info_arr = [];
        $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : '';
        $custom_option_sku = $product_one['custom_option_sku'];
        if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) {
            $custom_option_info = $custom_option[$custom_option_sku];
            foreach ($custom_option_info as $attr=>$val) {
                if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) {
                    $attr = str_replace('_', ' ', $attr);
                    $attr = ucfirst($attr);
                    $custom_option_info_arr[$attr] = $val;
                }
            }
        }

        $spu_options = $product_one['spu_options'];
        if (is_array($spu_options) && !empty($spu_options)) {
            foreach ($spu_options as $label => $val) {
                $custom_option_info_arr[$label] = $val;
            }
        }

        return $custom_option_info_arr;
114
    }
T
Terry 已提交
115 116 117

    
    
118
    /**
T
Terry 已提交
119
     * 把产品加入到购物车.
120 121 122 123
     */
    public function actionAdd()
    {
        //echo 1;exit;
T
Terry 已提交
124 125 126
        $custom_option = Yii::$app->request->post('custom_option');
        $product_id = Yii::$app->request->post('product_id');
        $qty = Yii::$app->request->post('qty');
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
        //$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 ,
        ];
    }
    /**
T
Terry 已提交
176
     * 购物车中添加优惠券.
177 178 179 180
     */
    public function actionAddcoupon()
    {
        if (Yii::$app->user->isGuest) {
T
Terry 已提交
181 182 183 184
            return [
                'code' => 400,
                'content' => 'you must login your account',
            ];
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        }
        $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);
T
Terry 已提交
202 203
                return [
                    'code' => '401',
204
                    'content'=> $error_str,
T
Terry 已提交
205
                ];
206
            } else {
T
Terry 已提交
207 208
                return [
                    'code' => '200',
209
                    'content'=> 'add coupon success',
T
Terry 已提交
210
                ];
211 212
            }
        } else {
T
Terry 已提交
213 214
            return [
                'status' => '402',
215
                'content'=> 'coupon is empty',
T
Terry 已提交
216
            ];
217 218 219
        }
    }
    /**
T
Terry 已提交
220
     * 购物车中取消优惠券.
221 222 223 224
     */
    public function actionCancelcoupon()
    {
        if (Yii::$app->user->isGuest) {
T
Terry 已提交
225 226 227 228
            return [
                'code' => 400,
                'content' => 'you must login your account',
            ];
229 230 231 232 233 234 235 236
        }
        $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) {
                    $innerTransaction->rollBack();
T
Terry 已提交
237 238 239 240 241
                    return [
                        'code' => 401,
                        'content'=> 'cancel coupon fail',
                    ];
                    
242 243 244 245 246
                }
                $error_arr = Yii::$service->helper->errors->get(true);
                if (!empty($error_arr)) {
                    $error_str = implode(',', $error_arr);
                    $innerTransaction->rollBack();
T
Terry 已提交
247 248 249 250 251
                    return [
                        'code' => '401',
                        'content'=> $error_str,
                    ];
                    
252 253
                } else {
                    $innerTransaction->commit();
T
Terry 已提交
254 255 256 257
                    return [
                        'code' => '200',
                        'content'=> 'add coupon success',
                    ];
258 259 260
                }
            } catch (Exception $e) {
                $innerTransaction->rollBack();
T
Terry 已提交
261 262 263 264
                return [
                    'code' => '401',
                    'content'=> 'fail',
                ];
265 266
            }
        } else {
T
Terry 已提交
267 268
            return [
                'status' => '402',
269
                'content'=> 'coupon is empty',
T
Terry 已提交
270
            ];
271 272
        }
    }
T
Terry 已提交
273
    
274 275
    public function actionUpdateinfo()
    {
T
Terry 已提交
276 277
        $item_id = Yii::$app->request->post('item_id');
        $up_type = Yii::$app->request->post('up_type');
278 279 280 281 282 283 284 285 286 287
        $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) {
T
Terry 已提交
288
                
289
                $innerTransaction->commit();
T
Terry 已提交
290 291 292 293
                return [
                    'code' => 200,
                    'content' => 'success'
                ];
294 295 296 297 298 299
            } else {
                $innerTransaction->rollBack();
            }
        } catch (Exception $e) {
            $innerTransaction->rollBack();
        }
T
Terry 已提交
300 301 302 303
        return [
            'code' => 401,
            'content' => 'update cart info  fail'
        ];
304 305
    }
}