Gift cards
List of examples:
- Creating a gift card
- Adding coupon codes to a gift card
- Generating coupon codes for a gift card
- Updating gift cards
- Setting the remaining value for gift card coupon codes
- Deleting a gift card
Creating a gift card
// Creates a new gift card object and sets its properties
var newGiftCard = new GiftCardInfo()
{
GiftCardDisplayName = "New gift card",
GiftCardName = "NewGiftCard",
GiftCardEnabled = true,
GiftCardSiteID = SiteContext.CurrentSiteID,
GiftCardValue = 10,
GiftCardMinimumOrderPrice = 50,
GiftCardCartCondition = @"{% Currency.CurrencyName==""Dollar"" %}",
};
// Saves the gift card to the database
GiftCardInfo.Provider.Set(newGiftCard);
Adding coupon codes to a gift card
// Gets the first gift card on the current site whose name starts with 'NewGift'
GiftCardInfo giftCard = GiftCardInfo.Provider.Get().OnSite(SiteContext.CurrentSiteID)
.WhereStartsWith("GiftCardName", "NewGift")
.TopN(1)
.FirstOrDefault();
if (giftCard != null)
{
// Creates a coupon code and adds it to the gift card (the code is "GIFT-10")
GiftCardCouponCodeInfoProvider.CreateGiftCardCoupon(giftCard, "GIFT-10");
}
Generating coupon codes for a gift card
// Gets the first gift card on the current site whose name starts with 'NewGift'
GiftCardInfo giftCard = GiftCardInfo.Provider.Get().OnSite(SiteContext.CurrentSiteID)
.WhereStartsWith("GiftCardName", "NewGift")
.TopN(1)
.FirstOrDefault();
if (giftCard != null)
{
// Prepares a query that gets all existing coupon codes from the current site
var existingCouponCodeQuery = ECommerceHelper.GetAllCouponCodesQuery(SiteContext.CurrentSiteID);
// Creates a cache of coupon codes on the current site
var existingCodes = existingCouponCodeQuery.GetListResult<string>();
// Prepares an instance of a class that checks against existing coupon codes to avoid duplicates
var coudeUniquenessChecker = new CodeUniquenessChecker(existingCodes);
// Initializes a coupon code generator
RandomCodeGenerator codeGenerator = new RandomCodeGenerator(coudeUniquenessChecker, "**********");
// Loops to generate 100 coupon codes
for (int i = 0; i < 100; i++)
{
// Generates a new unique code text
string code = codeGenerator.GenerateCode();
// Creates a coupon code and adds it to the gift card
GiftCardCouponCodeInfoProvider.CreateGiftCardCoupon(giftCard, code);
}
}
Updating gift cards
// Gets all enabled gift cards on the current site
var giftCards = GiftCardInfo.Provider.Get().OnSite(SiteContext.CurrentSiteID)
.WhereTrue("GiftCardEnabled");
// Loops through the gift cards
foreach (GiftCardInfo giftCard in giftCards)
{
// Updates the gift card properties (sets the minimum order price to 100)
giftCard.GiftCardMinimumOrderPrice = 100;
// Saves the changes to the database
GiftCardInfo.Provider.Set(giftCard);
}
Setting the remaining value for gift card coupon codes
// Gets the 'Gift-10' gift card coupon code (coupon codes are unique within the context of each site)
GiftCardCouponCodeInfo giftCardCouponCode = GiftCardCouponCodeInfo.Provider.Get("Gift-10", SiteContext.CurrentSiteID);
// Sets the remaining value to '5' for the coupon code
giftCardCouponCode.GiftCardCouponCodeRemainingValue = 5m;
// Saves the updated coupon code to the database
giftCardCouponCode.Update();
Deleting a gift card
// Gets the first gift card on the current site whose name starts with 'NewGift'
GiftCardInfo giftCard = GiftCardInfo.Provider.Get().OnSite(SiteContext.CurrentSiteID)
.WhereStartsWith("GiftCardName", "NewGift")
.TopN(1)
.FirstOrDefault();
if (giftCard != null)
{
// Deletes the gift card
GiftCardInfo.Provider.Delete(giftCard);
}