This article will cover how to create a campaign from code. You will learn how you can leverage the Ucommerce APIs to create discounts, criteria, promotions, and campaigns.


The code examples in this article are based on version 9.6.3 of Ucommerce.  


It is required that you have read and understand how the Ucommerce Marketing Foundation works. 

- Head over to the documentation to refresh your memory here.

Instruction


Step-by-step guide

  • Step 1: Creating the Campaign
  • Step 2: Creating the Promotion
  • Step 3: Creating the Criteria
  • Step 4:Creating the Discount


Step 1: Creating the Campaign

The first thing that you will need is to create a campaign, which is where you choose the start and end dates as well as associate the promotions you wish to have in this campaign. 


You can read more about the campaign concept here.


var summerCampaign = new Campaign
{
	Name = "Summer",
	Enabled = false,
	Priority = 0,
	StartsOn = DateTime.UtcNow,
	EndsOn = DateTime.UtcNow.AddMonths(1),
	ProductCatalogGroups = stores
};


On the campaign, you specified a Name that is going to be visible on any order that have gotten a discount from this campaign. 


The Enabled field as well as the StartsOn and EndsOn dates, define whether the Marketing Engine should evaluate this campaign in the basket pipeline or not. 


The ProductCatalogGroups are used to limit the campaign to be available on certain stores. 


Finally, we got the Priority, this field is used together with the "Allow following promotions to be combined with this one" on promotion (see below or read more here) and controls in which order the Marketing Foundation will be evaluating the active campaigns and promotions. 


Step 2: Creating the Promotion

Now that we have created our Campaign it is time to create the promotions we wish to make available through the campaign. 


You can read more about the promotion concept here.


var item = new CampaignItem
{
  Name = "Summer outlet",
  Priority = GetNextPriority(),
  Enabled = true,
  AnyTargetAppliesAwards = false,
  AllowNextCampaignItems = true,
  Definition = definition
};

summerCampaign.AddCampaignItem(item);
summerCampaign.Save();

The Name field is going to be added to the order, just like the Name field on the campaign, to which this Promotion has been added. The Priority and Enabled fields are also working in the same way as on the Campaign.


The AnyTargetAppliesAwards is used to determine if all the configured criteria should meet or if a any of them is enough to trigger the discount.


AllowNextCampaignItems, is used to determine if any other campaign should be able to trigger a discount, if this promotion is already triggering a discount. This is important to configure correctly and since it is working together with the prioritizations of Promotions and Campaigns.


Finally, you need to specify which Definition you want to use for creating the Promotion. By default Ucommerce provides a "Default Campaign Item" definition.


Step 3: Creating the Criterion/Criteria

Now it is time to configure the criterion/criteria, which is what the customer must do in order to receive the discount.

You can read all about how you build this here.


Step 4: Creating the Discount

Now, it is time to create the discount, which will be applied to the basket if the criterion/criteria has been met. 


In this example we will create a discount which will give the customer an amount of 100 off the order total.


var discount = new AmountOffOrderTotalAward
{ 
    CampaignItem = item, 
    AmountOff = 100M, 
    Guid = Guid.NewGuid()
};
discount.Save();


That’s it. Happy coding !