Last active
August 6, 2021 15:45
-
-
Save callumbwhyte/c7d9f39702d403376b025ab138177693 to your computer and use it in GitHub Desktop.
Example of importing discounts to Vendr programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Umbraco.Core; | |
using Vendr.Core; | |
using Vendr.Core.Api; | |
using Vendr.Core.Models; | |
public class DiscountImporter | |
{ | |
private readonly IVendrApi _vendr; | |
public DiscountImporter(IVendrApi vendr) | |
{ | |
_vendr = vendr; | |
} | |
public void ImportDiscount(Guid storeId, Guid id, string name, string code, double percentage) | |
{ | |
using (var uow = _vendr.Uow.Create()) | |
{ | |
var discount = Discount.Create(uow, id, storeId, name.ToSafeAlias(), name); | |
discount.SetType(DiscountType.Code); | |
discount.AddCode(code, isUnlimited: true); | |
var rewardProperties = new Dictionary<string, string> | |
{ | |
{ "priceType", OrderPriceType.SubtotalPrice.ToString() }, | |
{ "adjustmentType", AdjustmentType.Percentage.ToString() }, | |
{ "percentage", percentage.ToString() } | |
}; | |
var rewards = new List<DiscountRewardConfig>() | |
{ | |
new DiscountRewardConfig("orderAmountDiscountReward", rewardProperties) | |
}; | |
var rule = new DiscountRuleConfig("groupDiscountRule", new Dictionary<string, string>() | |
{ | |
{ "matchType", MatchType.All.ToString() } | |
}); | |
discount.SetRewards(rewards); | |
discount.SetRules(rule, true, true); | |
discount.SetActive(true); | |
_vendr.SaveDiscount(discount); | |
uow.Complete(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment