I want to write a procedure (Razor/C#) that I can feed to related products into (ie their ids) and identify if there is any bundled offer in the Marketing foundation for those two items.
The easiest way to do it is to populate the targeting context with the products you're looking for and have Marketing Foundation evaluate them for you. This requires that your bundles are configured with the two or more product in both the Advertise section and the Act section as shown in the screenshot:
Here's the code you need to make it work. Marketing Foundation will basically evaluate the current targeting context and return any campaign items, which match the context. For your specific case what you need is a way to set specific products instead of using the default approach, which is to look at what the customer is currently browsing.
So first we need a way to specific which products we're interested in. To do that we need a new populator, which is used to populate the context. Basically this guy accepts a number of products (the ones making up your bundle) and the Populate method overrides the current targeting context for products with just the ones you're looking for.
public class SpecificProductPopulator : ITargetingContextPopulator
{
private IList<Product> Products { get; set; }
public SpecificProductPopulator(IList<Product> products)
{
Products = products;
}
public TargetingContext Populate(TargetingContext targetingContext)
{
targetingContext.Products = Products;
return targetingContext;
}
}
Now we need the MarketingService to actually do the targeting for us. It requires a bunch of dependancies, which we are going to look up using the config and finally we're going to override one of them to ensure that our custom populator is used instead of the default ones.
// Find the products of the bundle. var bundledProducts = Product.All().Where(x => (x.Sku == "100-000-001" || x.Sku == "200-000-001") && x.ParentProductId == null).ToList();
// Set up our custom populator with the products of the bundle.
var specificProductPopulator = new SpecificProductPopulator(bundledProducts);
// Set up the context aggregator (this guy visits multiple populators, but in this case there's just our custom populator). ITargetingContextAggregator targetingContextAggregator = new TargetingContextAggregator(new List<ITargetingContextPopulator> { specificProductPopulator}.ToArray());
// Now set up the marketing service overriding the default context aggregator. var targetingService = new MarketingService(
ObjectFactory.Instance.Resolve<IRepository<CampaignItem>>(),
targetingContextAggregator,
ObjectFactory.Instance.Resolve<ITargetAggregator>(),
ObjectFactory.Instance.Resolve<IAwardAggregator>());
// And finally get all the campaign items, which involves one or both of our two products. var targetedCampaignItems = targetingService.GetTargetedCampaignItems();
The campaign items returned will contain both bundles and any other campaign items, which involved one of the products specified. From here you can loop through and just grab the ones you want.
OK, I've tried this on the example store in the example store, using the bundle example already in there (and the hardcoded sku's included above). I'm testing with the following
foreach (var offer in targetedCampaignItems){
@offer.Name
}
All this is outputting is the Software unit discount. A count of targetedCampaignItems just returns one
Is the bundle discount configured like the screenshot I posted? It's important that both the Advertise and Act sections contain the products of the bundle.
Yes. You want to filter on the number of DisplayTargets for the CampaignItems you get back. Basically do Target.All().Where(x => x.CampaignItem == myCampaignItem)
Razor: Related products bundled together
I want to write a procedure (Razor/C#) that I can feed to related products into (ie their ids) and identify if there is any bundled offer in the Marketing foundation for those two items.
I have no idea where to start. Anyone help?
"two related"
The easiest way to do it is to populate the targeting context with the products you're looking for and have Marketing Foundation evaluate them for you. This requires that your bundles are configured with the two or more product in both the Advertise section and the Act section as shown in the screenshot:
Here's the code you need to make it work. Marketing Foundation will basically evaluate the current targeting context and return any campaign items, which match the context. For your specific case what you need is a way to set specific products instead of using the default approach, which is to look at what the customer is currently browsing.
So first we need a way to specific which products we're interested in. To do that we need a new populator, which is used to populate the context. Basically this guy accepts a number of products (the ones making up your bundle) and the Populate method overrides the current targeting context for products with just the ones you're looking for.
Now we need the MarketingService to actually do the targeting for us. It requires a bunch of dependancies, which we are going to look up using the config and finally we're going to override one of them to ensure that our custom populator is used instead of the default ones.
The campaign items returned will contain both bundles and any other campaign items, which involved one of the products specified. From here you can loop through and just grab the ones you want.
Hope this helps.
OK, I've tried this on the example store in the example store, using the bundle example already in there (and the hardcoded sku's included above). I'm testing with the following
All this is outputting is the Software unit discount. A count of targetedCampaignItems just returns one
Is the bundle discount configured like the screenshot I posted? It's important that both the Advertise and Act sections contain the products of the bundle.
OK. So, now if I access the product page it is listing the discount fo the bundle and for the single item. I only want those that apply to both sku
Yes. You want to filter on the number of DisplayTargets for the CampaignItems you get back. Basically do Target.All().Where(x => x.CampaignItem == myCampaignItem)
is working on a reply...