How to Create a Discount

This topic shows how to create a new discount with an attached promotion code.

To create a discount

  1. In Visual Studio, create a new Commerce Server Core Systems Web application.

  2. Add the Microsoft.CommerceServer.CrossTierTypes and Microsoft.CommerceServer.Marketing.CrossTierTypes references to the application.

  3. Add a using directive for the Microsoft.CommerceServer and Microsoft.CommerceServer.Marketing namespaces.

  4. Define the Marketing Web Service URL.

  5. Create the MarketingContext object.

    For more information, see How to Connect to the Marketing System. In this example, the MarketingContext is named marketingSystem.

  6. Look up the Default Campaign ID.

  7. Look up the Promotion code ID.

  8. Create the Discount.

  9. Save the Discount.

Example

The following code example illustrates how to create a discount. In addition to creating the discount, the ID of the promotion code created in the How to Create a Promotion Code example ("Promo1") is looked up and attached to the discount.

This example attaches to a Windows Forms button. When the user clicks the button, the Commerce Server Core Systems runs the code and creates the discount.

Note that the UseEligibilityRequirementForTargeting property has no representation in the Marketing Manager UI. It can be only by using the API.

Also note that if a discount does not have a basket display in the default marketing language, the discount is invalidated. If this happens an error is recorded in the event log with a description similar to "The discount with id 1 has an invalid value for the field 'description'. The discount has been invalidated."

You can view the results in the Marketing Manager application.

using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void NewDiscount()
        {
            try
            {
                // Set the URL to the Web service.
                string marketingWebServiceUrl = @"https://localhost/MarketingWebService/MarketingWebService.asmx";
                // Create an instance of the Marketing System agent.
                MarketingServiceAgent ma = new MarketingServiceAgent(marketingWebServiceUrl);
                MarketingContext marketingSystem = MarketingContext.Create(ma);

                // Look up the Default Campaign ID.
                SearchClauseFactory CsearchClauseFactory = marketingSystem.Campaigns.GetSearchClauseFactory();
                SearchClause Csc = CsearchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Default Campaign");
                DataSet CdataSet = marketingSystem.Campaigns.Search(Csc);
                int CampaignId = (int)CdataSet.Tables["SearchResults"].Rows[0][0];

                // Look up a promotion code definition ID.
                SearchClauseFactory PsearchClauseFactory = marketingSystem.PromoCodeDefinitions.GetSearchClauseFactory();
                SearchClause Psc = PsearchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Promo1");
                DataSet PdataSet = marketingSystem.PromoCodeDefinitions.Search(Psc);
                int PromocodeId = (int)PdataSet.Tables["SearchResults"].Rows[0][0];

                // Create a new test discount.
                Discount di = marketingSystem.CampaignItems.NewDiscount(CampaignId);
                di.Name = "Test Discount";
                di.StartDate = DateTime.Now;
                di.EndDate = DateTime.Now + TimeSpan.FromDays(14);
                di.Description = "Discount description ";
                di.DiscountType = DiscountType.OrderLevelCustom;
                di.MultilingualBasketDisplay.Add(new LanguageString("random string ", "fr"));
                di.WizardType = 5;
                di.UseEligibilityRequirementForTargeting = false;
                di.PerOrderLimit = 1;
                di.ClickRequired = true;
                di.MaxAwards = 1;
                di.OfferType = OfferType.Percentage;
                di.OfferAmount = 10;
                di.OfferDisjoint = true;
                di.CustomOrderLevelOfferType = "test";
                di.AutoAddAward = true;
                // Attach the promotion code to the discount.
                di.PromoCodeDefinitionId = PromocodeId;  
                di.ReuseConditionsAsAwards = true;
                di.ReuseAwardsAsConditions = true;
                di.ReuseAwardsAsAwards = true;
                di.ReuseAwardsAsConditions = true;
                di.Condition.Basis = ConditionBasis.Price;
                di.Condition.MinimumAmount = 100;
                di.SizeName = "Full Banner";
                di.TemplateName = "No Display";
                di.Save(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                Console.ReadLine();
            }
        }
    }
}

See Also

Other Resources

Marketing Management Scenarios