Home ยป Laravel: Increase Quantity If Product Already Exists In Cart

Laravel: Increase Quantity If Product Already Exists In Cart

Last updated on December 24, 2020 by

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

  1. 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 */

  1. But the first-time session will have no data so the first time we will assign a product id and product quantity to a variable and we will pass that variable to the session.
$cart[$product_id]['qty'] = 1;

Session::put('cart', $cart);

  1. 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);

  1. 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:

  1. Best Way to Remove Public from URL in Laravel
  2. Run PHP Artisan Commands On Shared Hosting Servers
  3. How To Calculate Age From Birthdate
  4. Active Directory Using LDAP in PHP or Laravel
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Between Events and Observers In Laravel
  10. 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!

 
 

34 thoughts on “Laravel: Increase Quantity If Product Already Exists In Cart”

  1. I am genuinely pleased to read this weblog posts which includes lots of valuable facts, thanks for providing such statistics. Roseanna Frazier Pearla

    Reply
  2. 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

    Reply
  3. You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.

    Reply
  4. 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

    Reply
  5. 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.

    Reply
  6. Itโ€™s hard to find knowledgeable people about this subject, but you seem like you know what youโ€™re talking about! Thanks

    Reply
  7. 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!

    Reply
  8. 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!

    Reply
  9. 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.

    Reply
  10. 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.

    Reply
  11. 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!

    Reply
  12. 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.

    Reply
  13. 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?

    Reply

Leave a Comment