Skip to content
Advertisement

Laravel 5.8: Can you help me with this problem

I’m using Laravel 5.8 to develop my Online Store. And in this store, every product has a field of prd_delivery and it can be set one of these values:

city_free_delivery

country_free_delivery

null

When it is null, it means that it does not have any free delivery type.

And when users select their products, these situations must be applied:

capture

Note that the row City means, if user lives at the city and the row Country means, if he lives at outside of city (villages).

These situations applies to cart.blade.php, so I tried this:

@if($conflicting  && ($prd[0]->prd_delivery == 'country_free_delivery'))
    @php
        Session::forget('city');
        Session::forget('city_country');
        Session::put('country' , '1')
    @endphp
@endif

@if($conflicting  && ($prd[0]->prd_delivery == 'city_free_delivery'))
    @php
        Session::forget('country');
        Session::put('city' , '2');
    @endphp
@endif

@if($conflicting  && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
    @php
        Session::forget('country');
        Session::forget('city');
        Session::forget('free_notfree');
        Session::put('city_country' , '0');
        Session::put('free_notfree','13');
    @endphp
@endif

@if($conflicting  && ($prd[0]->prd_delivery == '' || $prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
    @php
        if((session('free_notfree') != '13')){
            Session::forget('country');
            Session::forget('city_country');
            Session::put('free_notfree' , '3');
        }
    @endphp
@endif

And at the Controller, I have this:

$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', ...));

So by this way I can check price of delivery (it can be free or not free), depends on the user location and user cart products.

For example at checkout.blade.php, if he’s location is city, this will run

@if(Session()->has('city') || Session()->has('city_country') || Session()->has('country'))
    Free
@else
    {{ $item->price . ' Dollar'}}
@endif

Finally, the situations work fine but except the last one which is this:

enter image description here

In fact, it shows FREE to users, while the {{ $item->price . ' Dollar'}} must be shown!

So how to print price at the last situation ?

I know this question was a little but confusing, but I tried to explain the problem the best way. Because I’m on my wits with this.

I would really appreciate any idea or suggestion from you guys.

Thanks in advance.

Advertisement

Answer

The problem is you are setting session value inside a loop so only last loop value applied. With this requirement, you can create an array to store all delivery types of products in the cart then set session value depend on that array. And you should put your session setting logic in controller, not view.

$delivery_types = [];
$conflicting = false;

if ($foundOrder != null) {
  foreach ($foundOrder as $key => $value) {
    $product = Product::with('uploaded')->wherePrdId($value->id)->select('*')->first();

    array_push($delivery_types, $product->prd_delivery);

    $prds[] = [
       $product,
       $value->quantity,
       $value->price
    ];
  }

  $delivery_types = array_unique($delivery_types);

  // Your conflicting check here
  if (in_array('country_free_delivery', $delivery_types) && in_array('city_free_delivery', $delivery_types)) {
     $conflicting = true;
  }

  // Free logic check here
  if (in_array(null, $delivery_types)) {
     Session::put('free' , 'no');
  } else if (in_array('country_free_delivery', $delivery_types)) {
     Session::put('free' , 'country');
  } else {
     Session::put('free' , 'city');
  }

} else {
    $prds = [];
}
...

And then in your checkout.blade.php:

@if (session('free') == 'no)
  {{ $item->price . ' Dollar'}}
@else
  // Depend on user's location
@endIf
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement