Skip to content

Instantly share code, notes, and snippets.

@callumbwhyte
Last active August 6, 2021 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callumbwhyte/c7d9f39702d403376b025ab138177693 to your computer and use it in GitHub Desktop.
Save callumbwhyte/c7d9f39702d403376b025ab138177693 to your computer and use it in GitHub Desktop.
Example of importing discounts to Vendr programatically
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