Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nguyen Dung Tri 106 posts 606 karma points
    Sep 18, 2016 @ 17:43
    Nguyen Dung Tri
    0

    How to add brief and description to a product?

    Hello,

    I want to add brief and description to a product (see image) from code behind.

    enter image description here

    I have looked into ProductApiController.cs under Merchello.Web and see some posible function to help me to do that:

    public ProductDisplay PutProductWithDetachedContent(
                [ModelBinder(typeof(ProductContentSaveBinder))]
                ProductContentSave detachedContentItem)
            {
                ProductVariantDetachedContentHelper<ProductContentSave, ProductDisplay>.MapDetachedProperties(detachedContentItem);
    
                var merchProduct = _productService.GetByKey(detachedContentItem.Display.Key);
    
                merchProduct = detachedContentItem.Display.ToProduct(merchProduct);
    
                _productService.Save(merchProduct);
    
                return merchProduct.ToProductDisplay(DetachedValuesConversionType.Editor);
            }
    

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

    But still I don't know how to modify the code to help me to insert a brief and description to a particular product by ID. Please help me with this.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 18, 2016 @ 18:33
    Rusty Swayne
    1

    You should not need to alter any code at all and can add any number of properties to you product you want by adding them to the document type you have associated with your product.

    From the front end you access the properties in the same way you do with a normal Umbraco page.

      var brief = product.GetPropertyValue<IHtmlString>("brief");
    
  • Nguyen Dung Tri 106 posts 606 karma points
    Sep 18, 2016 @ 18:38
    Nguyen Dung Tri
    0

    Actually, I'm writing a method to create a product and try to add brief and description into new created product automatically. So I need a short code to do that.

    Does "product" in your line of code has data type of "ProductDisplay" or "IProduct"?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 18, 2016 @ 18:41
    Rusty Swayne
    100

    As long as you have the document type setup in Umbraco it should be pretty straight forward.

    Checkout how we do it in the FastTrack installer: https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.FastTrack/Install/FastTrackDataInstaller.cs#L525

  • Nguyen Dung Tri 106 posts 606 karma points
    Sep 18, 2016 @ 22:39
    Nguyen Dung Tri
    0

    I got stuck at this code block:

    // we need to remove the detached content to generate the product to begin with due to db foreign keys
                var detachedContents = product.DetachedContents.ToArray();
                product.DetachedContents = Enumerable.Empty<ProductVariantDetachedContentDisplay>();
    
                // First create the product record and save it
                var merchProduct = _productService.CreateProduct(product.Name, product.Sku, product.Price);
                merchProduct = product.ToProduct(merchProduct);
    
                // Add description to product
                var detachedContentType = detachedContents.FirstOrDefault();
                ProductVariantDetachedContent detachedContent = new ProductVariantDetachedContent(
                       merchProduct.ProductVariantKey,
                       detachedContentType,
                       "en-US",
                       new DetachedDataValuesCollection(
                           new[]
                                {
                                    new KeyValuePair<string, string>("description", offerDescription),
                                    new KeyValuePair<string, string>("brief", ""),
                                    new KeyValuePair<string, string>("image", "")
                                }))
                {
                    CanBeRendered = true
                };
                merchProduct.DetachedContents.Add(detachedContent);
    

    The code generate an error:

    enter image description here

    How can I convert "ProductVarianDetachedContentDisplay" to "IDetachedContentType" to be used to get "detachedContentType"?

Please Sign in or register to post replies

Write your reply to:

Draft