2

There is an event that fires after a product import (And we run this update on the product import). Is it possible to make this save without extra events firing?

/*UPDATE UNIT COST*/
                    if ($unit_cost > 0) {
                        try {
                            $product->setData('cost', $unit_cost);
                            $product->save();
                            Mage::log("SKU {$sku} cost was updated to {$unit_cost}", null, 'custom.log');
                        }
                        catch (Exception $e) {
                            Mage::logException($e);
                        }
                    }
1

3 Answers 3

3

You could try updating your code to use the saveAttribute function:

$product->setData('cost', $unit_cost);
$product->getResource()->saveAttribute($product, 'cost');

This should not fire any of the normal events fired when saving a product and should also speed up your code.

For EAV models the function saveAttribute will do exactly what it name suggests and save a single attribute without you having to call save on the model. It will work out what attribute and tables your are working with and in the end run either:

$adapter->insertOnDuplicate($table, $data, array('value'));

OR

$adapter->delete($table, array('value_id IN (?)' => $valueIds));
3
  • But I will still have to call $product->save() correct? Commented Apr 24, 2014 at 20:06
  • No this will just updated the single attribute for that model without having to call save. Commented Apr 24, 2014 at 20:11
  • Yes Thank you David - This work magically Commented Apr 25, 2014 at 1:46
0

If you are saving a model in magento, you will fire many events, no way to turn them off, unless you modify core code.

0

You may be able to disable the event by using the event type disabled. For example:

    <events>
        <product_save>
            <observers>
                <reports>
                    <type>disabled</type>
                </reports>
            </observers>
        </product_save>
    </events>

Reference: http://www.atwix.com/magento/observers-reality-or-fiction/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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