Copied to clipboard

Flag this post as spam?

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


  • Nick Hoang 51 posts 180 karma points
    Oct 01, 2019 @ 04:53
    Nick Hoang
    0

    How to set product quantity programtically

    I'm doing import products to Merchello but can't set the quantity of products, here's what I did:

    var warehouse = MerchelloContext.Current.Services.WarehouseService.GetDefaultWarehouse();
    var catalog = warehouse.WarehouseCatalogs.FirstOrDefault();
    if (product.Shippable)
    {
         product.AddToCatalogInventory(catalog);     
         MerchelloContext.Current.Services.ProductService.Save(product);
    
         product.TrackInventory = true;
         product.CatalogInventories.First().Count = 100;               
         MerchelloContext.Current.Services.ProductService.Save(product);
    }
    

    Could you help to tell what's wrong here?

    Thanks,

    Nick

  • Puck Holshuijsen 184 posts 727 karma points
    Oct 01, 2019 @ 05:59
    Puck Holshuijsen
    1

    Hi Nick,

    I'm not sure what's wrong, but it could be the save after the addtocataloginventory.

    Here's some code of mine (it is the productvariant, but should work the same).

    var productVariant = merchelloServices.ProductVariantService.GetBySku(productId);
                                if (productVariant == null) {
                                    productVariant = merchelloServices.ProductVariantService.CreateProductVariantWithKey(masterProduct, description, productId, price, attributes);
                                    productVariant.AddToCatalogInventory(catalog);
    
    
                                        var inventory = productVariant.CatalogInventories.First();
                                        if (inventory != null) {
                                            inventory.Count = 100;
                                            merchelloServices.ProductVariantService.Save(productVariant);
                                        }
    
                                }
                                productVariant.SalePrice = masterProduct.SalePrice;
                                productVariant.OnSale = masterProduct.OnSale;
                                productVariant.Shippable = true;
                                productVariant.Taxable = true;
                                productVariant.TrackInventory = true;
                                productVariant.Available = true;
                                productVariant.OutOfStockPurchase = true;
                                merchelloServices.ProductVariantService.Save(productVariant);
    

    Hope this helps :)

    //Puck

  • Nick Hoang 51 posts 180 karma points
    Oct 01, 2019 @ 10:43
    Nick Hoang
    0

    Thanks @Puck, save after the addtocataloginventory works for me :)

  • Puck Holshuijsen 184 posts 727 karma points
    Oct 01, 2019 @ 11:09
    Puck Holshuijsen
    0

    Good to hear!

    If you can mark my reply as the solution, others can benefit from this also :)

    //Puck

Please Sign in or register to post replies

Write your reply to:

Draft