Buy a bundle get a discount

When giving a talk at the Serbia meetup for Episerver last Thursday, about setting up / creating your catalog (models), I got the question if it was possible to give a discount on a bundle. This is not possible out of the box but can be easily done with a custom promotion.

Nothing special going on in the code, just using the content references in the bundle instead of the references below a category. Note: set MatchRecursive to false when getting the DiscountItems or PromotionItems

protected static DiscountItems GetTargetItems(BuyBundleGetItemDiscount promotionData)
{
    IEnumerable<ContentReference> entries = ListBundleEntries(referenceToBundle:  promotionData.Bundle).Select(e => e.Child);

    return new DiscountItems { Items = entries.ToList(), MatchRecursive = false };
}

protected static IEnumerable<BundleEntry> ListBundleEntries(ContentReference referenceToBundle)
{
    IRelationRepository relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();

    // Relations to bundle entries are of type BundleEntry
    List<BundleEntry> bundleEntries = relationRepository.GetChildren<BundleEntry>(parentLink: referenceToBundle).ToList();

    return bundleEntries;
}

The complete promotion can be found in a Gist

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s