Partager via


How to Accept Promotion Codes for a Discount

This runtime code example shows how accept a promotion code for a discount.

To accept a promotion code for a discount

  1. Create a new Basket object.

  2. Add a OrderForm to the Basket object that was created in step 1.

  3. Add a LineItems product to the OrderForm object.

  4. Set the ShippingMethodName.

  5. Add a PromoCodes object to the order.

  6. Run the Basket, Checkout, and Total pipelines.

  7. Save the Basket object.

Example

In this example, you create a new basket, add an item to the basket, set the shipping method, enter the promotion code, run the basket total and checkout pipelines, and then save the basket.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Pipelines;
using Microsoft.CommerceServer.Runtime.Orders;

public partial class marketingdev_AcceptPromoCode : System.Web.UI.Page
{
    protected void cmdAcceptPromoCode_Click(object sender, EventArgs e)
    {
        //Define the product catalog and product ID
        string productCatalog = "Adventure Works Catalog";
        string productId = "AW087-04";

        // Create a new basket.
        Basket basket = CommerceContext.Current.OrderSystem.GetBasket(Guid.NewGuid());

        //Add a new OrderForm to the basket.
        basket.OrderForms.Add(new OrderForm());

        //Add hiking boots to the order (productId AW087-04).
        basket.OrderForms[0].LineItems.Add(new LineItem(productCatalog, productId, null, 1));

        //Set the shipping method.
        basket.OrderForms[0].LineItems[0].ShippingMethodName = "Second-Day Air";

        //Add a coupon code to the order.
        basket.OrderForms[0].PromoCodes.Add("Promo1");
        basket.OrderForms[0].PromoUserIdentity = "test@microsoft.com";
        basket.OrderForms[0].LineItems[0].DisplayName = "Test Basket";

        // Run Basket, Total, and Checkout pipelines.
        basket.RunPipeline(new PipelineInfo("basket"));
        basket.RunPipeline(new PipelineInfo("total"));
        basket.RunPipeline(new PipelineInfo("checkout"));

        //Save the basket.
        basket.Save();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

See Also

Other Resources

Marketing Run-time Scenarios