# Wednesday, 21 March 2012

In many cases we come across companies who operate a separate discounting system from their e-commerce platform. In these cases it’s convenient to be able to import external campaigns into uCommerce via code.

This article outlines how you can create campaigns, campaign items, and associated advertising and discounts from code.

If you’re unfamiliar with uCommerce Marketing Foundation you can watch a quick video tour on uCommerce.tv.

Also before you continue you might want to take a look at Marketing Foundation Explained: Targeted Advertisements and Discounts, which explains the terminology of uCommerce Marketing Foundation and Extending Marketing Foundation- Adding A New Advertise and Act Target, which deals with added new targets and awards to Marketing Foundation.

A Bit Of Background

A discount in uCommerce requires a couple of moving parts to get going:

  1. A campaign to group marketing activities aka campaign items
  2. A campaign item to aggregate targets and awards
  3. One or more targets to determine when the campaign item is displayed to the customer, e.g. Customer is looking at the Clothing category
  4. One or more targets to determine when an award is triggered, e.g. Buy for more than 400€
  5. One or more awards, e.g. 10% off unit price

Let’s dive into each of the points individually.

Adding a Campaign

All of the classes below are available from the UCommerce.EntitiesV2 namespace found in the UCommerce.dll.

// Create a campaign to group campaign items
var campaign = new Campaign
                {
                    Name = "My Awesome Custom Campaing",
                    StartsOn = DateTime.Now,
                    EndsOn = DateTime.Now.AddDays(30),
                    Enabled = true
                };
 
campaign.Save();

Adding a Campaign Item

Campaign items require a definition, which determines which fields are available as part of it. We’re using the default campaign item definition in this case, but you can add your own with the fields and multilingual fields matching your requirements. You can learn more about definitions in Catalog Foundation Explained: Product Definitions.

Also worth noting is the “AllowNextCampaignItems” property, which is a way to tell uCommerce to stop evaluating campaign items if the current one is awarded to the customer. This is handy if you wish to limit the number of discounts awarded to the customer. Marketing Foundation Explained: Targeted Advertisements and Discounts describes this in more detail.

// Find a definition for the item to determine which fields are available
var definition = Definition.SingleOrDefault(x => x.Name == "Default Campaign Item");
 
// Create the campaign item to tie targets and awards to
var campaignItem = new CampaignItem
                {
                    Name = "Summer offers",
                    Enabled = true,
                    Definition = campaignItemDefinition,
                    AllowNextCampaignItems = true
                };
 
// Add campaign item to our campaign
campaign.AddCampaignItem(campaignItem);
campaign.Save();

Adding a Target for Advertise and Act

There a many different types of targets you can use. In this case it’s a product target, but you could go with “ProductCatalogGroupTarget” to target a specific store, “ProductCatalogTarget” to target a specific catalog, “CategoryTarget” to targat a specific category, “VoucherTarget” to require a specific voucher- or promo code, “OrderAmountTarget” to target a specific order total, etc. You get the idea :)

It’s important to note that if a target you need is not built-in you can extend uCommerce with your own targets as described in Extending Marketing Foundation- Adding A New Advertise and Act Target.

“EnabledForDisplay” and “EnabledForApply” determines whether the target is used for advertising, act, or both.

// add a product target
var productTarget = new ProductTarget
                {
                    CampaignItem = campaignItem,
                    Sku = "100-000-001",
                    VariantSku = "001", // this guy is optional
                    EnabledForApply = true, // use it for Act
                    EnabledForDisplay = true // use it for Advertise
                };
 
productTarget.Save();

Adding an Award

Here we’re using a discounted unit price award, but again there a many different types of awards you can add like AmountOffOrderTotalAward, AmountOffOrderLinesAward and so on. Remember if the award you’re looking for is not there you can extend uCommerce with your own types.

// Add a unit price discount award
var amountOffUnitPrice = new AmountOffUnitAward
                {
                    CampaingItem = campaignItem,
                    AmountOff = 10
                };
 
amountOffUnitPrice.Save();

Summary

One of the best features of uCommerce is that everything you can do from the UIs you can do from code. This enables developers to do some awesome automated things with uCommerce and it doesn’t take a whole lot of code to do either.

In the article we discussed how to create new campaigns, add marketing activities to them in the form of campaign items, and configure those campaign items with targets and awards to set up actual customer discounts.

Comments are closed.