Developer forum

Forum » Development » Check if voucher discount is applied to the cart

Check if voucher discount is applied to the cart

John Broers
Reply

I'm working on a feature that needs to determine whether a discount or voucher code has been applied to the current cart or order. While the order calculation itself functions as expected, I want to implement a true/false validation that provides a clear explanation if the voucher code isn't applied.

Single-use voucher code
We have a voucher code that can only be used once per user. I need to display a message when a user tries to reuse the code, informing them that it has already been redeemed. Currently, the API we are using only checks if the voucher exists and is active (Dynamicweb.Ecommerce.Orders.SalesDiscounts.PromoCodeChecker.CheckOrderPromoCode(vouchercode)) but does not verify whether the user has previously used it.

Multiple discounts logic
We have set up multiple order discounts and configured the system in the Advanced Settings to apply only the highest-value discount. If a user enters a voucher code, and a higher-value discount is already applied to the order, I want to show a message explaining that the voucher wasn't applied because a better discount is already in place.

How can I implement checks to handle these two scenarios? Any advice or suggestions would be appreciated.

BR,

John


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi John,

 

Discount validations in general

When using Discounts you can enable a check for "Check for Validation reason" which will then render you a list of reasons why a certain discount has not been applied

DISCLAIMER: Only enable this for the Discounts you want to present reasons for. There is a performance overhead in using these validations.

 

 

Single-use voucher code

Using the "Check for Validation reason" you can actually look for the particular Discount Id (where the single-use voucher is configured) and you can then pick the message associated with the needed validation.

If you want to use the API itself, then what you can do is to check for the user's Orders and if any orders that have used the Discount where the Discount list is assigned to. Here's an example.

var hasUsedSingleUseVoucher = Dynamicweb.Ecommerce.Services.Orders.GetAll().Any(o => o.CustomerAccessUserId == Pageview.User.ID && !o.Deleted && o.Complete && o.OrderLines.Any(ol => ol.DiscountId == "MyDiscountId"));

 

Another way that this is a bit more readable is this

var allUserOrders = Dynamicweb.Ecommerce.Services.Orders.GetAll().Where(o => o.CustomerAccessUserId == Pageview.User.ID);
var allUserValidOrders = allUserOrders.Where(o => !o.Deleted && o.Complete);
var hasUsedSingleUseVoucher = allUserValidOrders.Any(o => o.OrderLines.Any(ol => ol.DiscountId == "MyDiscountId"));

 

 

Multiple discount logic

I'm not sure if the "Check for Validation reason" will give you the answer that "The voucher is not applied because a better discount has been triggered" kind of message, but I would look for it there first.

 

Let me know what other questions you might have.

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum