cancel
Showing results for 
Search instead for 
Did you mean: 

Override the addToCart price

Former Member
0 Kudos

Hi,

We need custom logic for the addToCart price, we have several price rows with an additional field, based on that field we know which price row we should use. However Hybris doesn't understand this custom logic, so we need to override it. But how can we override the addToCart price?

I already tried to override the class CommercePriceService with their methods getPriceFromProduct() and getWebPriceFromProduct() and that has influence on the base price, but it hasn't any influence on the addToCart price.

How should we handle this?

Thanks in advance.

Edit: I found the page https://wiki.hybris.com/display/release5/Extending+CommerceCartService where is explained that you can hook the addToCart mechanism. Ofcourse I could add this and call the afterAddToCart method to recalculate the prices. Is this the best way to solve this issue?

Edit2: Someone mentioned that I should override (and extend) the Europe1PriceFactory and than only override the method 'matchPriceRowsForInfo' and that should be enough. Is this the best solution to solve this?

View Entire Topic
Former Member
0 Kudos

I solved this problem by overriding the Europe1PriceFactory. First you should create your own PriceFactory and extend the Europe1PriceFactory and than you should override the current priceFactory in aN aftersessionevent.

CustomPriceFactory

 package com.custom.b2b.hybris.core.factory;
 
 import de.hybris.platform.europe1.jalo.Europe1PriceFactory;
 import de.hybris.platform.europe1.jalo.PriceRow;
 import de.hybris.platform.jalo.JaloInvalidParameterException;
 import de.hybris.platform.jalo.SessionContext;
 import de.hybris.platform.jalo.c2l.Currency;
 import de.hybris.platform.jalo.enumeration.EnumerationValue;
 import de.hybris.platform.jalo.order.AbstractOrder;
 import de.hybris.platform.jalo.order.AbstractOrderEntry;
 import de.hybris.platform.jalo.order.price.JaloPriceFactoryException;
 import de.hybris.platform.jalo.order.price.PriceFactory;
 import de.hybris.platform.jalo.order.price.ProductPriceInformations;
 import de.hybris.platform.jalo.product.Product;
 import de.hybris.platform.jalo.product.Unit;
 import de.hybris.platform.jalo.security.JaloSecurityException;
 import de.hybris.platform.jalo.user.User;
 import de.hybris.platform.util.PriceValue;
 import de.hybris.platform.util.localization.Localization;
 
 import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
 import org.apache.log4j.Logger;
 
 import com.custom.b2b.hybris.core.enums.PriceRowTypeEnum;
 
 
 public class CustomPriceFactory extends Europe1PriceFactory
 {
 
     private final PriceFactory originalPriceFactory = Europe1PriceFactory.getInstance();
     private final Logger logger = Logger.getLogger(CustomPriceFactory.class);
 
     @Override
     public de.hybris.platform.europe1.jalo.PriceRow matchPriceRowForPrice(final SessionContext ctx, final Product product,
             final EnumerationValue productGroup, final User user, final EnumerationValue userGroup, final long qtd, final Unit unit,
             final Currency currency, final Date date, final boolean net, final boolean giveAwayMode)
             throws JaloPriceFactoryException
     {
             // Override the matchPriceRowForPrice here
         //        return matchPriceRowForPrice(ctx, product, productGroup, user, userGroup, qtd, unit, currency, date, net, giveAwayMode);
     }

     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getAllPriceInformations(de.hybris.platform.jalo.SessionContext,
      * de.hybris.platform.jalo.product.Product, java.util.Date, boolean)
      */
     @Override
     public ProductPriceInformations getAllPriceInformations(final SessionContext paramSessionContext, final Product paramProduct,
             final Date paramDate, final boolean paramBoolean) throws JaloPriceFactoryException
     {
 
         return originalPriceFactory.getAllPriceInformations(paramSessionContext, paramProduct, paramDate, paramBoolean);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getBasePrice(de.hybris.platform.jalo.order.AbstractOrderEntry)
      */
     @Override
     public PriceValue getBasePrice(final AbstractOrderEntry entry) throws JaloPriceFactoryException
     {    
         //Override this method and call the matchPriceRowForPrice that you also should override
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getDiscountValues(de.hybris.platform.jalo.order.AbstractOrderEntry
      * )
      */
     @Override
     public List getDiscountValues(final AbstractOrderEntry paramAbstractOrderEntry) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getDiscountValues(paramAbstractOrderEntry);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getDiscountValues(de.hybris.platform.jalo.order.AbstractOrder)
      */
     @Override
     public List getDiscountValues(final AbstractOrder paramAbstractOrder) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getDiscountValues(paramAbstractOrder);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getProductDiscountInformations(de.hybris.platform.jalo.SessionContext
      * , de.hybris.platform.jalo.product.Product, java.util.Date, boolean)
      */
     @Override
     public List getProductDiscountInformations(final SessionContext paramSessionContext, final Product paramProduct,
             final Date paramDate, final boolean paramBoolean) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getProductDiscountInformations(paramSessionContext, paramProduct, paramDate, paramBoolean);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getProductPriceInformations(de.hybris.platform.jalo.SessionContext
      * , de.hybris.platform.jalo.product.Product, java.util.Date, boolean)
      */
     @Override
     public List getProductPriceInformations(final SessionContext paramSessionContext, final Product paramProduct,
             final Date paramDate, final boolean paramBoolean) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getProductPriceInformations(paramSessionContext, paramProduct, paramDate, paramBoolean);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getProductTaxInformations(de.hybris.platform.jalo.SessionContext,
      * de.hybris.platform.jalo.product.Product, java.util.Date)
      */
     @Override
     public List getProductTaxInformations(final SessionContext paramSessionContext, final Product paramProduct,
             final Date paramDate) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getProductTaxInformations(paramSessionContext, paramProduct, paramDate);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see
      * de.hybris.platform.jalo.order.price.PriceFactory#getTaxValues(de.hybris.platform.jalo.order.AbstractOrderEntry)
      */
     @Override
     public Collection getTaxValues(final AbstractOrderEntry paramAbstractOrderEntry) throws JaloPriceFactoryException
     {
         return originalPriceFactory.getTaxValues(paramAbstractOrderEntry);
     }
 
     /*
      * (non-Javadoc)
      * 
      * @see de.hybris.platform.jalo.order.price.PriceFactory#isNetUser(de.hybris.platform.jalo.user.User)
      */
     @Override
     public boolean isNetUser(final User paramUser)
     {
         return originalPriceFactory.isNetUser(paramUser);
     }
 
 }

AfterSessionEvent

customcore-spring.xml

 <bean id="AfterSessionCreationEventCustomPriceFactoryDecorator"
       class="com.custom.b2b.hybris.core.event.AfterSessionCreationEventCustomPriceFactoryDecorator">
            <property name="priority" value="10"/>
     </bean>

AfterSessionCreationEventCustomPriceFactoryDecorator Class public class AfterSessionCreationEventCustomPriceFactoryDecorator extends AbstractEventDecorator {

     @Override
     public AfterSessionCreationEvent decorate(final AfterSessionCreationEvent event)
     {
         final JaloSession jaloSession = (JaloSession) event.getSource();
         final CustomPriceFactory customPriceFactory = new CustomPriceFactory();
         jaloSession.setPriceFactory(customPriceFactory);
 
         return event;
     }
 }
Former Member
0 Kudos

Where is the AbstractEventDecorator?

Former Member
0 Kudos

How I can have retrieveChannelStrategy. We have only the setter and variable is private. setRetrieveChannelStrategy(RetrieveChannelStrategy retrieveChannelStrategy)

Please advice,

hmabdul
Explorer
0 Kudos

instead of AbstractEventDecorator we can use AbstractEventListener.

hmabdul
Explorer
0 Kudos

instead of AbstractEventDecorator we can use AbstractEventListener