OVERVIEW
This article will help you as a developer in cases where you need to integrate with external applications to save and read additional information about an order. For example, these could include properties that systems such as an ERP or CRM might need in order to properly communicate information about a product in order to keep your catalog synchronized.
REQUIREMENTS
The following pre-requisites will be necessary to accomplish the goals of this article:
- Visual Studio 2012 or newer
- Understanding of C#
- Familiarity with building ASP.Net applications
GETTING STARTED
You should have a project ready to insert code to complete your application, such as a DNN CMS module, a custom workflow, or another project/solution.
READ AND WRITE CUSTOM PROPERTIES FOR AN PRODUCT
Before you can write or successfully add your code, you will first need to ensure that your project imports the following assemblies.
- Hotcakes.Commerce.dll
- Hotcakes.Modules.dll
- Hotcakes.Web.dll
We are finally at the point of writing some code to add, update and list your own custom properties to Hotcakes Commerce products. If you were to use the example you are about to see the same way it was generated, it would allow you to submit, save, and list custom properties for an order, like shown in the image below.
The examples below are thoroughly commented and assume you are writing a web forms interface. However, this can still apply to pretty much any server-side code examples. The code behind is the most important part of the sample.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomProperties.ascx.cs" Inherits="CodeSamples.CustomProductProperties" %>
<%@ Register tagPrefix="dnn" tagName="Label" src="~/controls/labelcontrol.ascx" %>
<div class="dnnForm">
<fieldset>
<div class="dnnFormItem">
<dnn:label runat="server" text="SKU:"></dnn:label>
<asp:textbox runat="server" id="txtSku"></asp:textbox>
</div>
<div class="dnnFormItem">
<dnn:label runat="server" text="Property Name:"></dnn:label>
<asp:textbox runat="server" id="txtPropertyName"></asp:textbox>
</div>
<div class="dnnFormItem">
<dnn:label runat="server" text="Property Value:"></dnn:label>
<asp:textbox runat="server" id="txtPropertyValue"></asp:textbox>
</div>
<div class="dnnFormItem">
<asp:linkbutton runat="server" cssclass="dnnPrimaryAction" onclick="AddCustomProperty" text="Add Custom Property to Product"></asp:linkbutton>
</div>
</fieldset>
</div>
<div class="dnnClear">
<asp:gridview runat="server" id="grdProperties" cssclass="dnnGrid">
<headerstyle cssclass="dnnGridHeader"></headerstyle>
<rowstyle cssclass="dnnGridItem"></rowstyle>
<alternatingrowstyle cssclass="dnnGridAltItem"></alternatingrowstyle>
</asp:gridview>
</div>
using System;
using System.Linq;
using DotNetNuke.Entities.Modules;
using Hotcakes.Commerce;
using Hotcakes.Commerce.Extensions;
namespace CodeSamples
{
public partial class CustomProductProperties : PortalModuleBase
{
#region Private Properties
// any unique key to identify your orginization
private const string DEVELOPERID = "YOURUNIQUEID";
private HotcakesApplication _HccApp = null;
///
/// Required to access most of the Hotcakes API
///
private HotcakesApplication HccApp
{
get
{
if (_HccApp == null) _HccApp = HccAppHelper.InitHccApp();
return _HccApp;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
// using a sample product from the sample product data (Brown Fedora)
txtSku.Text = "SAMPLE004";
// show all of the custom properties for the Product
BindData();
}
protected void AddCustomProperty(object sender, EventArgs e)
{
// get an instance of the Product using the SKU
var p = HccApp.CatalogServices.Products.FindBySku(txtSku.Text.Trim());
// only proceed if there is a Product found
if (p != null)
{
// check to see if the custom property already exists
if (p.CustomPropertyExists(DEVELOPERID, txtPropertyName.Text.Trim()))
{
// update the existing custom property
p.CustomProperties.SetProperty(DEVELOPERID, txtPropertyName.Text.Trim(),
txtPropertyValue.Text.Trim());
}
else
{
// add a new custom property to the Product
p.CustomProperties.Add(DEVELOPERID, txtPropertyName.Text.Trim(), txtPropertyValue.Text.Trim());
}
// update the Product to save the new or updated custom property
HccApp.CatalogServices.Products.Update(p);
}
// show all of the custom properties for the Product
BindData();
}
private void BindData()
{
// only proceed if there is a Product SKU to work with
if (!string.IsNullOrEmpty(txtSku.Text))
{
// get an instance of the Product using the SKU
var p = HccApp.CatalogServices.Products.FindBySku(txtSku.Text.Trim());
// generate a collection of custom properties matching your unique developer id
var props =
p.CustomProperties.Where(
y => y.DeveloperId.Trim().ToLowerInvariant() == DEVELOPERID.ToLowerInvariant());
// only proceed if there is at least one property
if (props.Any())
{
// bind the properties to the view
grdProperties.DataSource = props;
grdProperties.DataBind();
}
}
}
}
}
Need More Help?
Do you need more assistance with this article? Please review your support options.