/// <summary> /// Gets a test product draft. /// </summary> /// <param name="project">Project</param> /// <param name="productTypeId">Product type ID</param> /// <param name="taxCategoryId">Tax category ID</param> /// <returns></returns> public static ProductDraft GetTestProductDraft(Project.Project project, string productTypeId, string taxCategoryId) { List <PriceDraft> priceDrafts = new List <PriceDraft>(); foreach (string currency in project.Currencies) { Money value = new Money(); value.CurrencyCode = currency; value.CentAmount = Helper.GetRandomNumber(10, 999999); priceDrafts.Add(new PriceDraft(value)); } string randomSku = Helper.GetRandomString(10); ProductVariantDraft productVariantDraft = new ProductVariantDraft(); productVariantDraft.Sku = randomSku; productVariantDraft.Prices = priceDrafts; ResourceIdentifier productType = new ResourceIdentifier(); productType.Id = productTypeId; productType.TypeId = Common.ReferenceType.ProductType; LocalizedString name = new LocalizedString(); LocalizedString slug = new LocalizedString(); LocalizedString description = new LocalizedString(); LocalizedString metaTitle = new LocalizedString(); LocalizedString metaDescription = new LocalizedString(); LocalizedString metaKeywords = new LocalizedString(); foreach (string language in project.Languages) { name.SetValue(language, string.Concat("Test Product ", language, " ", randomSku)); slug.SetValue(language, string.Concat("test-product-", language, "-", randomSku)); description.SetValue(language, string.Concat("Created by commercetools.NET ", language)); metaTitle.SetValue(language, string.Concat("Product Meta Title ", language)); metaDescription.SetValue(language, string.Concat("Product Meta Description ", language)); metaKeywords.SetValue(language, string.Concat("Product Meta Keywords ", language)); } Reference taxCategory = new Reference(); taxCategory.Id = taxCategoryId; taxCategory.ReferenceType = Common.ReferenceType.TaxCategory; ProductDraft productDraft = new ProductDraft(name, productType, slug); productDraft.Key = Helper.GetRandomString(15); productDraft.Description = description; productDraft.MetaTitle = metaTitle; productDraft.MetaDescription = metaDescription; productDraft.MetaKeywords = metaKeywords; productDraft.TaxCategory = taxCategory; productDraft.MasterVariant = productVariantDraft; return(productDraft); }
/// <summary> /// Get Random Product Variant Draft with attributes /// </summary> /// <param name="referenceId"></param> /// <param name="referenceTypeId"></param> /// <param name="prices"></param> /// <param name="attributes"></param> /// <returns></returns> public static ProductVariantDraft GetRandomProductVariantDraft(string referenceId = "", ReferenceTypeId?referenceTypeId = null, List <PriceDraft> prices = null, List <Attribute> attributes = null) { var productVariantDraft = new ProductVariantDraft() { Key = RandomString(10), Sku = RandomString(10), Prices = prices ?? GetRandomListOfPriceDraft(),//two prices Attributes = attributes ?? GetListOfRandomAttributes(referenceId, referenceTypeId) }; return(productVariantDraft); }
public ProductDraft GetProductDraft(Category category, ProductType productType, bool withVariants = false, bool publish = false) { ProductDraft productDraft = new ProductDraft(); productDraft.Name = new LocalizedString() { { "en", this.RandomString(10) } }; productDraft.Key = this.RandomString(10); productDraft.Slug = new LocalizedString() { { "en", this.RandomString(10) } }; productDraft.ProductType = new ResourceIdentifier <ProductType> { Key = productType.Key }; ProductVariantDraft productMasterVariant = this.GetRandomProductVariantDraft(category.Id, ReferenceTypeId.Category); productDraft.MasterVariant = productMasterVariant; productDraft.Categories = new List <IReferenceable <Category> > { new ResourceIdentifier <Category> { Key = category.Key } }; productDraft.Publish = publish; //if true, the product is published immediately if (withVariants) //then create variants for this product { productDraft.Variants = new List <ProductVariantDraft>(); var productVariantDraft = this.GetRandomProductVariantDraft(category.Id, ReferenceTypeId.Category); productDraft.Variants.Add(productVariantDraft); } //Add taxCategory to product var taxCategory = CreateNewTaxCategory(); productDraft.TaxCategory = new Reference <TaxCategory>() { Id = taxCategory.Id }; return(productDraft); }
/// <summary> /// Get Random Product Variant Draft with attributes /// </summary> /// <param name="referenceAttributeId"></param> /// <param name="referenceTypeId"></param> /// <returns></returns> public ProductVariantDraft GetRandomProductVariantDraft(string referenceAttributeId = "", ReferenceTypeId?referenceTypeId = null) { var price = GetRandomPriceDraft(); var productVariantDraft = new ProductVariantDraft() { Key = this.RandomString(10), Sku = this.RandomString(10), Prices = new List <PriceDraft>() { price }, Attributes = GetListOfRandomAttributes(referenceAttributeId, referenceTypeId) }; return(productVariantDraft); }
public static ProductDraft DefaultProductDraftWithMultipleVariants(ProductDraft draft, int variantsCount = 1) { var randomInt = TestingUtility.RandomInt(); var productDraft = DefaultProductDraft(draft); var variants = new List <ProductVariantDraft>(); for (int i = 1; i <= variantsCount; i++) { var productVariant = new ProductVariantDraft { Key = $"variant{i}_Key_{randomInt}", Sku = $"variant{i}_Sku_{randomInt}" }; variants.Add(productVariant); } productDraft.Variants = variants; return(productDraft); }