0

I have 2 observers, catalog_product_prepare_save and catalog_product_save_after that work great when I save a product from admin or when a product is saved through a sale of the product. However they're not begin called from a script I wrote to update some data.

Script not calling observer:

$_product = Mage::getModel('catalog/product')->load($ProductID);
$_stockItem = $_product->getStockItem();

$_stockItem->setData('manage_stock', 1);
$_stockItem->setData('is_in_stock', 0);
$_stockItem->save();

$_product->setData($Attribute, $NewValue);
$_product->save();

My Observer xml file:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Vampower_MyObserver>
            <version>0.0.1</version>
        </Vampower_MyObserver>
    </modules>

    <global>
        <models>
            <Vampower_MyObserver>
                <class>Vampower_MyObserver_Model</class>
            </Vampower_MyObserver>
        </models>

        <events>
            <catalog_product_prepare_save>
                <observers>
                    <Vampower_MyObserver>
                        <class>Vampower_MyObserver/observer</class>
                        <method>UpdateInfo</method>
                        <type>singleton</type>
                    </Vampower_MyObserver>
                </observers>
            </catalog_product_prepare_save>

            <catalog_product_save_after>
                <observers>
                    <Vampower_MyObserver>
                        <class>Vampower_MyObserver/observer</class>
                        <method>qtyUpdate</method>
                        <type>singleton</type>
                    </Vampower_MyObserver>
                </observers>
            </catalog_product_save_after>
        </events>
    </global>
</config>
  • 1
    In this case, you can try dispatch event – Ankur140290 Jun 25 '17 at 15:24
1

Solved the problem by switching the observer to catalog_product_save_before. Did not need to make any other changes to the code, everything worked perfectly.

  • You can also accept your own answer to mark to question as solved :) – sv3n Jul 3 '17 at 22:54
0

in your root script add this

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

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.