In out of the box Hybris, we cannot continue to checkout with an item in the cart that does not have any in stock. In this post, I show how to bypass stock level in Hybris during checkout without creating Stock Level.

If we do not have Stock level for a product and we add that product to the cart, then as soon we try to go to checkout, it will remove that item from the cart and throw the following error:

Unfortunately <product name> was removed from your cart as it is out of stock. You previously had <quantity> in your cart.

This error shows up when Hybris tries to call getStockLevel(cartEntryModel) and this method returns less than the amount added to the cart.

We will override this method so that it returns null. In SAP Hybris, if this method returns null, then Hybris will play safe. It will only allow quantity that was already in the cart. In other words, it will let us bypass the stock level constraint.

Add the following bean to the mycore-spring.xml:

 <alias name="defaultMyB2BCartValidationStrategy" alias="cartValidationStrategy"/>
<bean id="defaultMyB2BCartValidationStrategy"
class="com.my.core.service.cart.strategies.impl.DefaultMyB2BCartValidationStrategy"
parent="defaultB2BCartValidationStrategy"/>

Create the class DefaultMyB2BCartValidationStrategy that the defined defined:

import de.hybris.platform.b2bacceleratorservices.strategies.impl.DefaultB2BCartValidationStrategy;
import de.hybris.platform.core.model.order.CartEntryModel;

public class DefaultMyB2BCartValidationStrategy extends DefaultB2BCartValidationStrategy {

 /**
 * Overriden method to return null for stock level
 *
 * @param cartEntryModel
 * the cartEntryModel
 */
 @Override
 protected Long getStockLevel(CartEntryModel cartEntryModel) {
 return null;
 }
} 

This is the end of showing how to bypass stock level in Hybris during checkout without creating Stock Level. To view all of our posts on SAP Hybris, please click here.

For more information on Stock Level from SAP, view their documentations.

Categories: Hybris

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *