Question

I have created a new product document type in Kentico in site manager / document types. I added new fields such as size and color. In the ecommerce tab I selected "Document type represents a product type". This created a new table for my new document type in the database with the custom fields.

I am able to add a new content item of this type in my products section in cms desk / content. But in my code, I am not sure how best to use the API in order to access the custom fields I created for this document type. When it was just a product I would use the following:

CMS.Ecommerce.SKUInfoProvider.GetInfoById(PredefinedObjectType.SKU, id)

I can still get the sku info for my new product type using this method, but I do not see how I can access the custom fields I created for my new product type.

Also, I am not sure how Kentico is tying the new table with my custom fields for this product type to the COM_SKU table

Was it helpful?

Solution

The way Kentico ties the CMS to the EMS(E-Commerce management system) is not instantly obvious.

It tries to keep them relatively separate. When you create a product instance in the tree, Kentico does two things:

  • Stores it's ecommerce-relevant data as in a new row COM_SKU table (accessible in code as the SKUInfo object)

  • Creates a new CMS_Tree row (as well as a matching CMS_Document row) of document type Product (or your document type inherited from product)

The database only holds one piece of info to link the two tables - the CMS_Tree.SKUNodeID, which is null for documents that aren't also SKU products.

Either of these tables lets you add custom fields via the Kentico Site Manager CMS. You can create custom fields within the Document Type for the product, or as custom fields for the SKU (within SiteManager->Development->System Tables->COM_SKU).

Depending on whether your information is relevant to the EMS or not should decide where you store it.

Looking at your question, you've already defined the custom fields within the Document part of the object, so would access them using:

TreeNode productTreeNode = TreeHelper.SelectSingleNode("/Home/TreeNode/Path/To/Product");
object customValue = productTreeNode.GetValue("[your_custom_field_name]");

However, If you needed to access custom fields you'd defined within the ecommerce part (the SKUInfo object), it's slightly more difficult. If you have access to the skuID:

SKUInfo skuObject = SKUInfoProvider.getSKUInfo(skuID);
skuObject.GetValue("[your_custom_field_name]");

If you have access to the TreeNode of the product, it should be castable as a SKUTreeNode, and the SKUInfo object (as well as its skuID) accessed from there:

SKUTreeNode skuNode = CurrentDocument as SKUTreeNode;
if( skuNode != null ) {
    skuNode.SKU.SKUID; 
}

Also note:

There can be multiple tree nodes that are assigned a single SKU product. You can also create a product in the tree without also creating a matching SKU object. (There should be a checkbox when creating the instance within the CMS)

OTHER TIPS

I suggest to look at API examples, which you can find in Site manager - Support - Api Examples - Ecommerce. I belive for your situation you have to use document API. Custom fields are accessed by GetValue and SetValue methods.

For your case you should use something like this example:

private bool GetAndUpdateProductDocument()
{
    TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

    // Get the product document
    TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/MyNewProduct", null, true);

    if (node != null)
    {
        // Set the properties
        node.SetValue("ProductDescription", "Product was updated.");
        node.DocumentName = node.DocumentName.ToLowerCSafe();

        // Update the product document
        DocumentHelper.UpdateDocument(node, tree);

        return true;
    }

    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top