Hello Coders, Today we will see how to increase quantity if product already exists in cart. So without wasting a second, Let’s jump into it.
Prevent Duplicate Product In Cart In Laravel
- In the very first step, we will get the requested product data and get the data from the session.
$product_id = $request->all(); /*@ Generally, We pass only product id using ajax/axios but might vary based on requirement */
$cart = Session::get('cart'); /*@ First time session will be blank so we will add condition to check and increase product quantity according */
- But the first-time session will have no data so the first time we will assign a
product id
andproduct quantity
to a variable and we will pass that variable to the session.
$cart[$product_id]['qty'] = 1;
Session::put('cart', $cart);
- If the user increases the quantity or requests the same product to add into the cart then the session will have the product data so at that time we need to check if the session will have data then increase the quantity else assign the quantity to 1.
if(isset($cart[$product_id])):
$cart[$product_id]['qty'] += 1;
endif;
Session::put('cart', $cart);
- Now, Let’s join all the bits and pieces all together and make a addToCart function as below:
ProductController.php
<?php
/**
* Add product to the cart
*
* @return success message
*/
public function addToCart(Request $request){
$product_id = $request->all(); /*@ Generally, We pass only product id using ajax/axios but might vary based on requirement */
$cart = Session::get('cart'); /*@ First time session will be blank so we will add condition to check and increase product quantity according */
/*
* If product already exist into the cart then update QTY of product
* Othewise add new product into the cart
*/
if(isset($cart[$product_id])):
$cart[$product_id]['qty'] += 1;
else:
$cart[$product_id]['qty'] = 1;
endif;
Session::put('cart', $cart);
return Response::json(['success' => true, 'cart_items' => count(Session::get('cart')), 'message' => 'Cart updated.']);
}
Thatโs it for now. We hope this article helped you to learn how to increase quantity if product already exists in cart?
Additionally, read our guide:
- Best Way to Remove Public from URL in Laravel
- Run PHP Artisan Commands On Shared Hosting Servers
- How To Calculate Age From Birthdate
- Active Directory Using LDAP in PHP or Laravel
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Difference Between Events and Observers In Laravel
- Session Not Working In Laravel
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance ๐. Keep Smiling! Happy Coding!
I am genuinely pleased to read this weblog posts which includes lots of valuable facts, thanks for providing such statistics. Roseanna Frazier Pearla
We are very glad that this post is helpful for you. Thank you for reading this post.
Way cool! Some very valid points! I appreciate you penning this post plus the rest of the site is also really good. Susette Bald Hesta
We are very glad that this post is helpful for you. Thank you for reading this post.
Glad to be one of many visitants on this awe inspiring website :D. Bernice Clemmy Dola
We are glad that you like the blog. Thank you ๐
This article offers clear idea designed for the new users of blogging, that actually how to do blogging. Dodie Aldon Linis
Here is a great Blog You might Discover Intriguing that we encourage you to visit. Ulrike Jonas Isaac
Thank you for your kind word. It means us a lot ๐
You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.
Thank you for your repetitive & same comments.
Thank you for your kind word. It means us a lot ๐
Thank you for your kind word. It means us a lot ๐
Thank you for your kind word. It means us a lot ๐
Thank you for your kind word. It means us a lot ๐
Thank you for your repetitive & same comments
Thank you ๐
We are glad that you like our blog. And doing the same and repetitive comments. Thank you ๐
Hello colleagues, pleasant post and good arguments commented at this place, I am truly enjoying by these. Felicity Hillie Ahrens
We’re glad that you are enjoying our articles! Thank you ๐
This is my first time pay a quick visit at here and i am truly impressed to read everthing at alone place. Casandra Town Graig
We are glad that you are impressed. Thank you ๐
Hello! I simply would like to offer you a huge thumbs up for the excellent information you have here on this post. I will be coming back to your web site for more soon.
Itโs hard to find knowledgeable people about this subject, but you seem like you know what youโre talking about! Thanks
Congratulations on excellent lettering.
Hi! I’m at work surfing around your blog from my new iphone 3gs!
Just wanted to say I love reading through your blog and
look forward to all your posts! Carry on the outstanding work!
Hey, I think your site might be having browser compatibility issues.
When I look at your blog site in Chrome, it looks fine but
when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, fantastic blog!
Just desire to say your article is as astounding. The clarity to your post is simply great and i could
think you are an expert on this subject. Well together with
your permission allow me to snatch your RSS feed to keep up to date with approaching post.
Thank you a million and please continue the
enjoyable work.
Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam responses?
If so how do you stop it, any plugin or anything you can advise?
I get so much lately it’s driving me mad so any support is very much appreciated.
Hi this is kind of of off topic but I was wanting to know if
blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get guidance from
someone with experience. Any help would be greatly appreciated!
Excellent post. I was checking constantly this blog and I’m impressed!
Extremely helpful information specially the last part :
) I care for such info a lot. I was looking for this particular info
for a very long time. Thank you and good luck.
I was wondering if you ever thought of changing the structure of your
site? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could connect with
it better. Youve got an awful lot of text for only having one or two pictures.
Maybe you could space it out better?
Hi mates, its great paragraph regarding tutoringand entirely defined, keep it up all the time.
Thanks ๐