php - 为什么我的 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() 得到这个购物车:
并且 applyPromoCode() 得到这个购物车
应用促销代码后
解决方案
2个对象有什么不同?
推荐阅读
- python - pandas DataFrame.to_sql 和 nan 值
- kotlin - TornadoFX 指南:如何实现 Customer 类
- python - 尝试使用 atom 运行脚本时出错
- jquery - 如何在某些元素上触发代码?但不影响其他人
- .net-core - Azure DevOps 上的 Dotnet Core NuGet 包依赖项构建问题
- java - 比较两个结果集中的元数据
- c# - 仅当另一个通过时才运行 C# 单元测试
- sonos - Sonos API - 是否可以通过 API 播放特定歌曲
- r - 共享完全置信区间的区域(基本图形)
- php - < 和 > 不显示在字符串中?