@ThreadSafe
public class CurrentCart
extends java.lang.Object
A shopping cart stores most of the information that an Order
does,
e.g. a shipping address, and it is essentially an Order in progress.
This means the CurrentCart is typically also used to implement checkout process.
Constructor and Description |
---|
CurrentCart(CartService cartService,
OrderService orderService,
java.util.Currency cartCurrency,
Cart.InventoryMode inventoryMode) |
Modifier and Type | Method and Description |
---|---|
Cart |
addCustomLineItem(LocalizedString name,
Money money,
java.lang.String slug,
ReferenceId<TaxCategory> taxCategory)
Adds a custom line item to the cart.
|
Cart |
addCustomLineItem(LocalizedString name,
Money money,
java.lang.String slug,
ReferenceId<TaxCategory> taxCategory,
int quantity)
Adds a custom line item with a specific quantity to the cart.
|
play.libs.F.Promise<SphereResult<Cart>> |
addCustomLineItemAsync(LocalizedString name,
Money money,
java.lang.String slug,
ReferenceId<TaxCategory> taxCategory)
Adds a custom line item to the cart asynchronously.
|
play.libs.F.Promise<SphereResult<Cart>> |
addCustomLineItemAsync(LocalizedString name,
Money money,
java.lang.String slug,
ReferenceId<TaxCategory> taxCategory,
int quantity)
Adds a custom line item with a specific quantity to the cart asynchronously.
|
Cart |
addLineItem(java.lang.String productId,
int quantity)
Adds product's master variant in given quantity to the cart.
|
Cart |
addLineItem(java.lang.String productId,
int variantId,
int quantity)
Adds a product variant in given quantity to the cart.
|
play.libs.F.Promise<SphereResult<Cart>> |
addLineItemAsync(java.lang.String productId,
int quantity)
Adds product's master variant in the given quantity to the cart asynchronously.
|
play.libs.F.Promise<SphereResult<Cart>> |
addLineItemAsync(java.lang.String productId,
int variantId,
int quantity)
Adds a product variant in given quantity to the cart asynchronously.
|
Cart |
clearShippingAddress()
Clears the shipping address.
|
play.libs.F.Promise<SphereResult<Cart>> |
clearShippingAddressAsync()
Clears the shipping address asynchronously.
|
java.lang.String |
createCartSnapshotId()
Creates an cart snapshot identifier for the final page of a checkout process.
|
Order |
createOrder(CreateOrderBuilder createOrderBuilder) |
Order |
createOrder(PaymentState paymentState)
Transforms the cart into an order.
|
Order |
createOrder(java.lang.String cartSnapshotId,
PaymentState paymentState)
Transforms the cart into an order.
|
play.libs.F.Promise<SphereResult<Order>> |
createOrderAsync(CreateOrderBuilder createOrderBuilder) |
play.libs.F.Promise<SphereResult<Order>> |
createOrderAsync(PaymentState paymentState)
Transforms the cart into an order asynchronously.
|
play.libs.F.Promise<SphereResult<Order>> |
createOrderAsync(java.lang.String cartSnapshotId,
PaymentState paymentState)
Transforms the cart into an order asynchronously.
|
Cart |
decreaseLineItemQuantity(java.lang.String lineItemId,
int quantity)
Decreases the quantity of given line item.
|
play.libs.F.Promise<SphereResult<Cart>> |
decreaseLineItemQuantityAsync(java.lang.String lineItemId,
int quantity)
Decreases the quantity of the given line item asynchronously.
|
Cart |
fetch()
Fetches the cart object for the current session asynchronously.
|
play.libs.F.Promise<Cart> |
fetchAsync()
Fetches the cart object for the current session asynchronously.
|
int |
getQuantity()
Returns the number of items in the cart.
|
boolean |
isSafeToCreateOrder(java.lang.String cartSnapshotId)
Returns true if the
cartSnapshotId matches the current cart id and version,
and therefore it is certain that the cart was not modified in a different browser tab during checkout. |
Cart |
recalculate()
Updates line item prices and tax rates.
|
play.libs.F.Promise<SphereResult<Cart>> |
recalculateAsync()
Updates line item prices and tax rates asynchronously.
|
Cart |
removeLineItem(java.lang.String lineItemId)
Removes a line item from the cart.
|
play.libs.F.Promise<SphereResult<Cart>> |
removeLineItemAsync(java.lang.String lineItemId)
Removes a line item from the cart asynchronously.
|
Cart |
setBillingAddress(Address address)
Sets the billing address.
|
play.libs.F.Promise<SphereResult<Cart>> |
setBillingAddressAsync(Address address)
Sets the billing address asynchronously.
|
Cart |
setCountry(com.neovisionaries.i18n.CountryCode country)
Sets the country of the cart and updates line item prices.
|
play.libs.F.Promise<SphereResult<Cart>> |
setCountryAsync(com.neovisionaries.i18n.CountryCode country)
Sets the country of the cart asynchronously and updates line item prices.
|
Cart |
setCustomerEmail(java.lang.String email)
Sets the customer email for the cart.
|
play.libs.F.Promise<SphereResult<Cart>> |
setCustomerEmailAsync(java.lang.String email)
Sets the customer email for the cart asynchronously.
|
Cart |
setCustomShippingMethod(java.lang.String shippingMethodName,
ShippingRate shippingRate,
ReferenceId<TaxCategory> taxCategory)
Sets a custom shipping method.
|
play.libs.F.Promise<SphereResult<Cart>> |
setCustomShippingMethodAsync(java.lang.String shippingMethodName,
ShippingRate shippingRate,
ReferenceId<TaxCategory> taxCategory)
Sets a custom shipping method of the cart asynchronously.
|
Cart |
setLineItemQuantity(java.lang.String lineItemId,
int quantity)
Sets the quantity of given line item.
|
play.libs.F.Promise<SphereResult<Cart>> |
setLineItemQuantityAsync(java.lang.String lineItemId,
int quantity)
Sets the quantity of the given line item asynchronously.
|
Cart |
setShippingAddress(Address address)
Sets the shipping address.
|
play.libs.F.Promise<SphereResult<Cart>> |
setShippingAddressAsync(Address address)
Sets the shipping address asynchronously.
|
Cart |
setShippingMethod(ReferenceId<ShippingMethod> shippingMethod)
Sets the shipping method of the cart.
|
play.libs.F.Promise<SphereResult<Cart>> |
setShippingMethodAsync(ReferenceId<ShippingMethod> shippingMethod)
Sets the shipping method of the cart asynchronously.
|
Cart |
update(CartUpdate update)
Updates the cart, doing several modifications using one request, described by the
update object. |
play.libs.F.Promise<SphereResult<Cart>> |
updateAsync(CartUpdate update)
Updates the cart asynchronously.
|
public CurrentCart(CartService cartService, OrderService orderService, java.util.Currency cartCurrency, Cart.InventoryMode inventoryMode)
public Cart fetch()
Note: As an optimization, the cart is only created in the backend when user adds a first product in the cart. For users who haven't put anything in their cart yet, this method returns an empty dummy cart object without going to the backend.
public play.libs.F.Promise<Cart> fetchAsync()
fetch.
public int getQuantity()
This method exists purely as an optimization that lets you avoid calling fetch
and then
cart.getTotalQuantity
, if you only need to display the number of items in the cart.
The quantity stored in Play's session and updated automatically on all cart modifications.
public Cart addLineItem(java.lang.String productId, int variantId, int quantity)
public play.libs.F.Promise<SphereResult<Cart>> addLineItemAsync(java.lang.String productId, int variantId, int quantity)
public Cart addLineItem(java.lang.String productId, int quantity)
public play.libs.F.Promise<SphereResult<Cart>> addLineItemAsync(java.lang.String productId, int quantity)
public Cart addCustomLineItem(LocalizedString name, Money money, java.lang.String slug, ReferenceId<TaxCategory> taxCategory)
public Cart addCustomLineItem(LocalizedString name, Money money, java.lang.String slug, ReferenceId<TaxCategory> taxCategory, int quantity)
public play.libs.F.Promise<SphereResult<Cart>> addCustomLineItemAsync(LocalizedString name, Money money, java.lang.String slug, ReferenceId<TaxCategory> taxCategory)
public play.libs.F.Promise<SphereResult<Cart>> addCustomLineItemAsync(LocalizedString name, Money money, java.lang.String slug, ReferenceId<TaxCategory> taxCategory, int quantity)
public Cart removeLineItem(java.lang.String lineItemId)
public play.libs.F.Promise<SphereResult<Cart>> removeLineItemAsync(java.lang.String lineItemId)
public Cart decreaseLineItemQuantity(java.lang.String lineItemId, int quantity)
public play.libs.F.Promise<SphereResult<Cart>> decreaseLineItemQuantityAsync(java.lang.String lineItemId, int quantity)
public Cart setLineItemQuantity(java.lang.String lineItemId, int quantity)
public play.libs.F.Promise<SphereResult<Cart>> setLineItemQuantityAsync(java.lang.String lineItemId, int quantity)
public Cart setCustomerEmail(java.lang.String email)
public play.libs.F.Promise<SphereResult<Cart>> setCustomerEmailAsync(java.lang.String email)
public Cart setShippingAddress(Address address)
null
is passed, the shipping address is cleared
(see clearShippingAddress
).public play.libs.F.Promise<SphereResult<Cart>> setShippingAddressAsync(Address address)
null
is passed, the shipping address is cleared
(see clearShippingAddressAsync
).public Cart clearShippingAddress()
public play.libs.F.Promise<SphereResult<Cart>> clearShippingAddressAsync()
public play.libs.F.Promise<SphereResult<Cart>> setBillingAddressAsync(Address address)
public Cart setCountry(com.neovisionaries.i18n.CountryCode country)
public play.libs.F.Promise<SphereResult<Cart>> setCountryAsync(com.neovisionaries.i18n.CountryCode country)
public Cart setShippingMethod(ReferenceId<ShippingMethod> shippingMethod)
public play.libs.F.Promise<SphereResult<Cart>> setShippingMethodAsync(ReferenceId<ShippingMethod> shippingMethod)
public Cart setCustomShippingMethod(java.lang.String shippingMethodName, ShippingRate shippingRate, ReferenceId<TaxCategory> taxCategory)
public play.libs.F.Promise<SphereResult<Cart>> setCustomShippingMethodAsync(java.lang.String shippingMethodName, ShippingRate shippingRate, ReferenceId<TaxCategory> taxCategory)
public Cart recalculate()
public play.libs.F.Promise<SphereResult<Cart>> recalculateAsync()
public java.lang.String createCartSnapshotId()
See the documentation of createOrder
for the rationale behind this method.
public boolean isSafeToCreateOrder(java.lang.String cartSnapshotId)
cartSnapshotId
matches the current cart id and version,
and therefore it is certain that the cart was not modified in a different browser tab during checkout.
You should always call this method before calling
createOrder
.
If this method returns false, the cart was most likely modified in a different browser tab. You should notify the customer and refresh the checkout page.
cartSnapshotId
- The identifier of the current checkout summary page.public Order createOrder(java.lang.String cartSnapshotId, PaymentState paymentState)
This method should be called when the customer decides to finalize the checkout.
Before this method is called, a checkout summary page will typically display contents of the current cart. There needs to be a mechanism to make sure the customer didn't add an item to the cart from a different browser tab right before ordering. In other words, we have to make sure that what the cart contains is exactly what is displayed.
The solution is to create a hidden HTML form input field storing an
id of a cart snapshot
, and providing the id to this
method when creating the order. If the cart doesn't match the provided snapshot id, this method
throws a CartModifiedException
.
Note on implementing payments:
This method can also be used in a server-to-server callback invoked by a payment
gateway. You should pass the a cartSnapshotId
to the payment gateway,
receive it back in the callback and pass it to this method.
cartSnapshotId
- A snapshot identifier of the cart from the time it was displayed to the customerpaymentState
- The payment state of the new orderCartModifiedException
- if the cartSnapshotId
doesn't match the state of the current cart anymore.OutOfStockException
- if some of the products in the cart are not available anymore.
This can only happen if the cart is in the
ReserveOnOrder
mode.PriceChangedException
- if the price, tax or shipping of some line items changed since the items
were added to the cart.public play.libs.F.Promise<SphereResult<Order>> createOrderAsync(java.lang.String cartSnapshotId, PaymentState paymentState)
cartSnapshotId
- A snapshot identifier of the cart from the time it was displayed to the customerpaymentState
- The payment state of the new order
A result which can fail with the following exceptions:
CartModifiedException
if the cartSnapshotId
doesn't match the state of the current cart anymore.
OutOfStockException
if some of the products in the cart are not available anymore.
This can only happen if the cart is in the
ReserveOnOrder
mode.
PriceChangedException
if the price, tax or shipping of some line items changed since the items
were added to the cart.
createOrder
public Order createOrder(PaymentState paymentState)
createOrder(String cartSnapshotId, PaymentState paymentState)
with the snapshot id.paymentState
- The payment state of the new order
* A result which can fail with the following exceptions:
CartModifiedException
if the cartSnapshotId
doesn't match the state of the current cart anymore.
OutOfStockException
if some of the products in the cart are not available anymore.
This can only happen if the cart is in the
ReserveOnOrder
mode.
PriceChangedException
if the price, tax or shipping of some line items changed since the items
were added to the cart.
public Order createOrder(CreateOrderBuilder createOrderBuilder)
public play.libs.F.Promise<SphereResult<Order>> createOrderAsync(PaymentState paymentState)
createOrder(String cartSnapshotId, PaymentState paymentState)
with the snapshot id.paymentState
- The payment state of the new order
* A result which can fail with the following exceptions:
CartModifiedException
if the cartSnapshotId
doesn't match the state of the current cart anymore.
OutOfStockException
if some of the products in the cart are not available anymore.
This can only happen if the cart is in the
ReserveOnOrder
mode.
PriceChangedException
if the price, tax or shipping of some line items changed since the items
were added to the cart.
public play.libs.F.Promise<SphereResult<Order>> createOrderAsync(CreateOrderBuilder createOrderBuilder)
public Cart update(CartUpdate update)
update
object.public play.libs.F.Promise<SphereResult<Cart>> updateAsync(CartUpdate update)