Overview
Carts are a collection of products (or other custom purchasable types) that you would like to order. Carts belong to Users (which relate to Customers).Cart prices are dynamically calculated and are not stored (unlike Orders).
Carts
Creating a cart
Cart Lines
Validation
When adding items to a cart there are a series of validation actions which are run, which are defined in theconfig/lunar/cart.php config file.
These actions will throw a Lunar\Exceptions\Carts\CartException.
Hydrating the cart totals
Modifying Carts
If you need to programmatically change the Cart values, e.g. custom discounts or prices, you will want to extend the Cart. You can find out more in the Extending Lunar section for Cart Extending.Calculating Tax
During the cart’s lifetime, it’s unlikely you will have access to any address information, which can be a pain when you want to accurately display the amount of tax applied to the basket. Moreover, some countries don’t even show tax until they reach the checkout. We’ve tried to make this as easy and extendable as possible for you as the developer to build your store. When you calculate the cart totals, you will be able to set the billing and/or shipping address on the cart, which will then be used when we calculate which tax breakdowns should be applied.\Lunar\Models\Address model, or even another
\Lunar\Models\CartAddress
Cart Session Manager
When building a store, you’re going to want an easy way to fetch the cart for the current user (or guest user) by retrieving it from their current session. Lunar provides an easy to use class to make this easier for you, so you don’t have to keep reinventing the wheel.Available config
Configuration for your cart is handled inlunar/cart.php
There is additional, separate, config specifically for when using the
CartSession located in lunar/cart_session.php.
Getting the cart session instance
You can either use the facade or inject theCartSession into your code.
Fetching the current cart
null if they
don’t have a cart, or you want to create one straight away. By default, we do
not create them initially as this could lead to a ton of cart models being
created for no good reason. If you want to enable this functionality, you can
adjust the config in lunar/cart_session.php
Forgetting the cart
Forgetting the cart will remove it from the user session and also soft-delete the cart in the database.Using a specific cart
You may want to manually specify which cart should be used for the session.Add a cart line
Add multiple lines
collection or an array
Update a single line
Update multiple lines
Remove a line
Clear a cart
This will remove all lines from the cart.Associating a cart to a user
You can easily associate a cart to a user.Associating a cart to a customer
You can easily associate a cart to a customer.Adding shipping/billing address
As outlined above, you can add shipping / billing addresses to the cart using the following methods:ShippingOption override
In some cases you might want to present an estimated shipping cost without users having to fill out a full shipping address, this is where theShippingOptionOverride comes in, if set on the cart it can be used to calculate shipping for a single request.
getEstimatedShipping will be used when calculating shipping totals, bypassing any other logic, note this will only happen for that one request.
If you are using the CartSession manager, you can easily set the parameters you want to estimate shipping so you don’t need to pass them each time:
CartSession::current() by itself won’t trigger the shipping override, but you can pass the estimateShipping parameter to enable it:
Handling User Login
When a user logs in, you will likely want to check if they have a cart associated to their account and use that, or if they have started a cart as a guest and logged in, you will likely want to be able to handle this. Lunar takes the pain out of this by listening to the authentication events and responding automatically by associating any previous guest cart they may have had and, depending on yourauth_policy merge or override the basket on their account.
Determining cart changes
Carts by nature are dynamic, which means anything can change at any moment. This means it can be quite challenging to determine whether a card has changed from the one currently loaded, for example, if the user goes to check out and changes their cart on another tab, how does the checkout know there has been a change? To help this, a cart will have a fingerprint generated which you can check to determine whether there has been any changes and if so, refresh the cart.Changing the underlying class.
The class which generates the fingerprint is referenced inconfig/lunar/cart.php.
Pruning cart data
Over time you will experience a build up of carts in your database that you may want to regularly remove. You can enable automatic removal of these carts using thelunar.carts.prune_tables.enabled config. By setting this to true any carts without an order associated will be removed after 90 days.
You can change the number of days carts are retained for using the lunar.carts.prune_tables. prune_interval config.
If you have specific needs around pruning you can also change the lunar.carts.prune_tables.pipelines array to determine what carts should be removed.