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!

 
 

22 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

Leave a Comment