mardi 2 août 2022

"Invalid argument supplied for foreach()", in laravel controller

I want to add coupon code system in e commerce website using laravel 5.8 when i click on apply coupon button it gives me error like "Invalid argument supplied for foreach()" the problem is with that foreach loop in my controller when i remove it then it shows 0 price can anybody help me to solve this problem

here is my controller

   public function applycoupon(Request $request){
    $coupon = $request->input('coupon_code');
    if(Coupons::where('coupon_code',$coupon)->exists())
    {
      $coupon = Coupons::where('coupon_code',$coupon)->first();
     if($coupon->start_date<=Carbon::today()&& Carbon::today()<= $coupon->end_date){
        $totalprice = "0";
        $cookie_data = stripslashes(Cookie::get('shoping_cart'));
        $cart_data = json_decode($cookie_data,true);
        foreach ($cart_data as $itemdata){
            $product = Product::find($itemdata['item_id']);
            $prod_price = $product->offer_price;
           // $prod_price_tax = ($prod_price*$product->tax)/100;
            $totalprice = $totalprice+($itemdata['item_quantity']*$prod_price);
        }

        //checking Type (percentage or amount)
       if($coupon->coupon_type=='Percentage')
       {
        $discount_price = ($totalprice/100)*$coupon->coupon_price;
       }
       elseif($coupon->coupon_price=='amount'||'Amount')
       {
         $discount_price = $coupon->coupon_price;
       }
       $grand_total = $totalprice-$discount_price;
       return response()->json([
         'discount_price' =>$discount_price,
          'grand_total' =>$grand_total,
       ]);
     }
     else
     {
     return response()->json([
        'status'=> 'coupon code has been expired',
        'error_status'=>'error'
      ]);
      }
    }
    else
    {
        return response()->json([
            'status'=> 'coupon code does not exists',
            'error_status'=>'error'
          ]); 
    }
}

views.blade file

 <div class="card-body">
                    <div class="d-flex justify-content-between mb-3 pt-1">
                        <h6 class="font-weight-medium">Subtotal</h6>
                        <h6 class="font-weight-medium">$</h6>
                    </div>
                    <div class="d-flex justify-content-between">
                        <h6 class="font-weight-medium">Discount</h6>
                        <h6 class="font-weight-medium discount_price">0.00</h6>
                    </div>
                </div>

Route

Route::post('apply-coupons','CouponController@applycoupon');

custom js file

custom js file

    //Apply coupons
$(document).ready(function () {
    $('.apply-coupon-btn').click(function (e) {
        e.preventDefault();
        var coupon_code = $('.coupon_code').val();
        if ($.trim(coupon_code).length == 0) {
            error_coupon = "please enter valid coupon";
            $('#error_coupon').text(error_coupon);
        } else {
            error_coupon = "";
            $('#error_coupon').text(error_coupon);
        }
        if (error_coupon != '') {
            return false;
        }
        $.ajax({
            method: "POST",
            url: "/apply-coupons",
            data: {
                'coupon_code': coupon_code,
            },
            success: function (response) {
                if (response.error_status == 'error') {
                    alertify.set('notifier', 'position', 'top-right');
                    alertify.success(response.status);
                    $('.coupon_code').val('');
                }
                else{
                    var discount_price = response.discount_price;
                    var grand_total = response.grand_total;
                    $('.discount_price').text(discount_price);
                    $('.grand_total').text(grand_total);
                }
            }
        });

    });
});


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/g1WkEc0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire