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.
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?
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.
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,
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)
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
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
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
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
public class CustomOrderLineCalculator : OrderLineCalculator {
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
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!
is working on a reply...