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 π