Obviously the code from which you are putting this string "Your product is successfully added in the cart." into the session is being called over and over again whenever you refresh, so either by an ajax script or on page load or whatever. So make sure you track what is calling that function for you.
Session Flash Success continuously showing on some pages
I'm creating an e-commerce website. Its working fine in my localhost. But on server, sometimes add to cart button stopped working, and when I switch to another page, A success message appears again and again with this message
Your product is successfully added in the cart.
But cart is empty. I refresh the page open the website in guest windows. Cleared all cache and config but still I'm getting the same message on some product pages. I'm using Swiper.Jquery & Jquery.2.4. I think that jquery is overriding by any other script. Anyone can guide please. https://cleanshop.net/. You can check the errors from this website.
@Nakov It that possible the problem is because of incompatibility of Laravel and "bumbummen99/shoppingcart" versions?
@Sharim I'll never know unless I can try :)
@Nakov Sir can you please check the problem in this website. https://cleanshop.net/.
@Sharim just looking at the website I can tell what is going on in your code?
@Nakov The Route for cart is
// Cart
Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/cart', 'CartController@store')->name('cart.store');
Route::delete('/cart/{product}/{cart}', 'CartController@destroy')->name('cart.destroy');
The blade view
@if ($product->quantity > 0)
<form action="{{ route('cart.store') }}" method="POST">
@csrf()
<input type="hidden" name="id" value="{{ $product->id }}">
<input type="hidden" name="name" value="{{ $product->name }}">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="price" value="{{ $product->price }}">
<button type="submit" class="welcome-cart" title="Add to Cart" ><span>Add to Cart</span></button>
</form>
@endif
The Controller
public function store() {
session()->forget('coupon');
$duplicates = Cart::instance('default')->search(function($cartItem, $rowId) {
return $cartItem->id == request()->id;
});
$duplicatesLater = Cart::instance('specials')->search(function($cartItem, $rowId) {
return $cartItem->id == request()->id;
});
if($duplicates->isNotEmpty()) {
session()->flash('success', 'This Product is already in your cart');
return back();
} else if($duplicatesLater->isNotEmpty()) {
Cart::instance('specials')->remove($duplicatesLater->first()->rowId);
}
Cart::instance('default')->add(request()->id, request()->name, request()->quantity, request()->price)->associate('App\Product');
session()->flash('success', 'Your Product successfuly added to the cart');
return back();
}
@Sharim again my first reply from here applies. Inspect what is going on, you can open your "Network" tab in the developer tools, and see what causes the call to your store function over and over again, or check your project in case you are putting the same message to the session from somewhere else.
@Nakov In the Network tab when I click to add to cart, the status code is 302. Everything else is fine. And there is no request but flash messages that your product successfully added to the cart. But there is no product in the cart.
@Sharim The confusing thing for me is you are saying that there are no products in the cart and you get the message, while the code above should probably be called only when you are adding a new product in the cart, right?
So if you comment out this line :
session()->flash('success', 'Your Product successfuly added to the cart');
Do you still see a flash message? How are you printing the message in the view? is that why you use jQuery for?
What if you add:
@if(session('success'))
{{ session('success') }}
@endif
in your blade view, will you keep seeing that message?
@Nakov Sir that is my session file
@if (session()->has('success'))
<div class="alert alert-success">
<li>{{ session()->get('success') }}</li>
</div>
<?php
$totalItems = collect([Cart::instance('default')->content()->count(), Cart::instance('specials')->content()->count()])->sum();
?>
@if ($totalItems == 1)
<script>
document.getElementById("cart").classList.add('open');
</script>
@endif
{{ Session::forget('success') }}
@endif
@if (session()->has('primary'))
<div class="alert alert-info">
<li>{{ session()->get('primary') }}</li>
</div>
{{ Session::forget('primary') }}
@endif
@if (session()->has('warning'))
<div class="alert alert-warning">
<li>{{ session()->get('warning') }}</li>
</div>
{{ Session::forget('warning') }}
@endif
@if (session()->has('error'))
<div class="alert alert-danger">
<li>{{ session()->get('error') }}</li>
</div>
{{ Session::forget('error') }}
@endif
I @include it in the master layouts file on the top of the page.
@Nakov Sir in my console window the form action is showing "http" but ssl certificate is activated and url is secured with "https". May be that's why cart is not working good. Sometime it is showing page expired, some time server error, sometime connection failed. If it is the problem that how can I fix it?
@Sharim Yeah, that might be the problem.
You can add this in your AppServiceProvider:
use Illuminate\Support\Facades\URL;
// within the boot method:
URL::forceScheme('https');
@Nakov that ruin the whole website on localhost.
@Sharim you can wrap it:
if (app()->environment('production'))
{
URL::forceScheme('https');
}
@Nakov form action is still http. When I remove https:// from url of the website, it started working well. But the protocol is https:// its not working.
@Sharim is your APP_ENV set to production in the .env file? And do you have a valid certificate on the server?
Try php artisan config:clear in case you changed it and it still uses a cached version of the config.
@Nakov APP_ENV is set to production. but I'm facing security issues from ssl certificate.
How can I php artisan config:clear on my server.
@Sharim Does your host not give you shell access? Or ssh access?
@Sinnbeck I have cpanel. But I didn't use ssh ever.
Please or to participate in this conversation.