Exclude certain line items from all line item promotions

Vote:
 

Hi,

I am using commerce 12.15.0, I have a requirment to exclude certain items from all line item promotions. My approach is to override the GetLineItems method in the EntryPromotionProcessorBase abstract class. This way I can add my exclusion logic in one place, and since all entry promotions use this processor they should automatically filter the line items. But now I have a problem, EntryPromotionProcessorBase  being an abstract class, I need to implement Evaluate and GetPromotionItems method. But if I do this in the processor it would apply to all line items promotions right? And then all the different types of promotions would not be of any use. Below is my code, I need help to create a generic processor which can be used by all entry promotions.

    [ServiceConfiguration(Lifecycle = ServiceInstanceScope.Singleton)]
    public class TestProcessor : EntryPromotionProcessorBase<EntryPromotion>
    {
        public override IEnumerable<ILineItem> GetLineItems(IOrderForm orderForm)
        {
            //Custom logic to exclude certain line items from promotions.
            CustomerPriceService priceService = ServiceLocator.Current.GetInstance<IPriceService>() as CustomerPriceService;
            string currentCustomerGroup = ServiceLocator.Current.GetInstance<ICurrentCustomer>().GetCurrentCustomer().CustomerGroup;
            var currentMarket = ServiceLocator.Current.GetInstance<ICurrentMarket>().GetCurrentMarket();
            IEnumerable<ILineItem> defaultList = base.GetLineItems(orderForm);
            List<ILineItem> lineItems = new List<ILineItem>();
            foreach(ILineItem item in defaultList)
            {
                bool hasCustomerGroupPrice = false;
                PriceFilter filter = new PriceFilter()
                {
                    Currencies = new Currency[] { new Currency("USD") },
                    CustomerPricing = new CustomerPricing[]
                    {
                        CustomerPricing.AllCustomers,
                        new CustomerPricing(CustomerPricing.PriceType.PriceGroup, currentCustomerGroup)
                    },
                    Quantity = 1
                };

                hasCustomerGroupPrice = priceService.HasCustomerGroupPrice(currentMarket.MarketId, DateTime.UtcNow, new CatalogKey(item.Code), filter);
                if(hasCustomerGroupPrice)
                {
                    lineItems.Add(item);
                }

            }

            return lineItems.AsEnumerable();
        }

        protected override RewardDescription Evaluate(EntryPromotion promotionData, PromotionProcessorContext context)
        {
            throw new NotImplementedException();
        }

        protected override PromotionItems GetPromotionItems(EntryPromotion promotionData)
        {
            throw new NotImplementedException();
        }
    }

#201677
Feb 26, 2019 17:03
Vote:
 
#201678
Feb 26, 2019 17:26
Vote:
 

@Quan, thank you for the reply. But I dont think we have the bandwidth to upgrade the solution at this point in time. And also this is something more dynamic, there could be N number of items which will need to be filtered based on a metafield value for example. And not a manual step where I go in and exclude items manually for a promotion.

#201679
Edited, Feb 26, 2019 17:59
Vote:
 

You can do that with the new exclusion, it is quite flexible when it comes to API

#201680
Feb 26, 2019 18:30