Copied to clipboard

Flag this post as spam?

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


  • Jakob Pless 20 posts 60 karma points
    Mar 11, 2015 @ 11:33
    Jakob Pless
    0

    Create Product with Options from code

    I know from https://our.umbraco.org/base/uForum/TopicUrl/59840.aspx how to create a product via code, but what about variants/options? can I create those likewise?

    I'm looking to populate a fresh install with about 40 different products each with 1-3 options.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Mar 11, 2015 @ 19:25
    Rusty Swayne
    101

    Something like this?

     var productService = MerchelloContext.Current.ProductService;
    
     var product = productService.CreateProduct("My Name", "my-sku", 10M);
     product.ProductOptions.Add(new ProductOption("Color"));
     product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Black", "Blk"));
     product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blu"));
     product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
     product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Gre"));
     product.ProductOptions.Add(new ProductOption("Size"));
     product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
     product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "M"));
     product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
     product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
     product.Height = 20;
     product.Weight = 20;
     product.Length = 20;
     product.Width = 20;
     product.Shippable = true;
     productService.Save(product);
    
  • progproger 52 posts 130 karma points
    Oct 02, 2015 @ 11:17
    progproger
    0

    Hi Rusty,

    How do I also change product variant prices,sale prices , barcodes etc. in this case?

  • Jakob Pless 20 posts 60 karma points
    Mar 12, 2015 @ 07:21
    Jakob Pless
    0

    Perfect! Thank you :-)

  • Steve Borg 28 posts 99 karma points
    Jun 02, 2017 @ 08:40
    Steve Borg
    0

    I am using very similar code to the example code provided by Rusty above. I am having problems with performance. The back-office is very slow re: products and the following line in my view is taking 3 seconds to return on a product with a total of 140 variants :

    ProductDisplay merchelloProduct = umbracoProduct.GetPropertyValue<ProductDisplay>("merchelloProduct");
    

    If I create the same product non-programmatically in the back-office then the above line returns much, much faster. Therefore I am of the opinion that something else should be done after programmatically creating the product.

    Does anyone please have an example of code that adds the product and also deals with the performance issues?

    Many thanks for your help.

  • Puck Holshuijsen 184 posts 727 karma points
    Jun 06, 2017 @ 08:29
    Puck Holshuijsen
    0

    Just a thought: did you run the indexer for products? I also had performance issues with a lot of variants, until i ran the indexer.

    Not sure if/how you can do that programmatically though

  • Steve Borg 28 posts 99 karma points
    Jun 06, 2017 @ 11:16
    Steve Borg
    0

    Hi Puck,

    Many thanks for your post. I am running the MerchelloProduct Indexer in the Examine Management section of Umbraco after each import. I am doing it manually at the moment but will need to do that programmatically also.

    I have found that if I don't add extended content to each of my variants then the GetPropertyValue runs quicker but the back-office still runs very slowly in the Merchello section.

    Another issue I was seeing was that the SetCulture method had no effect on the programmatically added products but worked fine on my manually created ones. This is now resolved when I added the detached content for all the languages but the slow back-office and slow front-end calls remain. So it is fairly evident that my code is not quite correct or incomplete.

    Best regards,

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft