How to set the delivery address of a cart
Let us see how we can set delivery address to a cart
In Hybris data model, Cart is always associated with one delivery address
- AddressModel deliveryAddress;
AbstractOrderModel.java AddressModel deliveryAddress;
Since CartModel is a subclass of AbstractOrderModel, CartModel will also have delivery address(AddressModel)
Load the cart associated with the session
- final CartModel cartModel = cartService().getSessionCart();
final CartModel cartModel = cartService().getSessionCart();
Make sure cartService is injected in the required class.
Load all the addresses associated with user and delivery country using below code
- deliveryService.getSupportedDeliveryAddressesForOrder(cartModel, false)
Make sure deliveryService is injected in the required class.
load AddressModel for the given address_id (Need to pass address id from UI)
Note :This address_id has to be made available in UI and kept hidden if we select address from the list of address populated from backend.
Compare this address with all the available address loaded in Step 2, If its matching any address, then return that AddressModel
Note :For new address, we need to create new AddressModel and associate it to cart.
Set AddressModel obtained in Step 3 to CartModel
As an alternative to all the above steps, we can make single call to below method
But this method takes AddressData as a parameter, so prepare it and send
Make sure checkoutFacade is injected in the required class.
Now new address should be associated with the current cart.
One user can have any number of address in any ecommerce application
But it is always very much essential to have a cart with one valid delivery address