Copied to clipboard

Flag this post as spam?

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


  • Rune Grønkjær 1372 posts 3103 karma points MVP
    Feb 22, 2022 @ 12:28
    Rune Grønkjær
    0

    No discount when purchasing certain products (Giftcards)

    Hi,

    The shop we are creating are, amongst a lot of physical stuff, selling gift cards. Under no circomstance can there be added a discount to gift card order lines. And to the total amounts for that sake if it means discounting the gift card. You cannot get 10% off when buying a gift card for example.

    Is there a pipeline or event or whatever that I can use to do this? Or do I have to make a custom discount rule that prevents this? A discount rule would mean that the editor needs to remember to put this on all campaigns in the correct way, and you know you cannot trust an editor :)

    Thanks /Rune

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Feb 22, 2022 @ 13:45
    Matt Brailsford
    100

    Hey Rune,

    So you could probably do it as a 2 pronged attack using validation event handlers for both ValidateOrderProductAdd and ValidateOrderDiscountCodeRedeem preventing on from being added if the other is present, or you could also do it in a single ValidateOrderSave event handler checking to see if there are any discounts + gift card order lines.

    See docs here for some more info on validation event handlers https://vendr.net/docs/core/2.1.0/umbraco-v9/key-concepts/events/#validation-events

    Hope this helps

    Matt

  • Rune Grønkjær 1372 posts 3103 karma points MVP
    Feb 22, 2022 @ 14:11
    Rune Grønkjær
    0

    Ah ok. So you would simple block the saving of the order if the user is trying to do both discount and gift card. That could work yes.

    I will try it and get back to you with the result so others might learn something.

    /Rune

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Feb 22, 2022 @ 14:14
    Matt Brailsford
    0

    Yea, I think this would be the simplest option without needing to create your own custom discount rule. 👍

    Matt

  • Rune Grønkjær 1372 posts 3103 karma points MVP
    Feb 24, 2022 @ 13:18
    Rune Grønkjær
    0

    EDIT: There was a small bug in the script. Which have been fixed

    It worked like a charm. There's some error handling (everywhere) in the UI but that's what you can expect.

    Here's my code:

    public class ValidateOrderSaveHandler : ValidationEventHandlerBase<ValidateOrderSave> {
    private readonly IUnScopedDictionaryService _dictionaryService;
    
    public ValidateOrderSaveHandler( IUnScopedDictionaryService dictionaryService ) {
      _dictionaryService = dictionaryService;
    }
    
    public override void Validate( ValidateOrderSave evt ) {
      IEnumerable<OrderLineReadOnly> giftCardOrderLines = evt.Order.OrderLines.Where( ol => ol.IsGiftCard() );
      if ( giftCardOrderLines.Any() ) {
        var test = giftCardOrderLines.Where( ol => ol.UnitPrice.Adjustments.Count > 0 || ol.TotalPrice.Adjustments.Count > 0 ).ToList();
        if ( giftCardOrderLines.Any( ol => ol.UnitPrice.WithoutAdjustments.WithoutTax != ol.UnitPrice.Value.WithoutTax || ol.TotalPrice.WithoutAdjustments.WithoutTax != ol.TotalPrice.Value.WithoutTax )
             || ( evt.Order.SubtotalPrice.Adjustments.Count > 0 || evt.Order.TotalPrice.Adjustments.Count > 0 ) ) {
          evt.Fail( _dictionaryService.GetValue( "Cart.GiftCardsCannotBeDiscounted", CultureInfo.GetCultureInfo( evt.Order.LanguageIsoCode ) ) );
        }
      }
    }
    }
    
  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Feb 24, 2022 @ 14:14
    Matt Brailsford
    1

    Fantastic! 🎉

    Yea, error handling is a must, but if it's all via validation exceptions hopefully you only need to implement that once.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies