首页 > 解决方案 > 为什么我的 Laravel 项目有 2 个不同 ID 的不同 Cart 对象?

问题描述

就像标题一样,我不知道为什么我的项目中有 2 个具有不同 ID 的 Cart 对象。你们能给我关于这个问题的任何想法吗?以及如何解决?非常感谢!

我使用相同的方式来获取 Session,但它们创建了 2 个不同的对象这里我有我的 getCheckOut() 函数

public function getCheckOut(Request $request){

        if(Auth::check()){
            if ($request->has('change_info')) {
                if($request->change_info == 'billing'){
                    $payer = $request->payer_name;
                    $payer_phone = $request->payer_phone;
                    $billing_address = $request->billing_address;
                    Session::put('checkout_info.payer',$payer);
                    Session::put('checkout_info.payer_phone',$payer_phone);
                    Session::put('checkout_info.billing_address',$billing_address);

                }elseif ($request->change_info == 'shipping') {
                    $receiver = $request->receiver_name;
                    $receiver_phone = $request->receiver_phone;
                    $shipping_address = $request->shipping_address;
                    Session::put('checkout_info.receiver',$receiver);
                    Session::put('checkout_info.receiver_phone',$receiver_phone);
                    Session::put('checkout_info.shipping_address',$shipping_address);
                }
            }

            $oldCart  = Session::get('cart');
            $cart = new Cart($oldCart);
            dd($cart);
            $checkout_info = Session::get('checkout_info');
            return view('customer.checkout',['cart' => $cart, 'items' => $cart->items, 'totalPrice' => $cart->totalPrice, 'checkout_info' => $checkout_info]);
        }else{
            Session::put('to_checkout',1);
            return redirect('login')->with('loi','Please login your account');
        }
    }

和我的 applyPromoCode() 函数

public function applyPromoCode(Request $request){

        $oldCart  = Session::get('cart');
        $cart = new Cart($oldCart);
        $codes = PromoCode::where('name',$request->promo_code)->get();

        if(count($codes) > 0){
            $code = $codes[0];
            if($code->fixed != 0){
                $amount = $code->fixed;
                if($cart->totalPrice > $amount){
                    $totalAfterDiscount = $cart->totalPrice - $amount;
                }elseif ($cart->totalPrice <= $amount) {
                    $totalAfterDiscount = 0;
                }
            }elseif ($code->percentage != 0) {
                $amount = $cart->totalPrice * $code->percentage / 100;
                $totalAfterDiscount = $cart->totalPrice - $amount;
            }
            $cart->discountAmount = $amount;
            $cart->totalAfterDiscount = $totalAfterDiscount;
            $cart->promoCode = 1;
            Session::put('cart',$cart);
            Session::save();

            // $checkout_info = Session::get('checkout_info');
            // $newCart = Session::get('cart');
            // $cart = new Cart($newCart);
            // dd($cart->promoCode);
            return redirect('checkout');
            // return view('customer.checkout',['cart' => $cart, 'items' => $cart->items, 'totalPrice' => $cart->totalPrice, 'checkout_info' => $checkout_info]);
        }else{
            return redirect('checkout')->with('loi','Your code is invalid');
        }

我使用 dd() 检查并得到了 2 个不同的购物车

getCheckOut() 得到这个购物车:

购物车#423

并且 applyPromoCode() 得到这个购物车

购物车#411

应用促销代码后

标签: phplaravelobjectshopping-cart

解决方案


2个对象有什么不同?


推荐阅读