36

This works for me: $item = ['qty' => 11]; // For example $product->setStockData(['qty' => $item['qty'], 'is_in_stock' => $item['qty'] > 0]); $product->save(); Edit: This is no longer the correct way to handle this, because $product->save() is deprecated since Magento 2.1. The correct way to do this is by using the ...


28

It has been some time ago that I build something for this, I had to listen to multiple observers because they where not all handled by the save of the cataloginventory, I have to following code: <events> <cataloginventory_stock_item_save_commit_after> <observers> <genmato_stockupdate> ...


22

You need elevated permissions to use the CREATE ALGORITHM bit This can and will stop an import in it's tracks. Instead use this statement to create the view CREATE OR REPLACE VIEW `inventory_stock_1` AS select distinct `legacy_stock_status`.`product_id` AS `product_id`, `legacy_stock_status`.`website_id` AS `website_id`, `legacy_stock_status`.`...


20

Check your value for use_config_manage_stock, it could be defaulting to that default value, so you'll want to set use_config_manage_stock to 0 as well as manage_stock to 0 too, depending on your settings. $_product->setStockData(array( 'use_config_manage_stock' => 0, 'is_in_stock' => 1, 'qty' => 9999, ...


20

If you are using the @giel-berkers solution, you may need to set isInStock also, because it will not be automatically sets. So, the following code was useful for me: public function __construct( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry ) { $this->stockRegistry = $stockRegistry; parent::__construct(); } public ...


14

This is actually a feature of Magento. Setting Display out of stock products to No only hides them from the category list pages and the search results page. The product page is actually still available. Starting with CE 1.9.1 there is even a message under the config setting Display out of stock products saying Products will still be shown by direct product ...


14

I got the solution for this, you need to create a view table inventory_stock_1 for your catalog in database. Here is the command you need to run in your DB SQL. Make sure to update 3 things in below query dbusername, localhost, dbname. CREATE ALGORITHM=UNDEFINED DEFINER=`dbusername`@`localhost` SQL SECURITY INVOKER VIEW `inventory_stock_1` AS select ...


12

Warning, using setSkipSaleableCheck(true) appears to have an additional effect of showing disabled products in the dropdown. The function which is affected by this setting is in Mage_Catalog_Block_Product_View_Type_Configurable: public function getAllowProducts() { if (!$this->hasAllowProducts()) { $products = array(); $...


9

The "code" solution: Create your own module, and in the config.xml file of the module add these 2 events inside the <frontend> tag: <events> <controller_action_layout_render_before_catalog_product_view> <observers> <[namespace]_[module]> <class>[module]/...


9

There are at least two ways to implement a multi warehouse in Magento; we discussed about this topic during a Magento U course. The first is that of exploiting the fact that Magento is virtually ready to use multiple inventories as you higlighted. This is the most complex way because you should implement indexing logic. The second way is that of ...


8

I struggled with this same issue. While debugging I found that the product data has a quantity_and_stock_status array, so I tried setting it with: $product->setQuantityAndStockStatus(['qty' => $quantity, 'is_in_stock' => 1]); and it started working for me. I still am setting the $product->setStockData too if you edit a product and inspect element ...


8

One thing the other answers missed is that if you setQty($qty), it will apply the exact value you provide. But if a sale was made for that product a moment before your save, the original qty could have changed. So what you really want to do is tell Magento the difference you want to apply to the qty. Fortunately, Magento 2 provides a nice mechanism for this....


8

You can try the following code public function __construct( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry ) { $this->stockRegistry = $stockRegistry; } // you can pass $product object to the function public function getStockStatus($product) { return $this->stockRegistry->getStockItem($product->getId()); } You ...


8

If you are using the only simple products in your system then using the Stock Status field check then Product status is Ok. But when you have a configurable or bundle or group product then it is not a good idea to use the Stock Status field to check stock status. Because of these types of product's stock status, it depends on its child Products status. That ...


7

Having a look at this table's indexes it does seem to function completely as a standalone addition to the database, for the time being. If you look at the assignProduct() method around line 263 of app/code/core/Mage/CatalogInventory/Model/Stock/Item.php you'll notice that it references the method itself: Mage::getSingleton('cataloginventory/stock_status')...


7

It looks to me like qty in cataloginventory_stock_item is the source of truth, and qty in cataloginventory_stock_status is the cached/indexed value. Rebuilding the Stock Status index appears to always copy the value from cataloginventory_stock_item into cataloginventory_stock_status. If there are multiple websites, there will also be a record for each ...


7

Check below settings. Admin > Store > Configuration > Catalog > Inventory > Stock option > Decrease Stock When Order is Placed Set it to yes. If you have a multi-website or multi-store then change the same setting for all store.


7

Check inventory_reservation: https://github.com/magento-engcom/msi/wiki/Salable-Quantity-Calculation-and-Mechanism-of-Reservations If you don't wan't to use MSI (and stick to CatalogInventory_* from 2.2.7)- turn the Inventory_* modules off: https://devdocs.magento.com/guides/v2.3/comp-mgr/install-extensions/inventory-management-installation.html#disable-...


6

If you're not deep into MySQL queries or if you don't have a direct access to database, you can do the same in the Magento back-office. Go to Catalog -> Manage product, in the product table header on the left click on "select all" and then in the same bar at right "updated attribute" and "submit". You can do that operation filtering only the product ...


6

If possible go with Magento's native API (with extensive documentation). This won't be able to push but you can pull data out. Make sure you don't request the API to much as this will slow down the Magento installation. For example; updating products will set off the flat product data indexers which can be pretty heavy on the system. If the API is too ...


6

check your stock setting in admin panel Advance Inventory->manage stock set to yes if you want to reduce quantity


6

This is a bug affecting configurable products introduced by the new Magento_InventoryConfigurableProductFrontendUi module in Magento 2.4.2. You can read more about the issue here: https://github.com/magento/inventory/issues/3276 As a temporary solution you can disable the module which will remove the message. bin/magento module:disable ...


5

A general answer Observers are executed by area first, then by module loading order That means, all observers registered in <global> are executed before all observers registered in <frontend> or <adminhtml>. Within an area, observers are executed in the order they appear in the merged config XML tree, which means technically in the order ...


5

I would not delete the order but just cancel them and with a cronjob (either a script or Magento extension) you can do this. Run it daily to clean up the orders. Results are logged to var/logs/cancelorders.log for reference. $collection = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('updated_at', Mage::getModel('core/date')-&...


5

The information about the return to stock is not kept in the database. When a credit memo is created the method Mage_CatalogInventory_Model_Observer::refundOrderInventory() is called. In there an array is built with the qtys that need to return to stock based on the fact that the creditmemo item was previously marked with setBackToStock(true) or if the ...


5

You cannot use any event related to the stock item model, because Magento uses an optimized SQL query to decrease stock for all ordered items at once, bypassing the model. I solved this with a rewrite of Mage_CatalogInventory_Model_Stock where I added an additional event: <?php /** * Add events to observe stock qty change * * @author Fabian ...


5

The inventory is kept in the table cataloginventory_stock_item. The column you are looking for is qty. Run this query: UPDATE `cataloginventory_stock_item` SET `qty` = 1; But make sure you backup first and rebuild the indexes when you are done.


4

The suggestion by @Marius looks like a good suggestion especially with your scenario but with our scenario it was bit different than yours. We are selling cups with different colours and sizes where customers can choose size from S, M, L, XL and 36 different colours but our base size "cups" has one inventory. So when Small size cup get sold in any of the 36 ...


4

Here are my 2 cents. Since A & B have the same SKU and should share the same inventory, why not make it a single product? This way the stock is reduced correctly when you buy X meters. If the price for product A is not 1000 * price of B then you can easily manage this with tire prices. Create a tire price where customer group is ALL GROUPS and Qty is ...


4

I found this: "I know it's old but because this isn't yet fixed not even in 1.7.0.1 I came up with a better solution. Tested on 1.5.1 and above: \app\code\core\Mage\CatalogInventory\Model\Observer.php in public function refundOrderInventory($observer)" after Mage::getSingleton('cataloginventory/stock')->revertProductsSale($items); //add this ...


Only top voted, non community-wiki answers of a minimum length are eligible