Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Logan P. 47 posts 217 karma points
    Nov 17, 2014 @ 18:47
    Logan P.
    0

    Custom Shipping Calculator hits constructor but not CalculatePrice

    Hello, I am running teacommerce 2.3.2 and Umbraco 7.1.4 and am having trouble getting either CalculatePrice methods to fire. The constructor gets executed but not the CalulatePrice method. I have also overridden the OrderCalculator and even tried to call the CalculateShippingCosts manually, with no luck. I am out of ideas. Here is the custom ShippingCalculator.

    [SuppressDependency("TeaCommerce.Api.PriceCalculators.IShippingCalculator", "TeaCommerce.Api")]
    public class BritaxShippingCalculator : ShippingCalculator
    {
    
        public BritaxShippingCalculator( IVatGroupService vatGroupService, ICurrencyService currencyService )
            : base( vatGroupService, currencyService ) 
        {
            int something = 1;
        }
    
    
        protected override Price CalculatePrice( ShippingMethod shippingMethod, Currency currency, long countryId, long? countryRegionId, VatRate vatRate ) 
        {
          //Estimated shipping price - no order is present
          return new Price(6.95M, vatRate, currency);
        }
    
        protected override Price CalculatePrice( ShippingMethod shippingMethod, Currency currency, Order order ) 
        {
          //Calculate price based on order
            CustomProperty shippingTotalProp = order.Properties.Where(x => x.Alias.Equals("ShippingTotal")).FirstOrDefault();
    
    
            if(shippingTotalProp == null)
            {
                return new Price(ShippingHelper.GetLowestShippingCost(), order.VatRate, currency);
            }
    
            decimal shippingTotal;
    
            if(!decimal.TryParse(shippingTotalProp.Value, out shippingTotal))
            {
                return new Price(ShippingHelper.GetLowestShippingCost(), order.VatRate, currency);
            }
    
            return new Price(shippingTotal, order.VatRate, currency);
        }
    }
    
  • Anders Burla 2560 posts 8256 karma points
    Nov 18, 2014 @ 10:46
    Anders Burla
    0

    Hi Logan

    Could you try and make a really simple calculator that just returns a hardcoded price of e.g. 1000 and 2000. Just so we are sure that non of the custom code has errors and make things fail.

    Kind regards
    Anders

  • Logan P. 47 posts 217 karma points
    Nov 18, 2014 @ 13:44
    Logan P.
    0

    Hi Anders, I copied the two methods from the documentation and gave that a shot. Still no luck with that method getting hit. Any other ideas what could be going on? I checked the logs in the App_Data/Logs and I don't see anything about TeaCommerce.

    Thanks!

    Logan

  • Anders Burla 2560 posts 8256 karma points
    Nov 18, 2014 @ 14:37
    Anders Burla
    0

    And how do you test it? You need to change the order so it will save it. The calculation is done everytime an order is CHANGED and saved. If nothing changes for the order, nothing new is calculated.

    Kind regards
    Anders

  • Logan P. 47 posts 217 karma points
    Nov 18, 2014 @ 14:58
    Logan P.
    0

    I have added breakpoints in Visual Studio. Whenever I change an order, I can break in the CalculateOrderTotalPrice method on the order calculator, but the CalculatePrice method on the shipping calculator does not get called. I would have expected the Shipping calculator to have been called before the CalculateOrderTotalPrice, correct? Then just to be sure, I added a call to CalculateShippingCosts in the CalculateOrderTotalPrice method. I also have several order saves throughout my ordering process that should be triggering the Shipping Calculator. I know things on the order are changing because it doesn't even fire whenever I add order lines to my order.

    Thanks!

  • Anders Burla 2560 posts 8256 karma points
    Nov 19, 2014 @ 12:56
    Anders Burla
    0

    So you have your own order calculator? Which methods have you overridden?

  • Logan P. 47 posts 217 karma points
    Nov 19, 2014 @ 13:44
    Logan P.
    0

    Hi Anders, thanks a lot for you help thus far. As you can see below in the code snippet, I have overridden the CalculateOrderLineUnitPrice and CalculateOrderTotalPrice methods. Everything on the OrderCalculator is working great. I hope something that I am doing in here will give you a clue as to what might be going on. Thanks again!

    [SuppressDependency("TeaCommerce.Api.PriceCalculators.IOrderCalculator", "TeaCommerce.Api")]
    public class BritaxOrderCalculator : OrderCalculator
    {
        public BritaxOrderCalculator( IVatGroupService vatGroupService, ICurrencyService currencyService, IShippingMethodService shippingMethodService, IPaymentMethodService paymentMethodService, IShippingCalculator shippingCalculator, IPaymentCalculator paymentCalculator, IProductInformationExtractor productInformationExtractor )
            : base( vatGroupService, currencyService, shippingMethodService, paymentMethodService, shippingCalculator, paymentCalculator, productInformationExtractor ) 
        {
        }
    
    
        protected override Price CalculateOrderLineUnitPrice(OriginalUnitPrice originalUnitPrice, OrderLine orderLine, Currency currency, Order order ) 
        {
    
          decimal salePrice;
          if(decimal.TryParse(orderLine.Properties["salePrice"], out salePrice))
          {
              try
              {
                  originalUnitPrice = orderLine.OriginalUnitPrices[1];
                  //If it is on sale, give them the sale price.
                  if (salePrice < originalUnitPrice.Value)
                  {
                      return new Price(salePrice, order.VatRate, currency);
                  }
              }
              catch (Exception e)
              {
                  throw new Exception("There was a problem getting the price for this product. Please contact Customer Service.");
              }
    
    
          }
          //Something with the sale price went wrong or else there is no sale. Give them the
          //regular price.
          return new Price(originalUnitPrice.Value, order.VatRate, currency);
        }
    
    
    
        protected override Price CalculateOrderTotalPrice(Price subtotalPrice, Price shipmentTotalPrice, Price paymentTotalPrice, PriceWithoutVat transactionFee, Currency currency, Order order)
        {
            //Put this in here for debugging. I thought this would fire the shipping calculator. No luck.
            CalculateShippingCosts(order);
            //CalculateTotals(order);
    
            decimal discountAmount = OrderHelper.GetDiscountAmount(order);
    
            //calclulated without the vat. That is done automatically.
            Decimal totalPrice = subtotalPrice.Value + shipmentTotalPrice.Value + transactionFee.Value - discountAmount;
    
            return new Price(totalPrice, order.VatRate, currency);
        }        
    }
    
  • Anders Burla 2560 posts 8256 karma points
    Nov 19, 2014 @ 14:26
    Anders Burla
    0

    Could you try and not use your custom order calculator and see if the shipping stuff then gets called?

    Kind regards
    Anders

  • Logan P. 47 posts 217 karma points
    Oct 19, 2016 @ 17:42
    Logan P.
    100

    Better to answer 2 years later than never right? The problem turned out to be a combination of two things.

    1) You must call base.CalculateOrderTotalPrice(order, currency); in your override of CalculateOrderTotalPrice

    2) You must have your order.ShipmentInformation.ShippmentMethodId set in order for your custom shipping calculator to fire.

Please Sign in or register to post replies

Write your reply to:

Draft