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

  1. AbstractOrderModel.java
  2. AddressModel deliveryAddress;
AbstractOrderModel.java
AddressModel deliveryAddress;


Since CartModel is a subclass of AbstractOrderModel, CartModel will also have delivery address(AddressModel)

Step 1

Load the cart associated with the session

  1. final CartModel cartModel = cartService().getSessionCart();
final CartModel cartModel = cartService().getSessionCart();


Make sure cartService is injected in the required class.

Step 2

Load all the addresses associated with user and delivery country using below code

  1. deliveryService.getSupportedDeliveryAddressesForOrder(cartModel, false)
deliveryService.getSupportedDeliveryAddressesForOrder(cartModel, false)


Make sure deliveryService is injected in the required class.

Step 3

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.

Step 4

Set AddressModel obtained in Step 3 to CartModel

  1. cartModel.setDeliveryAddress(addressModel);
cartModel.setDeliveryAddress(addressModel);

step 5

Save CartModel

  1. modelService().save(cartModel);
modelService().save(cartModel);



As an alternative to all the above steps, we can make single call to below method

  1. checkoutFacade.setDeliveryAddress(addressData);
checkoutFacade.setDeliveryAddress(addressData);


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.


Note :

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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on