Try our conversational search powered by Generative AI!

Validating coupon code

Vote:
 

Hi, 

In the checkout process of our commerce site, there's a textbox for submitting a coupon code. If you add a valid coupon code, the code is added to the MarketingProfileContext, the CartValidate workflow is run, and the cart is updated with the discount. 

The problem arises if you submit a coupon code that is not valid. At the moment, the user receives no notification of the coupon being invalid, the cart just refreshes without any changes. 

Ideally, I would like to check whether or not a coupon code is valid before adding it to the MarketingProfileContext and running the CartValidate workflow. Any ideas how this could be done?

Another solution would be to notify the user after the workflow is run, but that requires retrieving some sort of result from the CalculateDiscountActivity. Is that possible?

Thanks in advance!

Karoline

#89518
Aug 19, 2014 10:49
Vote:
 

Hi,

The suggested solution is:

- Run the Workflow to make sure the cart is calcuated correctly and coupon is applied.

- Get all the coupons:

foreach (OrderForm form in CartHelper.Cart.OrderForms)
{
foreach (var discount in form.Discounts.Cast().Where(x => !String.IsNullOrEmpty(x.DiscountCode)))
{
AddToDiscountList(discount);
}

foreach (LineItem item in form.LineItems)
{
foreach (var discount in item.Discounts.Cast().Where(x => !String.IsNullOrEmpty(x.DiscountCode)))
{
AddToDiscountList(discount);
}
}

foreach (Shipment shipment in form.Shipments)
{
foreach (var discount in shipment.Discounts.Cast().Where(x => !String.IsNullOrEmpty(x.DiscountCode)))
{
AddToDiscountList(discount);
}
}
}

- Then check if the discount list contain the any discont with DiscountCode equals the coupon code. If not, then coupon code is not valid, and you can display information for the user.

Regards.

/Q

#89527
Aug 19, 2014 12:54
Vote:
 

Hi, 

I completely forgot to update this thread on whether or not this worked, but here's the result: 

In order to validate coupon code do the following: 

1) Add coupon to cart

2) Validate cart

3) Run Quan Mai's code from above to check whether or not the coupon was actually added. 

And it works like a charm! 

Thanks :)

#90957
Sep 23, 2014 8:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.