假設您瞭解選擇/選項和變體以及兩者之間的差異,這可以直接做到。
對於未開始......選擇或選項允許客戶指定同一產品的不同版本,僅此而已。例如,這可能會將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);
}