3

I need some help.

I have added a custom attribute to the cataloginventory/stockitem table and now I am trying to observe the cataloginventory_stock_item_save_before event to set my custom attribute before saving.

<global>
    <events>
        <cataloginventory_stock_item_save_before>
            <observers>
                <mypackage_mymodule_stocknote>
                    <type>singleton</type>
                    <class>Mypackage_Mymodule_Model_Observer</class>
                    <method>stocknote</method>
                </mypackage_mymodule_stocknote>
            </observers>
        </cataloginventory_stock_item_save_before>
    </events>
</global>

This should be triggered every time a stock_item model is saved, thus when an order is placed or when a product saved, right? I am testing the observer with a print and die command, but it doesn't look like my observer is executed. To add to the confusion, it works fine, with the same configuration, if I call cataloginventoriy_stock_item_save_after event instead.

Anyone got a hint for me what I am doing wrong? I need to set some custom attribute in the stock_item before it is saved, alternate approaches also welcome.

2

Unfortunately this event does not exist (any longer - it existed in a very old version of Magento). If you look into the class Mage_CatalogInventory_Model_Stock_Item

you have this method

/**
 * Before save prepare process
 *
 * @return Mage_CatalogInventory_Model_Stock_Item
 */
protected function _beforeSave()
{
    // see if quantity is defined for this item type
    $typeId = $this->getTypeId();
    if ($productTypeId = $this->getProductTypeId()) {
        $typeId = $productTypeId;
    }

    $isQty = Mage::helper('catalogInventory')->isQty($typeId);

    if ($isQty) {
        if (!$this->verifyStock()) {
            $this->setIsInStock(false)
                ->setStockStatusChangedAutomaticallyFlag(true);
        }

        // if qty is below notify qty, update the low stock date to today date otherwise set null
        $this->setLowStockDate(null);
        if ($this->verifyNotification()) {
            $this->setLowStockDate(Mage::app()->getLocale()->date(null, null, null, false)
                ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT)
            );
        }

        $this->setStockStatusChangedAutomatically(0);
        if ($this->hasStockStatusChangedAutomaticallyFlag()) {
            $this->setStockStatusChangedAutomatically((int)$this->getStockStatusChangedAutomaticallyFlag());
        }
    } else {
        $this->setQty(0);
    }

    return $this;
}

it does not call the parent::_beforeSave() method which would normally dispatch the cataloginventory_stock_item_save_before event and also does not dispatch the event in the method.

  • Hmm...is this a bug or a feature? – Marius Jul 17 '14 at 12:01
  • github.com/OpenMage/magento-mirror/blob/magento-1.3/app/code/… - I did report it as a bug a long time ago but unfortunately it didn't go very far – Kristof at Fooman Jul 17 '14 at 12:07
  • That explains a lot. Thank you. I'm gonna use a separate table to store my information then and use the _after event, looks like the easiest altenative. – user3154108 Jul 17 '14 at 12:22
  • Even cataloginventory_stock_item_save_after does not trigger always when a frontend order is placed (it triggers when status changes such as inStock status changes but not with normal qty decrements). I had to follow instructions from this answer magento.stackexchange.com/questions/25207/… – doc_id Jul 29 '15 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.