Copied to clipboard

Flag this post as spam?

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


  • M N 125 posts 212 karma points
    Apr 20, 2016 @ 22:25
    M N
    0

    Dynamically adding detached content to product variants does not seem to work.. 1.14.3

    Hey all,

    I am attempting to add detached content to each of my product variants. I can add it to the primary product, but not my variants..

    In breakpoints / database SSMS it looks like it is working (all the PK's line up etc). But I am receiving errors on the backoffice when I click on "Products". It hoses the backoffice, and I am unable to see any products (until I run some code to delete the product). Can't delete via backoffice.

    CODE:

    var prod = productService.CreateProductWithKey("Great Product", "GreatSku", 0M);
    
    prod.ProductOptions.Add(new ProductOption("Color"));
    var optionColor = prod.ProductOptions.First(x => x.Name == "Color");
    optionColor.Choices.Add(new ProductAttribute("Blue", "Blue"));
    
    productService.Save(prod);
    
    //Saving option physically creates a new variant in the database
    
    //can't seem to fetch variant directly and also save, so find it from the main product so we can save prod
    var newVariant = prod.ProductVariants.FirstOrDefault(a => a.Attributes.Any(b => b.Sku == "Blue"));
    
    //Some detached values
    DetachedDataValuesCollection dvals = new DetachedDataValuesCollection();
    dvals.GetOrAdd("colorName", "A Color"); // 1st and last character end up being stripped off!?
    dvals.GetOrAdd("size", "XL"); // 1st and last character end up being stripped off!? = empty string
    
    
    //Create detached content for the variant
    var detachedContent = new ProductVariantDetachedContent(
        newVariant.Key, 
        merchDetachedContentType,
        "en-US",
        dvals)
    { CanBeRendered = true };
    
    newVariant.DetachedContents.Add(detachedContent);
    
    productService.Save(prod);
    

    The code runs fine. No FK errors or anything. But i receive crashes as I mentioned.. And if go directly to the edit variant screen there are missing characters in the detached data!!!!! Help! Screen Shots Below & Thank you in advance!

    See screenshots enter image description here enter image description here

  • Arturo Torres Sánchez 2 posts 71 karma points
    Mar 02, 2017 @ 18:53
    Arturo Torres Sánchez
    0

    Hey, it might be a little late, but I want to share my answer anyway.

    It appears that values for detached content are expected to be serialized JSON. In this case, if your values are string, then you need to explicitly add quotation marks to your values.

    Something like this:

    dvals.GetOrAdd("colorName", "\"A Color\"");
    

    That way you shouldn't get an error in the backoffice.

Please Sign in or register to post replies

Write your reply to:

Draft