Search code examples
c#.netmagento

Cannot update Stock Availability / Is In Stock value via Magento API


Background

I have a live and test Magento store. I generate a MagentoApi C# class from the WSDL of the Magento store.

Problem

I am able to update product quantities with no issues via my API class. I am now trying to set the Stock Availability field from the API but it will not change it's value.

Code

[Test]
public void UpdateIsInStockField()
{
    MagentoStoreConfig storeConfig = GetTestMagentoStore();
    var magentoApiRepo = new MagentoApiRepository(storeConfig);
    catalogInventoryStockItemEntity magentoProduct = magentoApiRepo.GetProductFromSku(new[] { "SKU-123456" });

    var productUpdated = new catalogInventoryStockItemUpdateEntity
                      {
                          is_in_stock = 0,
                          manage_stock = 0,
                          use_config_manage_stock = 0,
                          qty = new Random().Next(50, 100).ToString(CultureInfo.InvariantCulture)
                      };

    magentoApiRepo.UpdateStockQuantity(magentoProduct.product_id, productUpdated);
}

Result

From the Magento store's admin section, the quantity value changes for the product but the Stock Availability value has not changed.

I am setting the manage_stock and use_config_manage_stock as per the instructions outlined here in the Magento API reference guide.


Solution

  • It turns out that I need to specify that I am supplying the is_in_stock field by adding the parameter is_in_stock_specified=true.

    So, my API call is as follows:

    var productUpdated = new catalogInventoryStockItemUpdateEntity
                          {
                              is_in_stock_specified = true,
                              is_in_stock = 0,
                              manage_stock = 0,
                              use_config_manage_stock = 0,
                              qty = new Random().Next(50, 100).ToString(CultureInfo.InvariantCulture)
                          };