ITZone

Laravel – Build a simple shopping cart with bumbummen99

Introduce

If you are implementing an E-Commerce project, it is very important to build a shopping cart and manage it. Normally, if we code this function manually, it will take quite a while. To save effort, we can use available packages that support ShoppingCart such as Crinsane/LaravelShoppingcart , bumbummen99/shoppingcart , darryldecode/cart ,…

In this article, I will share with everyone the package to build a shopping cart that I have used before – bumbummen99/shoppingcart – about its features, usage and benefits. As of the time of writing, this Package is compatible with Laravel from version 8+ and below.

Setting

To install Package Bumbummen99 we run the following command:

To reconfigure Package Bumbummen we must publish it in Vendor via the following command:

This will give us the cart.php file in the config folder so we can make changes to its parameters.

Now the package is ready to start using the shopping cart build for the project.

Use

Declare

To use it, we first need to call it in the Controller where we write the logic for the shopping cart

Some important functions

Add product to cart

Package provides Cart::add() method to add to cart

In there:

  • id, name, qty, price, weight : are the basic attributes required of a Shopping Cart application, and are also required by Bumbummen. If no attribute is used, the value can be set to 0 and omitted, but deletion is not allowed.
  • options : additional attributes to use of your own if not declared above.

For example, for example, a product is a shirt , we need to pass in the method required values ​​such as id to store its id, name : name of the shirt, qty : the number of shirts the user chooses, price : price , weight : the shirt does not need mass so we can pass any value and don’t care about it. In addition, to order a shirt we need to add size, color, code, … we will add them to the options array.

We build a function to add a shirt, name it addToCart.

Let’s analyze it a bit. The user adds the shirt, the addToCart function is executed.

First, will find that shirt by $id and get fields like quantity , size from the view via Request . Then use the Cart::add() method and pass the attributes like the code above to add the product to the cart.

Show products in cart.

We use:

In there:

  • Cart::content() returns an array of cart products, each of which will be stored as an object. For example, try dd(Cart::content()) to see how it returns:

In addition to some of the fields I introduced above, in the picture there are some other fields such as rowId ,…

rowId will be automatically generated by the package after we use the add() method to add the product to the cart. rowId was born with the purpose to identify the product that has been added to the cart, it is used for some of the functions I will introduce below.

The main purpose of this method is to get the products in the cart that the user has addedToCart and to build the cart details to display to the user for example.

Take out a product in the cart

If you want to get an item from the cart using its rowId , you can simply call the get() method on the cart and pass it the rowId .

Calculate the total amount in the cart

Total before tax

Total after tax

Code:

In there:

  • Tax discount is calculated in %, and the default is 21% which is declared in the config file config/cart.php .
  • We can reconfigure by changing the value of field 'tax' => 21 to 'tax' => 8 according to VAT in Vietnam

Update cart

To update cart we can use following method

In there:

  • $ rowId : a random string of characters that is automatically generated to manage each product in the cart each time a new product is added to the cart.
  • $qty : new quantity to update

If you want to update the options in the options of an item in the cart, you can use

Clear cart

Delete 1 product

To remove an item from the cart, you’ll need the rowId again. This RowId you just need to pass to remove() method and it will remove the item from cart.

Clear the entire cart

If you want to completely remove the contents of the cart, you can call the destroy() method. This will remove all CartItems from the cart for the current cart session.

This method will help us in case the user successfully orders all the products in the cart and it will delete the entire cart.

Some other methods

Cart::weight()

  • to get the total weight of all items in the cart, based on their weight and quantity

Cart::tax()

  • get the total amount of tax charged for all items in the cart, based on price and quantity

Cart::count()

  • If you want to know how many items are in your cart, you can use the count() method. This method will return the total number of items in the cart. So if you added 2 books and 1 shirt, it will return 3 items.

Cart::search()

  • Search the product in the cart by its attribute

For example, if you want to find all items whose id is 1 Example

Some functions to set values ​​for Tax, Discount,…

You can learn more about some functions or understand more about functions at

https://packagist.org/packages/bumbummen99/shoppingcart

summary

The above article has summarized the functions and usage of the package bumbummen99 . I find this to be a pretty useful and easy-to-use package. Hope it helps with your coding process. If there is a mistake somewhere, I hope to receive everyone’s suggestions

See you in the next posts. Love!

References: https://packagist.org/packages/bumbummen99/shoppingcart

Share the news now