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
- AbstractOrderModel.java
- 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
- 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
- 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
- cartModel.setDeliveryAddress(addressModel);
cartModel.setDeliveryAddress(addressModel);
step 5
Save CartModel
- modelService().save(cartModel);
modelService().save(cartModel);
As an alternative to all the above steps, we can make single call to below method
- 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
Hi, Thanks for the posting the coding part. u are doing great job for us.
Sir, i want to add new field in registration page. how to do this task.can you plse provide some input on this task.what are the file has to be changed and updated or created.
Some details on PDP page, how product details page is loading.
thanks Sir,