/// <summary> /// Converts Lucene cached ProductVariant into <see cref="ProductVariantDisplay"/> /// </summary> /// <param name="result"> /// The result. /// </param> /// <returns> /// The <see cref="ProductVariantDisplay"/>. /// </returns> internal static ProductVariantDisplay ToProductVariantDisplay(this SearchResult result) { var pvd = new ProductVariantDisplay() { Key = FieldAsGuid(result, "productVariantKey"), ProductKey = FieldAsGuid(result, "productKey"), Name = FieldAsString(result, "name"), Sku = FieldAsString(result, "sku"), Price = FieldAsDecimal(result, "price"), OnSale = FieldAsBoolean(result.Fields["onSale"]), SalePrice = FieldAsDecimal(result, "salePrice"), Manufacturer = FieldAsString(result, "manufacturer"), ManufacturerModelNumber = FieldAsString(result, "modelNumber"), CostOfGoods = FieldAsDecimal(result, "costOfGoods"), Weight = FieldAsDecimal(result, "weight"), Length = FieldAsDecimal(result, "length"), Height = FieldAsDecimal(result, "height"), Width = FieldAsDecimal(result, "width"), Barcode = result.Fields.ContainsKey("barcode") ? result.Fields["barcode"] : string.Empty, Available = FieldAsBoolean(result.Fields["available"]), TrackInventory = FieldAsBoolean(result.Fields["trackInventory"]), OutOfStockPurchase = FieldAsBoolean(result.Fields["outOfStockPurchase"]), TotalInventoryCount = FieldAsInteger(result, "totalInventoryCount"), Taxable = FieldAsBoolean(result.Fields["taxable"]), Shippable = FieldAsBoolean(result.Fields["shippable"]), Download = FieldAsBoolean(result.Fields["download"]), DownloadMediaId = FieldAsInteger(result, "downloadMediaId"), VersionKey = FieldAsGuid(result, "versionKey"), Attributes = RawJsonFieldAsCollection <ProductAttributeDisplay>(result, "attributes"), CatalogInventories = RawJsonFieldAsCollection <CatalogInventoryDisplay>(result, "catalogInventories"), DetachedContents = GetProductVariantDetachedContentDisplayCollection(result, "detachedContents") }; return(pvd); }
/// <summary> /// Initializes a new instance of the <see cref="ProductDisplay"/> class. /// </summary> /// <param name="masterVariant"> /// The master variant. /// </param> internal ProductDisplay(ProductVariantDisplay masterVariant) { Key = masterVariant.ProductKey; ProductVariantKey = masterVariant.Key; VersionKey = masterVariant.VersionKey; Name = masterVariant.Name; Sku = masterVariant.Sku; Price = masterVariant.Price; CostOfGoods = masterVariant.CostOfGoods; SalePrice = masterVariant.SalePrice; OnSale = masterVariant.OnSale; Manufacturer = masterVariant.Manufacturer; ManufacturerModelNumber = masterVariant.ManufacturerModelNumber; Weight = masterVariant.Weight; Length = masterVariant.Length; Height = masterVariant.Height; Barcode = masterVariant.Barcode; Available = masterVariant.Available; TrackInventory = masterVariant.TrackInventory; OutOfStockPurchase = masterVariant.OutOfStockPurchase; Taxable = masterVariant.Taxable; Shippable = masterVariant.Shippable; Download = masterVariant.Download; DownloadMediaId = masterVariant.DownloadMediaId; CatalogInventories = masterVariant.CatalogInventories; }
/// <summary> /// Initializes a new instance of the <see cref="ProductDisplay"/> class. /// </summary> /// <param name="masterVariant"> /// The master variant. /// </param> internal ProductDisplay(ProductVariantDisplay masterVariant) { Key = masterVariant.ProductKey; ProductVariantKey = masterVariant.Key; VersionKey = masterVariant.VersionKey; Name = masterVariant.Name; Sku = masterVariant.Sku; Price = masterVariant.Price; CostOfGoods = masterVariant.CostOfGoods; SalePrice = masterVariant.SalePrice; OnSale = masterVariant.OnSale; Manufacturer = masterVariant.Manufacturer; ManufacturerModelNumber = masterVariant.ManufacturerModelNumber; Weight = masterVariant.Weight; Length = masterVariant.Length; Height = masterVariant.Height; Width = masterVariant.Width; Barcode = masterVariant.Barcode; Available = masterVariant.Available; TrackInventory = masterVariant.TrackInventory; OutOfStockPurchase = masterVariant.OutOfStockPurchase; Taxable = masterVariant.Taxable; Shippable = masterVariant.Shippable; Download = masterVariant.Download; DownloadMediaId = masterVariant.DownloadMediaId; CatalogInventories = masterVariant.CatalogInventories; VersionKey = masterVariant.VersionKey; }
/// <summary> /// Creates <see cref="IProductVariantContent"/> from the display object. /// </summary> /// <param name="display"> /// The display. /// </param> /// <param name="factory"> /// The factory. /// </param> /// <returns> /// The <see cref="IProductVariantContent"/>. /// </returns> public static IProductVariantContent AsProductContent(this ProductVariantDisplay display, ProductContentFactory factory) { if (!display.DetachedContents.Any(x => x.CanBeRendered)) { return(null); } return(factory.BuildContent(display)); }
/// <summary> /// Turns a product variant into an InvoiceLineItem /// </summary> /// <param name="productVariant"></param> /// <param name="qty"></param> /// <param name="taxIncludedInProductPrice"></param> /// <returns></returns> internal static InvoiceLineItem ToInvoiceLineItem(this ProductVariantDisplay productVariant, int qty = 1, bool taxIncludedInProductPrice = false) { var extendedData = new ExtendedDataCollection(); extendedData.AddProductVariantValues(productVariant); if (taxIncludedInProductPrice) { extendedData.TryAdd(Constants.ExtendedDataKeys.TaxIncludedInProductPrice, true.ToString()); } // See if this variant is on sale var price = productVariant.OnSale ? productVariant.SalePrice : productVariant.Price; // TODO - Can we remove this extra step to turn into a line item var itemCacheLineItem = new ItemCacheLineItem(LineItemType.Product, productVariant.Name, productVariant.Sku, qty, price, extendedData); return(itemCacheLineItem.AsLineItemOf <InvoiceLineItem>()); }
/// <summary> /// The add product variant values. /// </summary> /// <param name="extendedData"> /// The extended data. /// </param> /// <param name="productVariant"> /// The product variant. /// </param> public static void AddProductVariantValues(this ExtendedDataCollection extendedData, ProductVariantDisplay productVariant) { extendedData.SetValue(Constants.ExtendedDataKeys.ProductKey, productVariant.ProductKey.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.ProductVariantKey, productVariant.Key.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.CostOfGoods, productVariant.CostOfGoods.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Weight, productVariant.Weight.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Width, productVariant.Width.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Height, productVariant.Height.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Length, productVariant.Length.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Barcode, productVariant.Barcode); extendedData.SetValue(Constants.ExtendedDataKeys.Price, productVariant.Price.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.OnSale, productVariant.OnSale.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Manufacturer, productVariant.Manufacturer); extendedData.SetValue(Constants.ExtendedDataKeys.ManufacturerModelNumber, productVariant.ManufacturerModelNumber); extendedData.SetValue(Constants.ExtendedDataKeys.SalePrice, productVariant.SalePrice.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.TrackInventory, productVariant.TrackInventory.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.OutOfStockPurchase, productVariant.OutOfStockPurchase.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Taxable, productVariant.Taxable.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Shippable, productVariant.Shippable.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Download, productVariant.Download.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.DownloadMediaId, productVariant.DownloadMediaId.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.VersionKey, productVariant.VersionKey.ToString()); }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if (!String.IsNullOrEmpty(productVariantDisplay.Name)) { destination.Name = productVariantDisplay.Name; } if (!String.IsNullOrEmpty(productVariantDisplay.Sku)) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; // We need to refactor the CatalogInventories to not be immutable if we are // going to need to do operations like this. In the UI, the user "unchecks" a catalog that was // previously checked - so we need to remove it. var deletedCatalogKeys = destination.CatalogInventories.Where( x => !productVariantDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { destination.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity) { //// Add to a new catalog destination.AddToCatalogInventory(catalogInventory.CatalogKey); } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.FirstOrDefault(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } destination.AddOrUpdateDetachedContent(productVariantDisplay); return(destination); }
public ProductVariantDisplay PutProductVariant(ProductVariantDisplay productVariant) { var variant = _productVariantService.GetByKey(productVariant.Key); variant = productVariant.ToProductVariant(variant); _productVariantService.Save(variant); return variant.ToProductVariantDisplay(); }
/// <summary> /// Adds a <see cref="ProductVariantDisplay"/> to the item cache /// </summary> /// <param name="productVariant"> /// The product variant to be added /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="quantity"> /// The quantity to be represented /// </param> public void AddItem(ProductVariantDisplay productVariant, string name, int quantity) { AddItem(productVariant, name, quantity, new ExtendedDataCollection()); }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if (!String.IsNullOrEmpty(productVariantDisplay.Name)) { destination.Name = productVariantDisplay.Name; } if (!String.IsNullOrEmpty(productVariantDisplay.Sku)) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { ICatalogInventory destinationCatalogInventory; if (destination.CatalogInventories.Count() > 0) { var catInv = destination.CatalogInventories.Where(x => x.CatalogKey == catalogInventory.CatalogKey).First(); if (catInv != null) { destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.First(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } return(destination); }
public HttpResponseMessage DeleteDetachedContent(ProductVariantDisplay productVariant) { var product = _productService.GetByKey(productVariant.ProductKey); if (product == null) return Request.CreateResponse(HttpStatusCode.NotFound); if (product.ProductVariants.Any() && product.ProductVariants.FirstOrDefault(x => x.Key == productVariant.Key) != null) { var variant = product.ProductVariants.FirstOrDefault(x => x.Key == productVariant.Key); if (variant != null) variant.DetachedContents.Clear(); //// TODO need to walk this through better, we should not need to save the variant and then the product //// as the product save should take care of it, but somewhere in the service the runtime cache is resetting //// the variant's detached content in the productvariant collection. Probably just need to rearrange some of the //// calls in the service - suspect EnsureProductVariants. _productVariantService.Save(variant); } else { product.DetachedContents.Clear(); } _productService.Save(product); return Request.CreateResponse(HttpStatusCode.OK); }
public HttpResponseMessage PutProductVariant(ProductVariantDisplay productVariant) { var response = Request.CreateResponse(HttpStatusCode.OK); try { IProductVariant merchProductVariant = _productVariantService.GetByKey(productVariant.Key); if (productVariant.TrackInventory && !merchProductVariant.CatalogInventories.Any()) { merchProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog()); } merchProductVariant = productVariant.ToProductVariant(merchProductVariant); _productVariantService.Save(merchProductVariant); } catch (Exception ex) // I think this is not required as the server will create the error response message anyway { response = Request.CreateResponse(HttpStatusCode.NotFound, String.Format("{0}", ex.Message)); } return response; }
public ProductVariantDisplay NewProductVariant(ProductVariantDisplay productVariant) { IProductVariant newProductVariant; try { var product = _productService.GetByKey(productVariant.ProductKey) as Product; if (product != null) { var productAttributes = new ProductAttributeCollection(); foreach (var attribute in productVariant.Attributes) { // TODO: This should be refactored into an extension method var productOption = product.ProductOptions.FirstOrDefault(x => x.Key == attribute.OptionKey) as ProductOption; if (productOption != null) productAttributes.Add(productOption.Choices[attribute.Key]); } newProductVariant = _productVariantService.CreateProductVariantWithKey(product, productAttributes); if (productVariant.TrackInventory) { newProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog()); } newProductVariant = productVariant.ToProductVariant(newProductVariant); _productVariantService.Save(newProductVariant); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } } catch (Exception e) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return newProductVariant.ToProductVariantDisplay(); }
/// <summary> /// Initializes a new instance of the <see cref="ProductVariantContent"/> class. /// </summary> /// <param name="variant"> /// The variant. /// </param> /// <param name="contentType"> /// The content type. /// </param> /// <param name="optionContentTypes"> /// The option Content Types. /// </param> /// <param name="cultureName"> /// The culture name. /// </param> /// <param name="parent"> /// The parent. /// </param> public ProductVariantContent(ProductVariantDisplay variant, PublishedContentType contentType, IDictionary<Guid, PublishedContentType> optionContentTypes, string cultureName = "en-US", IPublishedContent parent = null) : base(variant, contentType, optionContentTypes, cultureName) { _variant = variant; _parent = parent; }
/// <summary> /// Initializes a new instance of the <see cref="ProductVariantContent"/> class. /// </summary> /// <param name="variant"> /// The variant. /// </param> /// <param name="contentType"> /// The content type. /// </param> /// <param name="cultureName"> /// The culture name. /// </param> /// <param name="parent"> /// The parent. /// </param> public ProductVariantContent(ProductVariantDisplay variant, PublishedContentType contentType, string cultureName = "en-US", IPublishedContent parent = null) : base(variant, contentType, cultureName) { _variant = variant; _parent = parent; }
/// <summary> /// Adds a <see cref="ProductVariantDisplay"/> to the item cache /// </summary> /// <param name="productVariant">The product variant to be added</param> public void AddItem(ProductVariantDisplay productVariant) { AddItem(productVariant, 1); }
/// <summary> /// Adds a <see cref="ProductVariantDisplay"/> to the item cache /// </summary> /// <param name="productVariant">The product variant to be added</param> /// <param name="quantity">The quantity to be represented</param> public void AddItem(ProductVariantDisplay productVariant, int quantity) { AddItem(productVariant, productVariant.Name, quantity); }
/// <summary> /// The add product variant values. /// </summary> /// <param name="extendedData"> /// The extended data. /// </param> /// <param name="productVariant"> /// The product variant. /// </param> public static void AddProductVariantValues(this ExtendedDataCollection extendedData, ProductVariantDisplay productVariant) { extendedData.SetValue(Constants.ExtendedDataKeys.ProductKey, productVariant.ProductKey.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.ProductVariantKey, productVariant.Key.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.CostOfGoods, productVariant.CostOfGoods.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Weight, productVariant.Weight.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Width, productVariant.Width.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Height, productVariant.Height.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Length, productVariant.Length.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.Barcode, productVariant.Barcode); extendedData.SetValue(Constants.ExtendedDataKeys.Price, productVariant.Price.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.OnSale, productVariant.OnSale.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Manufacturer, productVariant.Manufacturer); extendedData.SetValue(Constants.ExtendedDataKeys.ManufacturerModelNumber, productVariant.ManufacturerModelNumber); extendedData.SetValue(Constants.ExtendedDataKeys.SalePrice, productVariant.SalePrice.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.TrackInventory, productVariant.TrackInventory.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.OutOfStockPurchase, productVariant.OutOfStockPurchase.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Taxable, productVariant.Taxable.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Shippable, productVariant.Shippable.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.Download, productVariant.Download.ToString()); extendedData.SetValue(Constants.ExtendedDataKeys.DownloadMediaId, productVariant.DownloadMediaId.ToString(CultureInfo.InvariantCulture)); extendedData.SetValue(Constants.ExtendedDataKeys.VersionKey, productVariant.VersionKey.ToString()); }
/// <summary> /// Adds a <see cref="ProductVariantDisplay"/> to the item cache /// </summary> /// <param name="productVariant"> /// The product variant to be added /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="quantity"> /// The quantity to be represented /// </param> /// <param name="extendedData"> /// The extended Data. /// </param> public void AddItem(ProductVariantDisplay productVariant, string name, int quantity, ExtendedDataCollection extendedData) { if (EnableDataModifiers) { var attempt = _productDataModifier.Value.Modify(productVariant); if (attempt.Success) { var modified = attempt.Result as ProductVariantDisplay; if (modified != null) { extendedData.MergeDataModifierLogs(modified); if (!extendedData.DefinesProductVariant()) extendedData.AddProductVariantValues(modified); productVariant = modified; } } } else { extendedData.MergeDataModifierLogs(productVariant); if (!extendedData.DefinesProductVariant()) extendedData.AddProductVariantValues(productVariant); } var price = productVariant.OnSale ? extendedData.GetSalePriceValue() : extendedData.GetPriceValue(); AddItem(string.IsNullOrEmpty(name) ? productVariant.Name : name, productVariant.Sku, quantity, price, extendedData); }
public ProductVariantDisplay PutProductVariant(ProductVariantDisplay productVariant) { var variant = _productVariantService.GetByKey(productVariant.Key); if (productVariant.DetachedContents.Any()) { foreach (var c in productVariant.DetachedContents.Select(x => x.CultureName)) { var pcs = new ProductVariantContentSave { CultureName = c, Display = productVariant }; ProductVariantDetachedContentHelper<ProductVariantContentSave, ProductVariantDisplay>.MapDetachedProperties(pcs); } } variant = productVariant.ToProductVariant(variant); _productVariantService.Save(variant); return variant.ToProductVariantDisplay(DetachedValuesConversionType.Editor); }