다음을 통해 공유


How to Generate Coupons and Promotion Codes

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

This topic shows how to generate a batch of randomly generated coupons that are attached to a private promotion code.

To create coupons and promotion codes

  1. In Visual Studio, create a new Commerce Server 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 object is named marketingSystem.

  6. Create a new PromoCodeDefinition object.

  7. Generate a batch of Random Promotion Codes attached to the promotion code created in step 6.

Example

The following code example illustrates how to create a new private promotion code object. Ten new random promotion codes are created in a batch and attached to the newly created promotion code.

You can view the results in the Commerce Server Marketing Manager application.

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

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void GenRandomPromoCodes()
        {
            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);

                // Create a new promotion code for the batch.
                // Batch promo codes much be private.
                PromoCodeDefinition promo = marketingSystem.PromoCodeDefinitions.NewPromoCodeDefinition(PromoCodeUsageOption.Private, 1);
                promo.Name = "BatchPromoCodes";
                promo.UsageLimit = 5;
                promo.Save(true);

                // Create a batch of 10 random promotion code strings.
                Guid g;
                RandomGenerationParameters r;
                r = new RandomGenerationParameters();
                r.CodeLength = 5;
                r.Prefix = "pre";
                r.Suffix = "suf";
                g = Guid.Empty;
                g = marketingSystem.PromoCodeDefinitions.GenerateRandomCodes(promo.Id, "BatchPromoCodes", 10, r);
            }
            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