Blog

30
Dec

Magento Credit Memo “Return to Stock” not updating Stock Availability

When issuing a credit memo in Magento 1.4.1.1, the Stock Availability is not updated to “In Stock” when the “Return to Stock” checkbox is checked.

This an inconvenience for store owners, as they have to take the additional step of going into the Product Manager and updating the Stock Availability there (on the Inventory tab) in order to have their inventory accurately reflected in the online store.

I set about trying to find a way to force the Stock Availability to be “In Stock” when the Credit Memo is saved.

Here are the steps to recreate the issue:

  1. On the frontend of your store, purchase a product that has a Qty of 1 in stock.
  2. On the backend of your store, go to Sales > Orders.
  3. Open the recently completed order.
  4. Create and Save an Invoice for the order.
  5. Open the Invoice.
  6. Click the Credit Memo button.
  7. Verify that the Return to Stock checkbox is checked.
  8. Submit the credit memo to process the refund and return the ordered item to stock.
  9. Now go lookup the product in the Product Manager.  Go to the Inventory tab.  Qty is back to 1, but the Stock Availability is “Out of Stock”.
  10. Also, the product is not visible on the front end.

The Solution

This required overriding a core file in order to add some extra code to update the Stock Availability in the database and refresh some of the indices so the product immediately shows on the frontend of the site.

  1. Copy app/code/core/Mage/Sales/Model/Order/Creditmemo.php to app/code/local/Mage/Sales/Model/Order/Creditmemo.php.  Create the directories in the local folder if they do not yet exist.
  2. Open the Creditmemo.php file in the local directory.
  3. Find the _aftersave() function around line 718. Below $item->save(); add the following lines:
    /* Set Stock Availability to In Stock when credit memo is issued for item */
    if ($item->getQty() == 0) {
        // Mage::log("-----Resetting Inventory via DB------");
    
        $orderItem = $item->getOrderItem();
    
        $productId = $orderItem->etProductId();
        //Mage::log("**** Product Id = " . $productId);
    
        // Open database connection
        $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
        // Run update query
        $sql = "update cataloginventory_stock_item set is_in_stock=1
                where product_id=".$productId;
        $write->query($sql);
    
        // Refresh indices required to have product display on frontend
        // Mage::log("Started Rebuilding Index At: " . date("d/m/y h:i:s"));
        // Stock status index
        $process = Mage::getModel('index/process')->load(9)->reindexEverything();
    
        // Product Attributes index
        $process = Mage::getModel('index/process')->load(1)->reindexEverything();
    
        // Product Prices index
        $process = Mage::getModel('index/process')->load(2)->reindexEverything();
    
        // Product Flat data
        $process = Mage::getModel('index/process')->load(4)->reindexEverything();
        //Mage::log("Finished Rebuilding Index At: " . date("d/m/y h:i:s"));
        //Mage::log("Done!");
        //Mage::log("*****");
    }
    
  4. Note that there are several debugging lines commented out in the code above.  You can uncomment these to save log messages to var/log/system.log.

Be Sociable, Share!