laravel shopping cart part 2

As I promised before, this is the second part of the article that I publish on creating a shopping cart with Laravel. In the previous one, we have completed some functionalities of the cart. They are add products to the cart and view cart. Now today I will show you how to implement the following functionalities to make it complete.

  • Remove a product from cart
  • Update cart quantity
  • Clear the whole cart

If you didn't read my previous article, the links is given below. That is a must that is to be read before this article!

Let's start!!!

shopping cart view

Step 1 - Create edit and remove functionalities for a product

The above picture shows the view of the cart we have implemented! So, for every product there's a delete button(trash icon) and a edit button(pen icon). These two buttons are responsible for the tasks meant by their own names. We have already configured the routes for these functionalities in routes/web.php file. Open it and check whether it contains the following routes.

Route::post('/update', 'CartController@update')->name('cart.update');
Route::post('/remove', 'CartController@remove')->name('cart.remove');

Open the CartController and just add the below code for implementation.

    public function remove(Request $request){
        \Cart::remove($request->id);
        return redirect()->route('cart.index')->with('success_msg', 'Item is removed!');
    }

    public function update(Request $request){
        \Cart::update($request->id,
            array(
                'quantity' => array(
                    'relative' => false,
                    'value' => $request->quantity
                ),
        ));
        return redirect()->route('cart.index')->with('success_msg', 'Cart is Updated!');
    }

Remove function takes ID from the incoming request and then it is passed to Cart package function called remove. Then route is redirected to the cart page with a alert message that indicating the product is removed!
Within the function for update, we have to perform an update only to change the quantity value. So it takes quantity from the request and send it to the Cart package update function. As I told before, route us then redirected.

Step 2 - Create function to clear the cart completely

Now we are going to implement the functionality to clear the whole cart at once instead of clearing one by one. For this we have to establish a new route. Configure the following route in your routes/web.php file.
NOTE: The routes MUST contain the names as you have mentioned them in your cart views. CHECK them! If you have done with me, you are pretty much safe!
Route::post('/clear', 'CartController@clear')->name('cart.clear');

Then we have to only add just 2 lines for CartController implementation. That is the magic of Laravel packages guys! They always simplify the implementations for developers and reduce the workload. Open CartController.php and modify the code.

    public function clear(){
        \Cart::clear();
        return redirect()->route('cart')->with('success_msg', 'Car is cleared!');
    }

Just one line do the job and the other is for redirecting to the cart page! How amazing is it? Imagine if we have to implement this from scratch?

So, basically now our cart is completed guys!

As I promised at the beginning of the previous article, I have completed the below functionalities related to the shopping cart.

  • View cart page
  • View cart contents in drop-down
  • Add products to cart
  • Remove products from cart
  • Update quantity of products
  • Clear cart

Hope you could complete the tutorial with me! If you have encountered any error, you can just drop a comment in the comments section! I will definitely help you!

Bye Guys!!!






45 Comments

  1. Thank you so much for this useful information. looking more from your side to update us on more updates and advancements

    ReplyDelete
    Replies
    1. Thanx Leo! I will update more in future..Keep it up with TechPool..

      Delete
  2. Nice tutorial.Thank you very much. Waiting for the updates and implements.

    ReplyDelete
  3. How to get this template and where?

    ReplyDelete
  4. Any planed updates on this? Kinda missing step 3 with paying and invoice generation :)

    ReplyDelete
    Replies
    1. Actually I'm not doing PHP work now..If I get time, will do :D

      Delete
  5. Me ayudó la parte de la actualización, yo estoy implementando el mismo paquete de shopping cart pero con VUE.. y es más complicado

    ReplyDelete
  6. Thank you for your article. Waiting for Checkout and Payment options.

    ReplyDelete
  7. thank you very much
    i followed one by one and boom thats good
    i will recommend this tutorial to other programmer :))
    goodjob i'll wait for next tutorial :))

    ReplyDelete
  8. You are the best servant of God with this great help.
    No site has worked so well on rare tutorials like shopping cards.

    ReplyDelete
  9. You are the best servant of God with this great help.
    No site has worked so well on rare tutorials like shopping cards.

    ReplyDelete
  10. thank you. btw, can you make a tutorial for checkout after this tutorial?

    ReplyDelete
  11. Hi i keep getting this error "Undefined variable: cartCollection"
    Pls help

    ReplyDelete
    Replies
    1. When trying to view cart..it keeps telling me the variable is undefined

      Delete
  12. thankyou so much keep going support

    ReplyDelete
  13. spectacular!

    seem so simple! because it is but what about the more advance stuff for laravel 7 like storing the cart, coupon % and fixed and add to cart with ajax. There's a lot outthere but always on laravel 5 or 6 which most don't work anymore. And of course the checkout and I went trhough everything as a tutorials but none have a reusable credit card customer order stripe checkout

    Thanks for this!

    ReplyDelete
  14. how to i can implement checkout(place order) functionality.
    please guide my

    ReplyDelete
  15. Cart is empty after logout....Please give me solution asap

    ReplyDelete
  16. Thank you. It's a good starting point for using this cart.

    ReplyDelete
  17. This article is a great article that I have seen in my career so far, it helps me a lot in my Laravel coding career, and will continue to do so in the future.

    website development company in Surat Gujarat

    ReplyDelete
  18. Thanks, please can you share the source code ?

    ReplyDelete
  19. can we access other fields like discription and other id field in this cart ?

    ReplyDelete
  20. please share part3 og shopping cart

    ReplyDelete
  21. Oh guys that's working . thanks a lot

    ReplyDelete
  22. Very good & informative content on topic
    Thanks for sharing such a useful tips.

    Visit our website forCRM software solution

    ReplyDelete
  23. I’m really happy to say it was an interesting post to read. I learned new information from your article on amity dissertation report
    , you are doing a great job

    ReplyDelete
  24. A web application developed based on the shopping cart migration concept will adopt the layout of the application.

    ReplyDelete
  25. useful information, keep sharing Data Entry also visit us.

    ReplyDelete
  26. This is the best essay I've ever read in my career; it has helped me tremendously and will continue to help me in my work as a Laravel programmer.Carlos Julio Heydra Castillo

    ReplyDelete
  27. Thanks for sharing such a useful tips.
    live score

    ReplyDelete