Last active
January 21, 2017 13:06
-
-
Save kbortnik/48121d0fd2cf3fae5bfa96a75428145b to your computer and use it in GitHub Desktop.
Kentico modular system for e-products
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 System.Linq; | |
using CMS.Ecommerce; | |
using CMS.Helpers; | |
namespace CustomEProducts | |
{ | |
public static class CustomEProductManager | |
{ | |
private static readonly Dictionary<string, ICustomEProduct> _EProducts = new Dictionary<string, ICustomEProduct>(); | |
public static void RegisterEProduct(string skuNumber, ICustomEProduct customEProduct) | |
{ | |
// Ensure that the parameters are provided | |
if (skuNumber == null) | |
{ | |
throw new ArgumentNullException(nameof(skuNumber)); | |
} | |
if (customEProduct == null) | |
{ | |
throw new ArgumentNullException(nameof(customEProduct)); | |
} | |
_EProducts[skuNumber] = customEProduct; | |
} | |
private static void ProcessCustomEProducts(OrderInfo oi) | |
{ | |
// Get shopping cart items | |
var sci = ShoppingCartInfoProvider.GetShoppingCartInfoFromOrder(oi.OrderID); | |
if (sci == null) | |
return; | |
var customProcessingData = oi.OrderCustomData.GetValue("CustomProcessing") as string; | |
var existingValues = new ContainerCustomData(); | |
existingValues.LoadData(customProcessingData); | |
foreach (var i in sci.CartItems.Where(x => existingValues[x.SKU.SKUNumber] == null)) | |
{ | |
if (!_EProducts.ContainsKey(i.SKU.SKUNumber)) | |
continue; | |
_EProducts[i.SKU.SKUNumber].ProcessPaidProduct(i); | |
existingValues[i.SKU.SKUNumber] = true; | |
} | |
oi.OrderCustomData.SetValue("CustomProcessing", existingValues.GetData()); | |
oi.Update(); | |
} | |
public static void OrderPaid(object sender, OrderPaidEventArgs e) | |
{ | |
ProcessCustomEProducts(e.Order); | |
} | |
} | |
} |
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 CMS; | |
using CMS.DataEngine; | |
using CMS.Ecommerce; | |
using CustomEProducts; | |
[assembly: RegisterModule(typeof(CustomEProductsModule))] | |
namespace CustomEProducts | |
{ | |
public class CustomEProductsModule : Module | |
{ | |
public CustomEProductsModule() : base("CustomEProducts") | |
{ | |
} | |
protected override void OnInit() | |
{ | |
EcommerceEvents.OrderPaid.Execute += CustomEProductManager.OrderPaid; | |
} | |
} | |
} |
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 CMS.Ecommerce; | |
namespace CustomEProducts | |
{ | |
public interface ICustomEProduct | |
{ | |
void ProcessPaidProduct(ShoppingCartItemInfo cartItem); | |
} | |
} |
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 CMS.Ecommerce; | |
using CMS.EventLog; | |
using CustomEProducts; | |
using MyCustomEProducts; | |
[assembly: RegisterCustomEProduct("MySpecialProduct", typeof(MySpecialProduct))] | |
namespace MyCustomEProducts | |
{ | |
public class MySpecialProduct : ICustomEProduct | |
{ | |
public void ProcessPaidProduct(ShoppingCartItemInfo cartItem) | |
{ | |
EventLogProvider.LogInformation("CustomEProducts", "Purchased", string.Format("Someone has purchased {0} unit(s) of the product!", cartItem.CartItemUnits)); | |
} | |
} | |
} |
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 CMS; | |
namespace CustomEProducts | |
{ | |
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | |
public class RegisterCustomEProductAttribute : Attribute, IPreInitAttribute | |
{ | |
public Type MarkedType { get; private set; } | |
private readonly string _skuNumber; | |
public RegisterCustomEProductAttribute(string skuNumber, Type customEProduct) | |
{ | |
if (skuNumber == null) | |
throw new ArgumentNullException(nameof(skuNumber)); | |
if (customEProduct == null) | |
throw new ArgumentNullException(nameof(customEProduct)); | |
if (!typeof(ICustomEProduct).IsAssignableFrom(customEProduct)) | |
throw new ArgumentException("Provided type does not implement ICustomEProduct interface.", nameof(customEProduct)); | |
_skuNumber = skuNumber; | |
MarkedType = customEProduct; | |
} | |
public void PreInit() | |
{ | |
CustomEProductManager.RegisterEProduct(_skuNumber, (ICustomEProduct)Activator.CreateInstance(MarkedType)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment