Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Tony Kiernan 278 posts 341 karma points
    Jun 27, 2011 @ 20:53
    Tony Kiernan
    0

    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?

  • Tony Kiernan 278 posts 341 karma points
    Jun 27, 2011 @ 21:07
    Tony Kiernan
    0

    "two related"

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jun 28, 2011 @ 14:37
    Søren Spelling Lund
    0

    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.

    Hope this helps.

  • Tony Kiernan 278 posts 341 karma points
    Jun 28, 2011 @ 17:59
    Tony Kiernan
    0

    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

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jun 29, 2011 @ 09:52
    Søren Spelling Lund
    0

    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.

  • Tony Kiernan 278 posts 341 karma points
    Jun 29, 2011 @ 13:39
    Tony Kiernan
    0

    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

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jun 30, 2011 @ 08:43
    Søren Spelling Lund
    0

    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)

Please Sign in or register to post replies

Write your reply to:

Draft