Share via


How to Display Discounts for Specific Products

You can retrieve all simple discounts that apply to a product or category from your site code. By doing this you can display discounts that apply to a product when you display other information about the product.

Note

A simple discount is a discount whose expression has a single clause, and which applies to a specific product or category. For example, "10 percent off all shoes" is a simple discount because it applies to a category and has a single clause. "10 percent off all brown shoes from manufacturer X" is not a simple discount because it has multiple clauses.

To retrieve and display a discount for a product

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

  2. Add the Microsoft.CommerceServer.Runtime resource to the Web application.

  3. Add a using directive for the Microsoft.CommerceServer.Runtime.Marketing namespace.

  4. Call the CreateFromCache method of the DiscountItemCollection class and provide the name of the discount cache. This method returns a DiscountItemCollection object that contains all discounts.

    Note

    The name of the discount cache is specified in the cache element of the Web.config file. For more information about the cache element, see caches Element. If you are displaying discounts for several products on one page, only create the DiscountItemCollection object one time.

  5. Create a DiscountCriteriaFilter object to describe the discounts to retrieve.

  6. Set the following properties of the DiscountCriteriaFilter object:

  7. Call the ApplyProductFilter method of the DiscountItemCollection object to retrieve all products that match the filter, or call the ApplyCategoryFilter method of the DiscountItemCollection object to retrieve all categories that match the filter. These methods return a DiscountItemCollection object that contains all the simple discounts that match the criteria that are specified by the DiscountCriteriaFilter object.

  8. Select the DiscountItem objects that you want to display to the user from the DiscountItemCollection object.

  9. Call the GetBasketDisplay method of the DiscountItem object to obtain a string that describes the discount.

  10. Use the string that you obtained in step 9 to compose the page that you display to the shopper.

Example

The following code example retrieves and displays the simple discounts for a product.

using System;
using System;
using System.Collections;
using System.Web;
using Microsoft.CommerceServer.Runtime.Marketing;

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get the collection of all discounts.
        DiscountItemCollection dic = DiscountItemCollection.CreateFromCache("Discounts");

        // Create a DiscountCriteriaFilter object and
        // set its properties to return only active
        // discounts that apply to a product and that
        // do not have other requirements.
        DiscountCriteriaFilter filter = new DiscountCriteriaFilter();
        filter.FilterOnAward = true;
        filter.FilterOnCondition = true;
        filter.IncludeDiscountsWithEligibilityRequirements = false;
        filter.IncludeDiscountsWithPromoCodes = false;
        filter.IncludeInactiveDiscounts = false;

        // Apply the filter and retrieve all matching
        // simple discounts for the product "AW074-04".
        DiscountItemCollection discounts = dic.ApplyProductFilter(filter, "Adventure Works Catalog", "AW074-04");

        foreach (DiscountItem item in discounts)
        {
            // Get the string that describes the discount.
            String discountString = discounts[0].GetBasketDisplay();
            Response.Write(discountString);
        }
    }
}

For this example to produce any output, your site must have the following items:

  • A catalog named "Adventure Works Catalog".

  • A product whose product ID is "AW074-04" in the Adventure Works catalog.

  • A simple discount that applies to the product whose product ID is "AW074-04". The discount must be active.

See Also

Reference

DiscountItemCollection

DiscountCriteriaFilter

Other Resources

Marketing Run-time Scenarios

What Discounts Are Loaded Into the Discount Cache?