用于创建单个变体的DNN Hotcakes Server Side API

时间:2016-05-11 18:33:22

标签: e-commerce dotnetnuke

我在我的DNN网站上使用Hotcakes Commerce和电子商务平台。我一直在使用Hotcakes的服务器端API将产品信息从一个Hotcakes安装转移到干净的Hotcakes安装。长话短说,我的一个数据库表以某种方式被修改,不知道将来它将如何影响平台我需要将所有产品数据移动到平台的干净安装。通过构建控制台应用程序和使用服务器端API,我完成了所需的大部分工作。

我需要的最后一件事是为每个产品创建变体信息。我在服务器端API中看到的唯一方法是ProductOptionsGenerateAllVariants()

有没有办法使用服务器端API创建单个变体?

1 个答案:

答案 0 :(得分:0)

这有点简单,假设您了解选择/选项和变体 - 以及两者之间的差异。

对于没有经验的人......选择或选项允许客户指定同一产品的不同版本,仅此而已。这方面的一个例子可能是将T恤颜色从蓝色变为灰色。除了颜色外没有其他任何改变。

变体仍然是其核心选择,但它还可以改变SKU和/或定价。这方面的一个例子是在购买笔记本电脑时选择屏幕尺寸。 17英寸屏幕将比15英寸屏幕更昂贵,库存可能会受到不同影响,并且可能完全不同的SKU /型号。

当您创建单个变体时,您将需要正确的信息来创建选项,其变量属性设置为true,然后您需要将这些变量与产品相关联。话虽如此,在某些商店中,即使是单一产品,也可能存在数百万种可能的变体。因此,代码并不像任何人想要的那样干净,但下面是一个例子。

using System;
using System.Collections.Generic;
using System.Linq;
using Hotcakes.Commerce.Catalog;
using Hotcakes.Commerce.Extensions;

// get an instance of the application
var HccApp = HccAppHelper.InitHccApp();

// get an instance of the product which you'd like to add a variant to
var product = HccApp.CatalogServices.Products.FindBySku("SAMPLE003");

// get a list of the options that can become variants
var variantOptions = product.Options.VariantsOnly();

// we'll fill this list with choices that we wish to make variants below
var selections = new List<OptionSelection>();

// repeat this line of code for each choice in the product that makes up this variant
// replace both of the parameters when adding the new OptionSelection, based on your use case
selections.Add(new OptionSelection(variantOptions[0].Bvin, "REPLACE THIS WITH THE INDIVIDUAL CHOICE BVIN"));

// create a new variant object
var newVariant = new Variant()
{
    ProductId = product.Bvin
};

// specify the choices that make up this variant
newVariant.Selections.AddRange(selections);

// get the unique key to use to compare below
var variantKey = newVariant.UniqueKey();

// check to see if the variant already exists first
if (!product.Variants.Any(v => v.UniqueKey() == variantKey))
{
    // create the single variant
    HccApp.CatalogServices.ProductVariants.Create(newVariant);
}