4

So when you do a credit memo in Magento, it sets the stock back to the correct level but does not change the "out of stock" back to "in stock" (if applicable). I came across this post by Wright Creatives (http://wrightcreativelabs.com/blog/55-credit-memo-in-stock.html) and it solves this problem. However, the method is too slow! It takes about 30 seconds per product.

I've ultimately had to remove this as a solution (because of the "speed") and now my boss would like the functionality reimplemented.

I know that the is_in_stock data controls this & I'm wondering if there is already a module out there, an article/tutorial, or someone who can help me get started on a "better/faster" solution.

Geoff
  • 3,534
  • 5
  • 30
  • 33

4 Answers4

5

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
    foreach ($creditmemo->getAllItems() as $item) {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);

        if(!$product->isConfigurable()){

            $stockItem = $product->getStockItem();

            //$stockItem->setQty($item->getQty());
            $stockItem->setIsInStock(1);
            $stockItem->save();

            $product->setStockItem($stockItem);
            $product->save();
        }
    }
Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Ovidiu
  • 2,921
  • 5
  • 28
  • 36
  • This seems to work. I'm going to test it a bit more but it looks very promising. Of course, after testing, if it works, I'll accept yours as a nice answer to implement (however I'm going to make this into a simple module for us so we're not overwriting core code. Now, if someone could help figure out the bug that Magento only returns 1 Qty to stock no matter how many products' qty are returning to stock (http://www.magentocommerce.com/boards/viewthread/280342/)... – Geoff Jul 09 '12 at 17:22
  • Before you posted this solution, I had rewrote the solution above to perform in 6-9 seconds (total). I was ready to live with that but this solution you posted is much better. I've written into a simple module (overriding the observer) & now it works like a charm. Just have to figure out that stupid Magento bug (above)... – Geoff Jul 09 '12 at 18:22
  • The other one is a known bug that's no longer present in 1.7.0.1. The main reason I upgraded my shop. Can you post a link with your observer rewrite? tried in a rush but no luck. – Ovidiu Jul 10 '12 at 08:25
  • I'm going to test it heavily and then I'll post the link. Yeah, I put in a bug report a while ago and they told me it was fixed in 1.7.0.1 but we cannot update Magento at this time. I don't understand why they won't release a patch - this seems like a big issue... – Geoff Jul 10 '12 at 19:46
  • Hi designer84, curious if you can share the credit memo "Return to Stock" extension or code that you used to fix this. Would be much appreciated! – Joe Fletcher Jul 26 '12 at 22:21
  • 1
    Sorry, I didn't realize anyone else posted on here so if you still need it, here's the module I made. http://www.graphicsourcecode.com/work/web-design-development/stock-availability/ – Geoff Dec 05 '12 at 00:35
  • I noticed that when creating a Credit Memo for certain online payment methods, the product's `qty` would not be updated. Sometimes the Stock Item that is being used in this solution still contains the "old" `qty`. Of course when saving this stock item, the old `qty` would be set again. To work around this issue I used "hydration" (see: http://stackoverflow.com/a/14366143/278840) to *only* update the `Is in Stock` attribute, and no others, by loading the Stock Item like this: `$stockItem = Mage::getModel('cataloginventory/stock_item')->load($product->getStockItem()->getId(), 'item_id');` – Louis B. May 03 '14 at 10:38
  • The link I posted above will no longer work after 9/24/14. Please use this instead: https://github.com/gsafcik/StockAvailability – Geoff Sep 14 '14 at 06:42
  • I have noticed that the fix posted above leads to an SQL error if a credit memo is created for a product that has been deleted. I have edited the code above and added a check to see if the loaded product has been found before trying to update it: `if($product->getId() && !$product->isConfigurable()){`. – Louis B. Dec 11 '14 at 10:58
0

Write a module that observes the event for credit memos and sets the in_stock flag on the products stock item object before product save. I cant tell you the event you want to observe but I am positive you can find it :)

If there is none, the uglier way would be to observe the products before save. More logic to pull it off but if you always want products to be in stock if they have qty regardless of anything else, then its not a bad idea.

Droydn
  • 1,047
  • 8
  • 13
  • Do you know where the functionality is for "Automatically Return Credit Memo Item to Stock" (in the admin)? `app/code/core/Mage/CatalogInventory/Model/Observer.php` looks promising..??? – Geoff May 07 '12 at 18:40
0

Stores >> Config >> Inventory >> scroll to bottom

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jake S
  • 1
  • 1
0

Go to System -> Configuration -> Inventory (under Catalog) -> Product Stock Options -> Automatically Return Credit Memo Item to Stock and make sure it's set to Yes. Or simply go to your database and in core_config_data where the path is 'cataloginventory/item_options/auto_return' make sure that the value column is set to '1';

Basil
  • 1,613
  • 12
  • 25
Caius36
  • 1
  • 2