Copied to clipboard

Flag this post as spam?

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


  • Tolu Jemilua 39 posts 166 karma points
    Aug 15, 2017 @ 12:16
    Tolu Jemilua
    0

    Anybody at all?? Could I get an answer?

    How do I assign options from the shared options to a product via code. I have over 100 colours as a shared option but every product only has maybe one or 2 products within the shared product and I need to assign the correct option from the shared options to each product via code

    I've asked before but no response. Could somebody help me out?

  • Puck Holshuijsen 183 posts 726 karma points
    Aug 16, 2017 @ 06:10
    Puck Holshuijsen
    0

    Hi Tolu,

    I hope this will help!

    First create the options you need, and put them in a dictionairy

    private readonly IDictionary<string, IProductOption> _sharedOptions = new Dictionary<string, IProductOption>();
    
       private void CreateSharedOptions()
        {
            var productOptionService = MerchelloContext.Current.Services.ProductOptionService;
    
            var size = new ProductOption("Size") { Shared = true, UiOption = "select" };
            size.Choices.Add(new ProductAttribute("Small", "sm") { SortOrder = 1, IsDefaultChoice = false });
            size.Choices.Add(new ProductAttribute("Medium", "md") { SortOrder = 2, IsDefaultChoice = false });
            size.Choices.Add(new ProductAttribute("Large", "lg") { SortOrder = 3, IsDefaultChoice = true });
            size.Choices.Add(new ProductAttribute("X-Large", "xl") { SortOrder = 4, IsDefaultChoice = false });
    
            var colour = new ProductOption("Colour") { Shared = true, UiOption = "select" };
            colour.Choices.Add(new ProductAttribute("White", "white") { SortOrder = 1, IsDefaultChoice = false });
            colour.Choices.Add(new ProductAttribute("Black", "black") { SortOrder = 2, IsDefaultChoice = false });
            colour.Choices.Add(new ProductAttribute("Grey", "grey") { SortOrder = 3, IsDefaultChoice = true });
            colour.Choices.Add(new ProductAttribute("Blue", "blue") { SortOrder = 4, IsDefaultChoice = false });
    
            productOptionService.Save(size);
            productOptionService.Save(colour);
    
            _sharedOptions.Add("size", size);
            _sharedOptions.Add("colour", colour);
        }
    

    During a product creating process, you need to add the options to the product.

    private void AddOptionsToProduct(IProduct product)
    {
        foreach (var option in _sharedOptions)
        {
            product.ProductOptions.Add(option.Value);
        }
    }
    
    var product= merchelloServices.ProductService.CreateProduct("product", "product-01", 13.99M);
        ... add all the product variables
    merchelloServices.ProductService.Save(product, false);
    
    AddOptionsToProduct(product);
    merchelloServices.ProductService.Save(product, false);
    

    Hope this helps!

Please Sign in or register to post replies

Write your reply to:

Draft