November Happy Hour will be moved to Thursday December 5th.

Set custom properties on a lineitem when a discount is applied by the promotions engine

Vote:
 

Commerce version 12.5.0

Hi All,
Is there a way to set custom properties on a lineitem when a discount is applied by the promotions engine?

All the discounts in our site will be of type BuyQuantityGetSelectedItemsDiscount and they will all be percentaged based as well.  I can see the LineItemDiscountAmount is set, but I'd also like to set some custom properties like the campaign the discount is in and the percentage from the discount that was applied. Would also like to set the criteria that meant the lineitem was affected by the discount. For example when setting up the discount in Marketing, you can select category nodes as part of the criteria.  If the variation is within a category node that means the discount is applied to the lineitem, I'd like to store the category node against the lineitem.  Because of the node hierarchy, this won't necessarily be the direct node of the variation, it may be the parent node or grandparent node, etc... Is that possible?

Thanks
Mark

#197809
Oct 15, 2018 5:35
Vote:
 

Good question. I don't have an answer out of my head, but I'll look into it. This is, however, likely a piece that we are missing 

#197810
Oct 15, 2018 6:54
Vote:
 

Untested by me, but it seems the workaround is to inherit from EntryRewardApplicator, and then inside the protected methods you can add information to the AffectedEntries, which each contains a list of PriceEntry and each PriceEntry has an ILineItem. Not sure if it works, but it looks like it will 

#197827
Oct 15, 2018 11:19
Vote:
 

Thanks Quan.  I have something half working, needs more work and testing but as an initial implementation this is what I have.

    [ServiceConfiguration(Lifecycle = ServiceInstanceScope.Singleton, ServiceType = typeof(EntryRewardApplicator))]
    public class CustomEntryRewardApplicator : EntryRewardApplicator
    {
        private static readonly Injected<IContentLoader> _contentLoader;

        protected override Decimal ApplyPercentage(AffectedEntries item, Decimal percentage, PromotionProcessorContext processorContext)
        {
            var returnVal = base.ApplyPercentage(item, percentage, processorContext);

            // Update custom properties on lineitem
            PriceEntry priceEntry = item.PriceEntries.FirstOrDefault();
            if (priceEntry != null)
            {
                // Retrieve the campaign
                if (_contentLoader.Service.TryGet(priceEntry.Promotion.ParentLink, out SalesCampaign campaign))
                {
                    priceEntry.ParentItem.Properties["PromotionCode"] = campaign.Code;
                }
            }

            return returnVal;
        }
    }
#197869
Oct 16, 2018 4:24
Vote:
 

NIce. However the default implementation is already registered via ServiceConfiguration, so if you want to make sure your implementation gets used,you have to register it in IConfigurationModule instead.

#197871
Oct 16, 2018 7:15
Vote:
 

Just FYI: I am working on something that will make the override easier. But let's see if that gets enough approvals from my colleagues 

#197877
Oct 16, 2018 12:22
Vote:
 

OK thanks Quan.  This seems to be working correctly for me, so for the time being will stick with this, but if an easier override does make it into a release I'd be keen to upgrade.

#197901
Oct 17, 2018 3:39
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.