Create a Shopping Cart with Laravel 6 - 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!!!
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.
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!
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!!!
50 Comments
Thank you so much for this useful information. looking more from your side to update us on more updates and advancements
ReplyDeleteThanx Leo! I will update more in future..Keep it up with TechPool..
DeleteNice tutorial.Thank you very much. Waiting for the updates and implements.
ReplyDeleteThanks Rachitha!
DeleteHow to get this template and where?
ReplyDeletetemplate means view code?
DeleteAny planed updates on this? Kinda missing step 3 with paying and invoice generation :)
ReplyDeleteActually I'm not doing PHP work now..If I get time, will do :D
DeleteMe ayudó la parte de la actualización, yo estoy implementando el mismo paquete de shopping cart pero con VUE.. y es más complicado
ReplyDeleteNice Article
ReplyDeleteThank you for your article. Waiting for Checkout and Payment options.
ReplyDeleteThank you for your feedback :D
Deletethank you very much
ReplyDeletei followed one by one and boom thats good
i will recommend this tutorial to other programmer :))
goodjob i'll wait for next tutorial :))
Thank you very much :D
DeleteYou are the best servant of God with this great help.
ReplyDeleteNo site has worked so well on rare tutorials like shopping cards.
Thank you very much :D
DeleteYou are the best servant of God with this great help.
ReplyDeleteNo site has worked so well on rare tutorials like shopping cards.
Thank you very much :D
Deletethank you. btw, can you make a tutorial for checkout after this tutorial?
ReplyDeleteactually i'm not trying out with php now :(
DeleteHi i keep getting this error "Undefined variable: cartCollection"
ReplyDeletePls help
When trying to view cart..it keeps telling me the variable is undefined
Deletethankyou so much keep going support
ReplyDeleteyou're welcome! keep in touch :D
Deletespectacular!
ReplyDeleteseem 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!
Thanks for the update <3
DeleteAmazing Bro
ReplyDeletehow to i can implement checkout(place order) functionality.
ReplyDeleteplease guide my
Cart is empty after logout....Please give me solution asap
ReplyDeleteThank you. It's a good starting point for using this cart.
ReplyDeleteThank You :D
DeleteThis 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.
ReplyDeletewebsite development company in Surat Gujarat
Glad to hear such a feedback :D Keep in touch!
DeleteThanks for the feedback :D
ReplyDeleteThanks, please can you share the source code ?
ReplyDeletecan we access other fields like discription and other id field in this cart ?
ReplyDeleteplease share part3 og shopping cart
ReplyDeleteOh guys that's working . thanks a lot
ReplyDeleteglad to hear
DeleteVery good & informative content on topic
ReplyDeleteThanks for sharing such a useful tips.
Visit our website forCRM software solution
I’m really happy to say it was an interesting post to read. I learned new information from your article on amity dissertation report
ReplyDelete, you are doing a great job
A web application developed based on the shopping cart migration concept will adopt the layout of the application.
ReplyDeleteuseful information, keep sharing Data Entry also visit us.
ReplyDeleteThis 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
ReplyDeleteThanks for sharing such a useful tips.
ReplyDeletelive score
Get top-notch Law assignment help from qualified experts. We offer personalized support to enhance your understanding and improve your grades in legal coursework.
ReplyDelete11Xplay is your go-to platform for an unparalleled online gaming experience, offering sports betting and casino games designed specifically for Indian players. Enjoy live sports betting, favorite games such as Teen Patti and Rummy, along with fast, secure payments and instant withdrawals. The platform boasts a user-friendly interface and 24/7 customer support to ensure a hassle-free experience. Explore other top-notch platforms like Lotusbhai and Betbook250 to elevate your gaming journey to the next level!
ReplyDeleteWelcome to 1xbet – your trusted gateway to 1xBet’s world of gaming and entertainment. We provide a seamless, secure login experience for users to access live sports, casino games, and global events with ease.
ReplyDeleteT10 Exchange offers seamless online gaming and sports exchange experiences. With a user-friendly interface, diverse game options, secure transactions, and 24/7 customer support, it’s the go-to platform for enthusiasts. Explore innovative features and join a trusted community to enhance your gaming journey.
ReplyDeleteSatsport is your ultimate destination for cutting-edge sports insights and services in India. Offering a seamless platform for sports enthusiasts, we specialize in delivering real-time updates, secure transactions, and an unmatched user experience. Discover the best of sports engagement with Satsports today!"
ReplyDelete