Just as improvement for answer above ... for better performance please avoid calling save
, load
or delete
inside a loop.
In this case you can just collect product IDs and useMage::getSingleton('catalog/product_action')->updateAttributes()
as written in your question.
public function updateProductAttribute($observer)
{
$orderIds = $observer->getEvent()->getOrderIds();
if (empty($orderIds) || !is_array($orderIds)) {
return;
}
$productIds = array();
foreach ($orderIds as $orderId) {
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllVisibleItems();
foreach ($items as $i) {
$productIds[] = $i->getProduct()->getId();
}
}
Mage::getSingleton('catalog/product_action')->updateAttributes(
$productIds,
array('cart_status' => 0),
$storeId = 0
);
}
Maybe to save you some time ... you're dealing with a lot of events/observer at the moment, what is the prefered way instead of rewriting classes. So this may help you ...
1.) find available events (just do it on test environment!)
Open app/Mage.php
and add one line to dispatchEvent()
method
public static function dispatchEvent($name, array $data = array())
{
Mage::log($name, null, $name . '.log', true);
...
}
To get the data from this event you can also do this here:
public static function dispatchEvent($name, array $data = array())
{
if ($name == 'event_you_want_to_log') {
Mage::log(array($name, $data), null, $name . '.log', true);
}
...
}
Now, do the action you want to observe and comment Mage::log
again.
2.) get data from observer
In this case you'll get something like this:
2017-06-15T07:34:22+00:00 DEBUG (7): Varien_Event_Observer Object
(
[_data:protected] => Array
(
[event] => Varien_Event Object
(
...
[_data:protected] => Array
(
[order_ids] => Array
(
[0] => 15
)
[name] => checkout_onepage_controller_success_action
)
...
)
[order_ids] => Array
(
[0] => 15
)
)
...
)
You can access data with either $observer->getEvent()->getOrderIds()
or $observer->getOrderIds()
.