OVERVIEW
This article will help you as a developer in cases where you need to access product type properties for a specific product. For example, you might be building a custom viewset and controller and need to display individual product type properties to your customers.
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, or a custom workflow.
READ AND DISPLAY PRODUCT TYPE PROPERTIES
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 orders.
The examples below are thoroughly commented. The first two tabs assume you are writing a web forms interface, while the last two views apply to a custom viewset.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductTypeProperties.ascx.cs" Inherits="CodeSamples.ProductTypeProperties" %>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using DotNetNuke.Entities.Modules;
using Hotcakes.Commerce;
using Hotcakes.Commerce.Catalog;
using Hotcakes.Commerce.Extensions;
namespace CodeSamples
{
public partial class ProductTypeProperties : PortalModuleBase
{
#region Private Members
private Collection _productTypePropertyValues = new Collection();
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
// get an instance of the Hotcakes Application
var HccApp = HccAppHelper.InitHccApp();
// find the product you want information on
// "SAMPLE004" is the brown fedora in the sample products
var p = HccApp.CatalogServices.Products.FindBySku("SAMPLE004");
// this is simply to output this to the page in this example
var sb = new StringBuilder();
// output all type properties for the product
LookForTypeProperties(p, ref sb, ref HccApp);
// output the type properties in an unordered list instead
//GetFormattedTypePropertiesList(p, ref sb, ref HccApp);
// display the output in the page for the example
plcOptions.Controls.Add(new LiteralControl(sb.ToString()));
}
#endregion
private void LookForTypeProperties(Product p, ref StringBuilder sb, ref HotcakesApplication HccApp)
{
// grab a list of all of the type properties for the product type
List props = HccApp.CatalogServices.ProductPropertiesFindForType(p.ProductTypeId);
// if false, this will change the overload below to not show hidden type properties
var forDropShipper = true;
// filter the list to only show displayable type properties
List displayableProperties = props.
Where(y => (y.DisplayToDropShipper && forDropShipper) || y.DisplayOnSite).
ToList();
// get the saved product type property values
LoadPropertyTypeValues(p.Bvin, ref displayableProperties, ref HccApp);
// this might need to be this instead
//LoadPropertyTypeValues(p.Id.ToString(), ref displayableProperties, ref HccApp);
// if there are any type properties...
if (displayableProperties.Any())
{
sb.Append("
Type Properties
"); sb.Append("- "); var count = 0; // display each of the type properties in a bullet list foreach (var property in displayableProperties) { // this is a system name, only seen by store admins string key = property.PropertyName; // this is a display name that everyone sees string keyDisplay = property.DisplayName; // this is the value of the type property // use this is there isn't a saved value //string value = property.DefaultValue; string value = _productTypePropertyValues[count - 1]; // used if you have multiple choices for the type property //string valueChoice = property.GetChoiceValue("choiceName"); // output the type property in the example page sb.AppendFormat("
- Property Name: {0}
Display Name: {1}
Default Value: {2}
", key, keyDisplay, value); count++; } sb.Append("
");
}
else
{
sb.AppendFormat(
"
",
p.ProductName, p.Sku);
}
}
private void LoadPropertyTypeValues(string ProductId, ref List Properties,
ref HotcakesApplication HccApp)
{
foreach (ProductProperty prop in Properties)
{
_productTypePropertyValues.Add(HccApp.CatalogServices.ProductPropertyValues.GetPropertyValue(ProductId,
prop.Id));
}
}
///
///
/// This is useful if you want a pre-formatted UL of the type properties
///
///
private void GetFormattedTypePropertiesList(Product p, ref StringBuilder sb, ref HotcakesApplication HccApp)
{
// just ask for the type properties
string typeProperties = p.RenderTypeProperties(HccApp);
// display it if it isn't empty
if (!string.IsNullOrEmpty(typeProperties))
{
sb.AppendFormat("
Type Properties:
"); // output the UL version of the product type properties sb.Append(typeProperties); sb.Append("");
}
else
{
sb.AppendFormat(
"
",
p.ProductName, p.Sku);
}
}
}
}
It is probably a good idea to name this view something like _ProductTypeProperties.cshtml.
@using Hotcakes.Commerce.Catalog
@model Hotcakes.Modules.Core.Models.ProductPageViewModel
@functions{
string getPropertyTypeValue()
{
// this is the system name of the custom property you want to find
var propertyName = "MyCustomProperty";
// get an instance of the current store
var HccApp = HccAppHelper.InitHccApp();
// grab a list of all of the type properties for the product type
List props = HccApp.CatalogServices.ProductPropertiesFindForType(Model.LocalProduct.ProductTypeId);
// filter the list to only show displayable type properties
List displayableProperties = props.
Where(y => y.PropertyName == propertyName).
ToList();
// return the value you want
return HccApp.CatalogServices.ProductPropertyValues.GetPropertyValue(Model.LocalProduct.Bvin, displayableProperties.FirstOrDefault().Id);
}
}
@getPropertyTypeValue()
Place this into the view or partial view that you want this to be part of. This example was built to be part of the _ProductDetails.cshtml view. This example assumes that your partial view is named _ProductTypeProperties.cshtml and it is in the same folder as the view you are editing.
@Html.Partial("_ProductTypeProperties", Model)
Need More Help?
Do you need more assistance with this article? Please review your support options.