-
Notifications
You must be signed in to change notification settings - Fork 1
Available methods
The shoppingcart gives you the following methods to use:
Adding an item to the cart is really simple, you just use the add()
method, which accepts a variety of parameters.
In its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.
Cart::add('293ad', 'Product 1', 1, 9.99);
As an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size.
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
The add()
method will return an CartItem instance of the item you just added to the cart.
Maybe you prefer to add the item using an array? As long as the array contains the required keys, you can pass it to the method. The options key is optional.
Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);
New in version 2 of the package is the possibility to work with the Buyable
interface. The way this works is that you have a model implement the Buyable
interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model.
This way you can just pass the add()
method a model and the quantity and it will automatically add it to the cart.
As an added bonus it will automatically associate the model with the CartItem
Cart::add($product, 1, ['size' => 'large']);
As an optional third parameter you can add options.
Cart::add($product, 1, ['size' => 'large']);
Finally, you can also add multipe items to the cart at once.
You can just pass the add()
method an array of arrays, or an array of Buyables and they will be added to the cart.
When adding multiple items to the cart, the add()
method will return an array of CartItems.
Cart::add([
['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
]);
Cart::add([$product1, $product2]);
To update an item in the cart, you'll first need the rowId of the item.
Next you can use the update()
method to update it.
If you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::update($rowId, 2); // Will update the quantity
If you want to update more attributes of the item, you can either pass the update method an array or a Buyable
as the second parameter. This way you can update all information of the item with the given rowId.
Cart::update($rowId, ['name' => 'Product 1']); // Will update the name
Cart::update($rowId, $product); // Will update the id, name and price
To remove an item for the cart, you'll again need the rowId. This rowId you simply pass to the remove()
method and it will remove the item from the cart.
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::remove($rowId);
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.
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::get($rowId);
Of course you also want to get the carts content. This is where you'll use the content
method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.
Cart::content();
This method will return the content of the current cart instance, if you want the content of another instance, simply chain the calls.
Cart::instance('wishlist')->content();
If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove all CartItems from the cart for the current cart instance.
Cart::destroy();
The total()
method can be used to get the calculated total of all items in the cart, given there price and quantity. Includes any additional costs too.
Cart::total(): float
Return a formatted total of all items.
Cart::totalFormat(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeparator = null): string
The tax()
method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.
Cart::tax(): float
Return a formatted tax amount of all items in the cart.
Cart::taxFormat(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeparator = null): string
The subtotal()
method can be used to get the total of all items in the cart, minus the total amount of tax or costs.
Cart::subtotal(): float
Return a formatted subtotal of a cart.
Cart::subtotalFormat(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeparator = null): string
If you want to know how many items there 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've added 2 books and 1 shirt, it will return 3 items.
Cart::count();
To find an item in the cart, you can use the search()
method.
This method was changed on version 2
Behind the scenes, the method simply uses the filter method of the Laravel Collection class. This means you must pass it a Closure in which you'll specify you search terms.
If you for instance want to find all items with an id of 1:
$cart->search(function ($cartItem, $rowId) {
return $cartItem->id === 1;
});
As you can see the Closure will receive two parameters. The first is the CartItem to perform the check against. The second parameter is the rowId of this CartItem.
The method will return a Collection containing all CartItems that where found
This way of searching gives you total control over the search process and gives you the ability to create very precise and specific searches.
If you want to add additional costs to the cart you can use the cost()
method. The method accepts a cost name or predefined type and the price of the cost. This can be used for eg shipping or transaction costs.
To get a cost again later, leave out the $price
parameter (or set to null)
Cart::cost(string|CostType $type, ?float $price = null): void|float
Add this method before summarizing the whole cart. The costs are not saved in the session (yet).
After adding a cost, you are able to format it to your liking, with the method costFormat()
.
Returns the formatted price of the cost.
Cart::costFormat(string|CostType $type, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeparator = null): string