Discounts
List of examples:
- Buy X Get Y discounts
- Catalog discounts
- Order discounts
- Free shipping offers
- Volume discounts
- Product coupons
Buy X Get Y discounts
Creating a Buy X Get Y discount
// Gets a product for a 2+1 free Buy X Get Y discount
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.FirstObject;
if (product != null)
{
// Creates a new Buy X Get Y discount object and sets its properties
MultiBuyDiscountInfo newDiscount = new MultiBuyDiscountInfo()
{
MultiBuyDiscountName = "MyDiscount",
MultiBuyDiscountDisplayName = "My Discount",
MultiBuyDiscountApplyFurtherDiscounts = true,
MultiBuyDiscountMinimumBuyCount = 2,
MultiBuyDiscountApplyToSKUID = product.SKUID,
MultiBuyDiscountIsFlat = false,
MultiBuyDiscountValue = 100,
MultiBuyDiscountAutoAddEnabled = true,
MultiBuyDiscountEnabled = true,
MultiBuyDiscountCustomerRestriction = DiscountCustomerEnum.All,
MultiBuyDiscountSiteID = SiteContext.CurrentSiteID
};
// Saves the Buy X Get Y discount to the database
MultiBuyDiscountInfoProvider.SetMultiBuyDiscountInfo(newDiscount);
// Creates a new relationship of a product that needs to be bought and the discount
MultiBuyDiscountSKUInfo newSkuDiscount = new MultiBuyDiscountSKUInfo();
// Sets the product-discount relationship properties
newSkuDiscount.MultiBuyDiscountID = newDiscount.MultiBuyDiscountID;
newSkuDiscount.SKUID = product.SKUID;
// Saves the product-discount relationship to the database
MultiBuyDiscountSKUInfoProvider.SetMultiBuyDiscountSKUInfo(newSkuDiscount);
}
Updating a Buy X Get Y discount
// Gets the first Buy X Get Y discount that contains 'MyDiscount' on the current site
MultiBuyDiscountInfo discount = MultiBuyDiscountInfoProvider.GetMultiBuyDiscounts(SiteContext.CurrentSiteID)
.WhereContains("MultiBuyDiscountName", "MyDiscount")
.FirstObject;
if (discount != null)
{
// Updates the Buy X Get Y discount properties
discount.MultiBuyDiscountMinimumBuyCount = 3;
// Saves the changes to the database
MultiBuyDiscountInfoProvider.SetMultiBuyDiscountInfo(discount);
}
Updating multiple Buy X Get Y discounts
// Gets all enabled Buy X Get Y discounts on the current site
var discounts = MultiBuyDiscountInfoProvider.GetMultiBuyDiscounts(SiteContext.CurrentSiteID)
.WhereTrue("MultiBuyDiscountEnabled");
// Loops through the Buy X Get Y discounts
foreach (MultiBuyDiscountInfo discount in discounts)
{
// Updates the Buy X Get Y discounts properties
discount.MultiBuyDiscountEnabled = false;
// Saves the changes to the database
MultiBuyDiscountInfoProvider.SetMultiBuyDiscountInfo(discount);
}
Adding coupons to a Buy X Get Y discount
// Gets the first Buy X Get Y discount that contains 'MyDiscount' on the current site
MultiBuyDiscountInfo discount = MultiBuyDiscountInfoProvider.GetMultiBuyDiscounts(SiteContext.CurrentSiteID)
.WhereContains("MultiBuyDiscountName", "MyDiscount")
.FirstObject;
if (discount != null)
{
// Enables using coupons for the Buy X Get Y discount
discount.MultiBuyDiscountUsesCoupons = true;
// Saves the change to the database
MultiBuyDiscountInfoProvider.SetMultiBuyDiscountInfo(discount);
// Prepares a query that gets all existing coupon codes from the current site
var existingQuery = ECommerceHelper.GetAllCouponCodesQuery(SiteContext.CurrentSiteID);
// Creates a cache of coupon codes on the current site
HashSet<string> existingCodes = new HashSet<string>();
using (DbDataReader reader = existingQuery.ExecuteReader())
{
while (reader.Read())
{
existingCodes.Add(reader.GetString(0));
}
}
// Initializes a coupon code generator
RandomCodeGenerator codeGenerator = new RandomCodeGenerator("**********");
// Sets that generated codes are checked against the existing coupon codes to avoid duplicates
codeGenerator.CodeChecker = code => !existingCodes.Contains(code);
// Loops to generate 300 coupon codes
for (int i = 0; i < 300; i++)
{
// Generates a new unique code text
string code = codeGenerator.GenerateCode();
// Creates a coupon code and adds it to the discount
MultiBuyCouponCodeInfoProvider.CreateCoupon(discount, code, 1);
}
}
Deleting a Buy X Get Y discount
// Gets the first Buy X Get Y discount that contains 'MyDiscount' on the current site
MultiBuyDiscountInfo discount = MultiBuyDiscountInfoProvider.GetMultiBuyDiscounts(SiteContext.CurrentSiteID)
.WhereContains("MultiBuyDiscountName", "MyDiscount")
.FirstObject;
if (discount != null)
{
// Deletes the Buy X Get Y discount
MultiBuyDiscountInfoProvider.DeleteMultiBuyDiscountInfo(discount);
}
Catalog discounts
Creating a catalog discount
// Creates a new catalog discount object and sets its properties
DiscountInfo newDiscount = new DiscountInfo()
{
DiscountName = "MyDiscount",
DiscountDisplayName = "My Discount",
ItemDiscountType = DiscountTypeEnum.CatalogDiscount,
DiscountIsFlat = false,
DiscountValue = 20,
DiscountApplyTo = DiscountApplicationEnum.Products,
DiscountProductCondition = @"{% SKU.SKUName.Contains(""new"") @%}",
DiscountEnabled = true,
DiscountOrder = 1,
DiscountApplyFurtherDiscounts = true,
DiscountUsesCoupons = false,
DiscountSiteID = SiteContext.CurrentSiteID
};
// Saves the catalog discount to the database
DiscountInfoProvider.SetDiscountInfo(newDiscount);
Updating a catalog discount
// Gets the first catalog discount that contains 'MyDiscount' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyDiscount")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Products.ToString())
.FirstObject;
if (discount != null)
{
// Updates the catalog discount properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Updating multiple catalog discounts
// Gets all enabled catalog discounts on the current site
var discounts = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID, true)
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Products.ToString());
// Loops through the catalog discounts
foreach (DiscountInfo discount in discounts)
{
// Updates the catalog discount properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Deleting a catalog discount
// Gets the first catalog discount that contains 'MyDiscount' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyDiscount")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Products.ToString())
.FirstObject;
if (discount != null)
{
// Deletes the catalog discount
DiscountInfoProvider.DeleteDiscountInfo(discount);
}
Order discounts
Creating a catalog discount
// Creates a new order discount object and sets its properties
DiscountInfo newDiscount = new DiscountInfo()
{
DiscountName = "MyDiscount",
DiscountDisplayName = "My Discount",
ItemDiscountType = DiscountTypeEnum.OrderDiscount,
DiscountIsFlat = false,
DiscountValue = 20,
DiscountApplyTo = DiscountApplicationEnum.Order,
DiscountCartCondition = @"{% Currency.CurrencyName==""Dollar"" @%}",
DiscountEnabled = true,
DiscountOrder = 1,
DiscountApplyFurtherDiscounts = true,
DiscountUsesCoupons = false,
DiscountSiteID = SiteContext.CurrentSiteID,
};
// Saves the order discount to the database
DiscountInfoProvider.SetDiscountInfo(newDiscount);
Updating a order discount
// Gets the first order discount that contains 'MyDiscount' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyDiscount")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Order.ToString())
.FirstObject;
if (discount != null)
{
// Updates the order discount properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Updating multiple order discounts
// Gets all enabled order discounts on the current site
var discounts = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID, true)
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Order.ToString());
// Loops through the order discounts
foreach (DiscountInfo discount in discounts)
{
// Updates the order discount properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Deleting a order discount
// Gets the first order discount that contains 'MyDiscount' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyDiscount")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Order.ToString())
.FirstObject;
if (discount != null)
{
// Deletes the order discount
DiscountInfoProvider.DeleteDiscountInfo(discount);
}
Free shipping offers
Creating a free shipping offer
// Creates a new free shipping offer object and sets its properties
DiscountInfo newDiscount = new DiscountInfo()
{
DiscountName = "MyShippingOffer",
DiscountDisplayName = "My Shipping Offer",
ItemDiscountType = DiscountTypeEnum.ShippingDiscount,
DiscountIsFlat = false,
DiscountValue = 20,
DiscountApplyTo = DiscountApplicationEnum.Shipping,
DiscountOrderAmount = 100,
DiscountEnabled = true,
DiscountOrder = 1,
DiscountApplyFurtherDiscounts = true,
DiscountUsesCoupons = false,
DiscountSiteID = SiteContext.CurrentSiteID
};
// Saves the free shipping offer to the database
DiscountInfoProvider.SetDiscountInfo(newDiscount);
Updating a free shipping offer
// Gets the first free shipping offer that contains 'MyShippingOffer' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyShippingOffer")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Shipping.ToString())
.FirstObject;
if (discount != null)
{
// Updates the free shipping offer properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Updating multiple free shipping offer
// Gets all enabled free shipping offers on the current site
var discounts = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID, true)
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Shipping.ToString());
// Loops through the free shipping offers
foreach (DiscountInfo discount in discounts)
{
// Updates the free shipping offer properties
discount.DiscountEnabled = false;
// Saves the changes to the database
DiscountInfoProvider.SetDiscountInfo(discount);
}
Deleting a free shipping offer
// Gets the first free shipping offer that contains 'MyShippingOffer' on the current site
DiscountInfo discount = DiscountInfoProvider.GetDiscounts(SiteContext.CurrentSiteID)
.WhereContains("DiscountName", "MyShippingOffer")
.WhereContains("DiscountApplyTo", DiscountApplicationEnum.Shipping.ToString())
.FirstObject;
if (discount != null)
{
// Deletes the free shipping offer
DiscountInfoProvider.DeleteDiscountInfo(discount);
}
Volume discounts
Creating a volume discount
// Gets a product for the volume discount
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.FirstObject;
if (product != null)
{
// Creates a new volume discount object
VolumeDiscountInfo newDiscount = new VolumeDiscountInfo();
// Sets the volume discount properties
newDiscount.VolumeDiscountMinCount = 100;
newDiscount.VolumeDiscountValue = 20;
newDiscount.VolumeDiscountSKUID = product.SKUID;
newDiscount.VolumeDiscountIsFlatValue = false;
// Saves the volume discount to the database
VolumeDiscountInfoProvider.SetVolumeDiscountInfo(newDiscount);
}
Updating a volume discount
// Gets a product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereEquals("SKUName", "NewProduct")
.FirstObject;
if (product != null)
{
// Gets the first volume discount defined for the product
VolumeDiscountInfo discount = VolumeDiscountInfoProvider.GetVolumeDiscounts(product.SKUID).FirstObject;
if (discount != null)
{
// Updates the volume discount properties
discount.VolumeDiscountMinCount = 800;
// Saves the changes to the database
VolumeDiscountInfoProvider.SetVolumeDiscountInfo(discount);
}
}
Updating multiple volume discounts
// Gets a product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.FirstObject;
if (product != null)
{
// Gets the product's volume discounts
var discounts = VolumeDiscountInfoProvider.GetVolumeDiscounts(product.SKUID);
// Loops through the volume discounts
foreach (VolumeDiscountInfo discount in discounts)
{
// Updates the volume discount properties
discount.VolumeDiscountMinCount = 500;
// Saves the changes to the database
VolumeDiscountInfoProvider.SetVolumeDiscountInfo(discount);
}
}
Deleting a volume discount
// Gets a product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.FirstObject;
if (product != null)
{
// Gets the first volume discount defined for the product
VolumeDiscountInfo discount = VolumeDiscountInfoProvider.GetVolumeDiscounts(product.SKUID).FirstObject;
if (discount != null)
{
// Deletes the volume discount
VolumeDiscountInfoProvider.DeleteVolumeDiscountInfo(discount);
}
}
Product coupons
Creating a product coupon
// Creates a new product coupon object
DiscountCouponInfo newCoupon = new DiscountCouponInfo();
// Sets the product coupon properties
newCoupon.DiscountCouponDisplayName = "New coupon";
newCoupon.DiscountCouponCode = "NewCoupon";
newCoupon.DiscountCouponIsExcluded = true;
newCoupon.DiscountCouponIsFlatValue = true;
newCoupon.DiscountCouponValue = 200;
newCoupon.DiscountCouponValidFrom = DateTime.Now;
newCoupon.DiscountCouponSiteID = SiteContext.CurrentSiteID;
// Saves the product coupon to the database
DiscountCouponInfoProvider.SetDiscountCouponInfo(newCoupon);
Updating a product coupon
// Gets the product coupon
DiscountCouponInfo updateCoupon = DiscountCouponInfoProvider.GetDiscountCouponInfo("NewCoupon", SiteContext.CurrentSiteName);
if (updateCoupon != null)
{
// Updates the product coupon properties
updateCoupon.DiscountCouponDisplayName = updateCoupon.DiscountCouponDisplayName.ToLowerCSafe();
// Saves the changes to the database
DiscountCouponInfoProvider.SetDiscountCouponInfo(updateCoupon);
}
Updating multiple product coupons
// Gets all product coupons whose code starts with 'New'
var coupons = DiscountCouponInfoProvider.GetDiscountCoupons()
.WhereStartsWith("DiscountCouponCode", "New");
// Loops through the product coupons
foreach (DiscountCouponInfo modifyCoupon in coupons)
{
// Updates the product coupon properties
modifyCoupon.DiscountCouponDisplayName = modifyCoupon.DiscountCouponDisplayName.ToUpperCSafe();
// Saves the changes to the database
DiscountCouponInfoProvider.SetDiscountCouponInfo(modifyCoupon);
}
Adding products to a product coupon
// Gets the discounted product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.WhereNull("SKUOptionCategoryID")
.FirstObject;
// Gets the product coupon
DiscountCouponInfo discountCoupon = DiscountCouponInfoProvider.GetDiscountCouponInfo("NewCoupon", SiteContext.CurrentSiteName);
if ((discountCoupon != null) && (product != null))
{
// Adds the product to the coupon
SKUDiscountCouponInfoProvider.AddDiscountCouponToSKU(product.SKUID, discountCoupon.DiscountCouponID);
}
Removing products from a product coupon
// Gets the discounted product
var product = SKUInfoProvider.GetSKUs()
.WhereStartsWith("SKUName", "New")
.WhereNull("SKUOptionCategoryID")
.FirstObject;
// Gets the product coupon
DiscountCouponInfo discountCoupon = DiscountCouponInfoProvider.GetDiscountCouponInfo("NewCoupon", SiteContext.CurrentSiteName);
if ((discountCoupon != null) && (product != null))
{
// Gets the object representing the product-coupon relationship
SKUDiscountCouponInfo skuDicountCoupon = SKUDiscountCouponInfoProvider.GetSKUDiscountCouponInfo(product.SKUID, discountCoupon.DiscountCouponID);
if (skuDicountCoupon != null)
{
// Removes the product from the product coupon
SKUDiscountCouponInfoProvider.DeleteSKUDiscountCouponInfo(skuDicountCoupon);
}
}
Deleting a product coupon
// Gets the product coupon
DiscountCouponInfo deleteCoupon = DiscountCouponInfoProvider.GetDiscountCouponInfo("NewCoupon", SiteContext.CurrentSiteName);
if (deleteCoupon != null)
{
// Deletes the product coupon from the database
DiscountCouponInfoProvider.DeleteDiscountCouponInfo(deleteCoupon);
}