Manage shopping cart with Laravel Shopping cart

Tram Ho

Setting.

To install this package, you just need to run the following command: composer require gloudemans/shoppingcart In case you are using Laravel version 5.5 you will do one more step as follows: Go to app/config.php add to array providers as follows: GloudemansShoppingcartShoppingcartServiceProvider::class and aliases array as follows: ‘Cart’ => Gloudemans Shoppingcart Facades Cart :: class, `

The basic method.

1. Cart :: add ()

Adding products to the cart to add an item to the cart is very simple, you just use the add() , eg Cart::add('293ad', 'Product 1', 1, 9.99); Of course, you can still define parameters like ‘id, name, quantity’ if you want, for example: Cart::add(['id' => '293ad', 'name' => 'Product 1', 'quantity' => 1, 'price' => 9.99, 'options' => ['size' => 'M']]); These items after being added to the cart (Cart) it will be of type CartItem .

2. Cart :: get ()

This method allows us to retrieve a CartItem item from the shopping cart, for example, if I want to retrieve an item with an id of 293ad from the shopping cart, I will do the following: Cart::get('293ad');

3. Cart :: content ()

In case you want to get all the CartItem in the cart, this is a very useful function, when you call this function it will return a list of Collection of CartItem, Ex: Cart::content();

4. Cart :: update ()

To update an item in the cart you just specify the item you need to update by providing the id and the data you want to change as the following example I want to rename Product 1 to Sản Phẩm 1 : Cart::update('293ad', ['name' => 'Sản phẩm 1'])

5. Cart :: remove ()

To remove an item, you simply need to pass the id of the CartItem item you want to delete: Cart::remove('293ad'); Now the item with id of 293ad will be removed from your cart

6. Cart :: destroy ()

If you want to delete all items in the cart then you just do the following: Cart::destroy();

7. Cart :: total ()

This is a very useful method you do not need to perform any calculations and can still quickly calculate all products in the shopping cart with prices and quantities by: total($decimals, $decimalPoint, $thousandSeperator) This method will calculate and return a currency format

8. Cart :: count ()

In case you want to count how many products in your cart, you can use 2 ways as follows: Cart::count(); or Cart::content()->count(); Either of these will return you to the number of products in the cart

List of items in the cart.

A list containing CartItems is stored in a collection, this will be easy because you can easily manipulate the collection with common functions, eg Cart::content()->count(); Cart::content()->groupBy('id');

Link to Model.

This package is really handy for being able to link to a model from the items (CartItem) in your shopping cart, assuming you have a Product model using the associate() methods you can create. Linking to the Product model from here you can use the model’s resources from CartItem, to create links from CartItem to the model, do the following: Cart::associate($cartItem->rowId, 'Product'); or Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'])->associate('Product');

Conclude.

Actually this package is one of the shopping cart management packages that are very useful for developing e-commerce web applications, the methods are provided very complete and easy to use.

Refer.

https://github.com/Crinsane/LaravelShoppingcart

Share the news now

Source : Viblo