2016-05-11 20 views
1

我在我的DNN網站上使用Hotcakes Commerce和電子商務平臺。我一直使用Hotcakes的服務器端API將產品信息從一個Hotcakes安裝轉移到一個乾淨安裝的Hotcakes。長話短說,我的一個數據庫表得到了某種修改,並且不知道它將來如何影響平臺,我需要將所有產品數據移到乾淨的平臺安裝。我通過構建控制檯應用程序和使用服務器端API完成了大部分所需的工作。DNN Hotcakes服務器端API用於創建單個變體

我需要的最後一部分是爲每個產品創建變體信息。我在服務器端API中看到的唯一方法是ProductOptionsGenerateAllVariants()

是否有方法使用服務器端API創建單個變體?

回答

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); 
}