Developer forum

Forum » Development » Custom DiscountProvider?

Custom DiscountProvider?

Niels Foldager
Reply

Hi Dynamicweb,

We would like to be able to exclude some products that are part of an order discount, if certain data on the individual product means that no discount may be granted on it.

In the old discount module we could create our own SalesDiscountProvider and override ProcessOrder and then check the product on each order line.

When we use the new discount module, we do not have this option. By using DiscountExtender, we can only validate the entire order discount, but not manipulate it.

Would you be able to offer the same options for overriding the order discounts or do you have other suggestions?

Best regards
Niels Foldager


Replies

 
Nicolai Pedersen
Reply

Hi Niels

If you override the DiscountValidForProduct and return false on that one for products with the data that indicates that no discount can be applied, order discounts will not be calculated for that product.

I.e. if you have 2 products in the cart, and a voucher giving 25% on the order, the 25% would only be given to one of the orderlines if your return false for the other product.

BR Nicolai

 
Niels Foldager
Reply

Hi Nicolai,

DiscountValidForProduct does not appear to be hit when adding a DiscountExtender to an order discount. Only DiscountValidForOrder will be hit. So it does not seems to be a solution. DiscountValidForProduct is only going to be hit when using product discounts.

Best regards
Niels Foldager

 
Nicolai Pedersen
Reply

According to code, the extender is called once for the order (ValidForOrder) and once for each orderline that is a product (ValidForProduct) (not loyalty, giftcards and  other stuff).

If other rules on the product discounts does not apply, the ValidForProduct is never called.

See dumps.

Screenshot_2021-01-21_161434.JPG Screenshot_2021-01-21_161455.JPG
 
Niels Foldager
Reply

Hi Nicolai,

The validation of the individual product in an order discount must not be dependent on the product also being included in an OrderLineDiscount (GetDiscountOrderLine includes an orderline from the order.ProductOrderLines loop).

I think the code shows that DiscountValidForProduct is only called when the product is included in one or more OrderLineDiscounts. Then there is no validation of a product when it is only included in an OrderDiscount?

Would it be posible to validate each orderline in an OrderDiscount?

Best regards
Niels Foldager

 
Nicolai Pedersen
Reply

Can you please post the setup of your discount?

You have an order discount that includes all products from group XX? And except some of the products, with a property set, the discount applies?

 
Niels Foldager
Reply

Hi Nicolai,

This is the only active discount in the solution. A DiscountExtender is attached to the discount (see the source code below). All products from group XX could have been included, but in this example I only have two products. Only one of the products may be included in the discount (because of the SpOnSale-property), but both are included.

DiscountExtender:

 
Nicolai Pedersen
Reply

Hi Niels

Alright - I think I start to understand what you are trying to do. You want to affect the amount used for calculating the discount?

Which you currently cannot. That requires a change to how the base price of a discount is calculated.

Why is this not a product discount?

BR Nicolai

 
Niels Foldager
Reply

Hi Nicolai,

Yes you are right. I don't know why our customer want to use order discount instead of product discount. Maybe discount is already applied to some products or maybe they don't want to have a discount order line for each product in the cart.

We hope you can find a solution for this.

Best regards
Niels Foldager

 
Nicolai Pedersen
Reply

Well, it would work as a product discount.

Try the attached ecommerce.dll. It will use the DicountValidForProduct to decide if an orderline is included in the base prices used to calculate the order discount. It will affect the entire orderline regardless of the quantity. 

 
Niels Foldager
Reply

Hi Nicolai,

It almost works. Now only the first product is included when calculating the order discount, because of property on the second product.

But if both products are also linked to some product discounts, then the order discount is calculated incorrectly, as both product discounts are included in the order subtotal amount.

This cart is correct:


This cart is not correct:


Best regards
Niels Foldager

 
Nicolai Pedersen
Reply

You probably have to change your order discount "Apply as" to be an Order discount (Exclusive) instead of an Inclusive. 

BR Nicolai

 
Nicolai Pedersen
Reply

Wrong answer... That is not it.

 
Nicolai Pedersen
Reply

Try the attached assembly

 
Niels Foldager
Reply

Hi Nicolai,

Yes, now it works when "Apply as" is "Order discount (Exclusive)" smiley

The product discount from the excluded product is no longer included when the order discount is calclulated.

Best regards
Niels Foldager

 
Niels Foldager
Reply

Hi Nicolai,

Are there any plans to launch these changes in Dynamicweb 9.9.X and 9.10.X?

Best regards
Niels Foldager

 
Nicolai Pedersen
Reply

TFS#90483 - merged to 9.10 and 9.9 branch. Curently in code review and test.

 
Niels Foldager
Reply

Thanks, Nicolai

Best regards
Niels Foldager

 

You must be logged in to post in the forum