Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Oct 29, 2015 @ 15:20
    Laurence Gillian
    0

    v3.0 - Adding discounts to orders (legacy support)

    Having upgraded to TeaCommerce v3, I am having to adjust some existing code to support a discounts system we wrote for TeaCommerce.

    Essentially, we have special 'discount' products, which can contain a discount, this can be of value e.g. £10 or a percentage based e.g. 10%.

    For the percentage discount to work, we need to grab the total order value, however it seems like this is no longer accessible in the 'CalculateOrderLineTotalPrice' method.

    I refactored the existing code and moved into the OrderTotalPrice override method, however, this doesn't properly affect the total price. (see: ol.UnitPrice.Value and ol.TotalPrice.Value inside foreach)

    protected override Price CalculateOrderTotalPrice(Order order, Currency currency)
        {
            Promotions promos = Promotions.GetInstance(); // node
            decimal fullPrice = order.TotalPrice.Value.Value;
            string pc = order.Properties["promocode"];
    
            var promoLines = order.OrderLines.Where(ol => ol.ProductIdentifier == promos.Id.ToString(CultureInfo.InvariantCulture));  // select promo items
    
            foreach (OrderLine ol in promoLines)
            {
                PromoCode pco = promos.PromoCodes.Where(c => String.Equals(c.Code, pc, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); // match specific promo code
                // Test is Promotional Code is still Valid
                if (pco != null && pco.ExpiryDate >= DateTime.Now && pco.MinimumOrderValue <= fullPrice)
                {
                    decimal newPrice = ol.UnitPrice.Value.Value;
                    if (pco.DiscountValue > 0)
                    {
                        newPrice = pco.DiscountValue * -1;
                    }
                    else
                    { 
                        newPrice = (fullPrice * (pco.DiscountPercentage / 100)) * -1; 
                    }
                    ol.UnitPrice.Value = new Price(newPrice, order.VatRate, currency);
                    ol.TotalPrice.Value = new Price(newPrice, order.VatRate, currency);
                }
            }
    
            return base.CalculateOrderTotalPrice(order, currency);
        }
    

    Any ideas on correctly setting the Price, or getting access to the Order within "CalculateOrderLineTotalPrice(OrderLine orderLine, Currency currency)" would be really appericated.

    Thanks, Laurie

  • Anders Burla 2560 posts 8256 karma points
    Nov 02, 2015 @ 09:16
    Anders Burla
    0

    Hi Laurence

    The calculators in newest v3 gets the order as a parameter in the method to calc the price - BUT remember that thr order is calculated from the order lines, the sub total, shipping, payment and then total. So you dont have the order total when you calculate the order lines. Properly better to make an award - or does the license not allow you to use the marketing features?

    Kind regards

    Anders

  • Laurence Gillian 600 posts 1219 karma points
    Nov 02, 2015 @ 14:28
    Laurence Gillian
    0

    No problem, I'll migrate my existing promo codes into the new system.

    I'm doing some work with overriding a price on OrderLines, I have taken the example from the Documentation website, and added to my solution, but with no changes to the code it errors.

    Any ideas? Many thanks, Laurie

    Documentation
    Image showing error

    Error   24  'TeaCommerce.Tests.PriceCalculators.CustomOrderLineCalculator.CalculatePrice(TeaCommerce.Api.Models.OrderLine, TeaCommerce.Api.Models.Currency, TeaCommerce.Api.Models.Order)': cannot change access modifiers when overriding 'public' inherited member 'TeaCommerce.Api.PriceCalculators.OrderLineCalculator.CalculatePrice(TeaCommerce.Api.Models.OrderLine, TeaCommerce.Api.Models.Currency, TeaCommerce.Api.Models.Order)' C:\Git\vdb.1024.gatherandsee\Src\gs.shop.Logic\EventHandlers\TeaCommerce\ProductSizePriceLookup.cs  18  34  gs.shop.Logic
    
  • Anders Burla 2560 posts 8256 karma points
    Nov 03, 2015 @ 08:16
    Anders Burla
    0

    Hi Laurence

    Try and remove the method or constructor and override again - maybe the documentation is a bit out dated - let us know when you get it working

  • Laurence Gillian 600 posts 1219 karma points
    Nov 03, 2015 @ 14:38
    Laurence Gillian
    0
    using TeaCommerce.Api.Dependency;
    using TeaCommerce.Api.Models;
    using TeaCommerce.Api.PriceCalculators;
    using TeaCommerce.Api.Services;
    
    namespace TeaCommerce.Tests.PriceCalculators {
      [SuppressDependency(     "TeaCommerce.Api.PriceCalculators.IOrderLineCalculator",   "TeaCommerce.Api" )]
    

    public class CustomOrderLineCalculator : OrderLineCalculator {

    public CustomOrderLineCalculator( IVatGroupService vatGroupService, IProductInformationExtractor productInformationExtractor )
      : base( vatGroupService, productInformationExtractor  ) {
    }
    
    protected override Price CalculatePrice( OrderLine orderLine, Currency currency, Order order ) {
      return new Price( 100M, orderLine.VatRate, currency );
    }
    
     }
     }
    

    Changing: protected override Price CalculatePrice

    To: public override Price CalculatePrice

    Resolves the issue, can I update the documentation via the Git repo if it's there? I did a little pull request the other day too,

    Thanks! Laurie

  • Anders Burla 2560 posts 8256 karma points
    Nov 04, 2015 @ 09:22
    Anders Burla
    0

    Great you got it solved. I will change on the documentation portal - its an Umbraco website.

    I saw the pull request and next release of Tea Commerce will add some of the pull requests that we have got. THANKS!

Please Sign in or register to post replies

Write your reply to:

Draft