如何检查添加到购物车的产品是否有免费送货相互冲突
How to check if products added to cart have free deliveries conflict with each other
我正在与 Laravel 5.8 建立一个网上商店,在这个项目中,我为一些产品设置了免费送货。
另外还有两种包邮:城市包邮 & 国家包邮.
而且我还需要定义,如果用户选择了具有不同交付类型的多种产品(例如,一种产品具有“城市免费送货”而另一种具有“国家免费送货”),则消息必须显示说:
“该产品免费送货,但与您选择的其他产品的服务区域冲突。”
因此 carts.blade.php
页面应该如下所示:(预期结果)
但现在看起来像这样:
但这是错误的,因为这将检查单个产品是否有 city_free_delivery
和 country_free_delivery
。所以它returns没什么。
所以我的问题是,我如何检查是否 prd_delivery
的产品已插入购物车,是否有不同的送货区域服务?
这也是CartsController.php
的index
方法:
public function index()
{
$cart = $this->getOpenCartOrCreate();
// Get the all of send types and set the first send type as default for this cart
$submit_type = ProductSubmit::query()->get();
if(!empty($submit_type)) {
Cart::whereCrt_id($cart->crt_id)->where('crt_completed', 0)->where('crt_send_type', 0)->update(['crt_send_type' => $submit_type[0]->id]);
}
$cart = $this->getOpenCartOrCreate();
$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->latest()->first();
$foundOrder = json_decode($foundOrder->crt_content);
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
$prds[] = [
Product::with('uploaded')->wherePrdId($value->id)->select('*')->first(),
$value->quantity,
$value->price
];
}
} else {
$prds = [];
}
$cartPrice = json_decode($this->computeAndUpdateCartPrice())->cartPrice;
$cartDiscountedPrice = json_decode($this->computeAndUpdateCartPrice())->cartDiscountedPrice;
$sendPrice = $this->computeSendPackagePrice($cart->crt_send_type);
if (auth()->check()) {
$user = auth()->user()->usr_id;
$discounts = CouponUserUses::where('cart_id', $cart->crt_id)->where('user_id', $user)->get();
foreach ($discounts as $key => $discount) {
$discountedPrice = 0;
if ($discount->coupon_end_date < time()) {
$user_id = auth()->user()->usr_id;
$user_uses = CouponUserUses::where('id', $discount->id)->first();
$coupon_id = $user_uses->coupon_id;
$cart = $this->getOpenCartOrCreate();
$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->where('crt_completed', 0)->first();
$foundOrder = json_decode($foundOrder->crt_content);
$discountedTotal = 0;
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
if ($value->coupon == $coupon_id) {
$foundOrder[ $key ]->discounted = $value->price;
$foundOrder[ $key ]->coupon = 0;
}
}
Cart::whereCrt_id($cart->crt_id)->where('crt_completed', 0)->update(['crt_content' => json_encode($foundOrder)]);
}
$user_uses->delete();
}else {
foreach ($foundOrder as $index => $value) {
if ($discount->coupon_id == $value->coupon) {
$discountedPrice += ($value->price - $value->discounted) * $value->quantity;
}
}
$discount['discountedPrice'] = $discountedPrice;
}
}
$membershipDiscount = [
'type' => MembershipPlan::userTypePersian(),
'discounted' => MembershipPlan::calculateProductPrice($cartPrice)
];
} else {
$discounts = [];
$membershipDiscount = [];
}
return view('frontend.shop.carts.index', compact('prds', 'cartPrice', 'cartDiscountedPrice', 'cart', 'submit_type', 'sendPrice', 'discounts', 'membershipDiscount'));
}
如果我没理解错的话,您想在所有有冲突的产品上显示一条消息。所以我根据你的代码做了一些改变来做到这一点:
$conflicting = false;
$free_delivery = '';
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
$product = Product::with('uploaded')->wherePrdId($value->id)->select('*')->first();
// Just put a check here in case you have some non-free delivery types
if ($product->prd_delivery == 'city_free_delivery' || $product->prd_delivery == 'country_free_delivery') {
// Check if cart already have free delivery item and it conflict with current looping product or not
if ($free_delivery && $free_delivery != $product->prd_delivery) {
$conflicting = true;
}
$free_delivery = $product->prd_delivery;
}
$prds[] = [
$product,
$value->quantity,
$value->price
];
}
} else {
$prds = [];
}
...
return view('frontend.shop.carts.index', compact('conflicting', ...));
然后在视图中(我不知道你是如何渲染这些产品的,所以下面的代码只是为了举例):
@foreach($prds as $prd)
@if($conflicting && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
<p style="color:red;">
This product has free delivery but it conflicts the service area with other products that you have chosen.
</p>
@endif
@endforeach
我正在与 Laravel 5.8 建立一个网上商店,在这个项目中,我为一些产品设置了免费送货。
另外还有两种包邮:城市包邮 & 国家包邮.
而且我还需要定义,如果用户选择了具有不同交付类型的多种产品(例如,一种产品具有“城市免费送货”而另一种具有“国家免费送货”),则消息必须显示说:
“该产品免费送货,但与您选择的其他产品的服务区域冲突。”
因此 carts.blade.php
页面应该如下所示:(预期结果)
但现在看起来像这样:
但这是错误的,因为这将检查单个产品是否有 city_free_delivery
和 country_free_delivery
。所以它returns没什么。
所以我的问题是,我如何检查是否 prd_delivery
的产品已插入购物车,是否有不同的送货区域服务?
这也是CartsController.php
的index
方法:
public function index()
{
$cart = $this->getOpenCartOrCreate();
// Get the all of send types and set the first send type as default for this cart
$submit_type = ProductSubmit::query()->get();
if(!empty($submit_type)) {
Cart::whereCrt_id($cart->crt_id)->where('crt_completed', 0)->where('crt_send_type', 0)->update(['crt_send_type' => $submit_type[0]->id]);
}
$cart = $this->getOpenCartOrCreate();
$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->latest()->first();
$foundOrder = json_decode($foundOrder->crt_content);
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
$prds[] = [
Product::with('uploaded')->wherePrdId($value->id)->select('*')->first(),
$value->quantity,
$value->price
];
}
} else {
$prds = [];
}
$cartPrice = json_decode($this->computeAndUpdateCartPrice())->cartPrice;
$cartDiscountedPrice = json_decode($this->computeAndUpdateCartPrice())->cartDiscountedPrice;
$sendPrice = $this->computeSendPackagePrice($cart->crt_send_type);
if (auth()->check()) {
$user = auth()->user()->usr_id;
$discounts = CouponUserUses::where('cart_id', $cart->crt_id)->where('user_id', $user)->get();
foreach ($discounts as $key => $discount) {
$discountedPrice = 0;
if ($discount->coupon_end_date < time()) {
$user_id = auth()->user()->usr_id;
$user_uses = CouponUserUses::where('id', $discount->id)->first();
$coupon_id = $user_uses->coupon_id;
$cart = $this->getOpenCartOrCreate();
$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->where('crt_completed', 0)->first();
$foundOrder = json_decode($foundOrder->crt_content);
$discountedTotal = 0;
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
if ($value->coupon == $coupon_id) {
$foundOrder[ $key ]->discounted = $value->price;
$foundOrder[ $key ]->coupon = 0;
}
}
Cart::whereCrt_id($cart->crt_id)->where('crt_completed', 0)->update(['crt_content' => json_encode($foundOrder)]);
}
$user_uses->delete();
}else {
foreach ($foundOrder as $index => $value) {
if ($discount->coupon_id == $value->coupon) {
$discountedPrice += ($value->price - $value->discounted) * $value->quantity;
}
}
$discount['discountedPrice'] = $discountedPrice;
}
}
$membershipDiscount = [
'type' => MembershipPlan::userTypePersian(),
'discounted' => MembershipPlan::calculateProductPrice($cartPrice)
];
} else {
$discounts = [];
$membershipDiscount = [];
}
return view('frontend.shop.carts.index', compact('prds', 'cartPrice', 'cartDiscountedPrice', 'cart', 'submit_type', 'sendPrice', 'discounts', 'membershipDiscount'));
}
如果我没理解错的话,您想在所有有冲突的产品上显示一条消息。所以我根据你的代码做了一些改变来做到这一点:
$conflicting = false;
$free_delivery = '';
if ($foundOrder != null) {
foreach ($foundOrder as $key => $value) {
$product = Product::with('uploaded')->wherePrdId($value->id)->select('*')->first();
// Just put a check here in case you have some non-free delivery types
if ($product->prd_delivery == 'city_free_delivery' || $product->prd_delivery == 'country_free_delivery') {
// Check if cart already have free delivery item and it conflict with current looping product or not
if ($free_delivery && $free_delivery != $product->prd_delivery) {
$conflicting = true;
}
$free_delivery = $product->prd_delivery;
}
$prds[] = [
$product,
$value->quantity,
$value->price
];
}
} else {
$prds = [];
}
...
return view('frontend.shop.carts.index', compact('conflicting', ...));
然后在视图中(我不知道你是如何渲染这些产品的,所以下面的代码只是为了举例):
@foreach($prds as $prd)
@if($conflicting && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
<p style="color:red;">
This product has free delivery but it conflicts the service area with other products that you have chosen.
</p>
@endif
@endforeach