The problem
I have added a custom attribute to the sales_flat_order
table like:
/* @var $installer Mage_Sales_Model_Mysql4_Setup */
$installer = new Mage_Sales_Model_Mysql4_Setup('setup');
$installer->addAttribute(
Mage_Sales_Model_Order::ENTITY,
'new_attribute_code',
array(
'type' => 'varchar',
'visible' => true,
)
);
I would like to programmatically do a bulk update of orders to set a new value for this attribute against many orders. This value will be scripted to update based on the result of another system, it's not a "one off" or solved by having a default value.
I hoped Magento would have a similar kind of interface as it does for products which allows for bulk updates like
Mage::getSingleton('catalog/product_action')->updateAttributes(
$productIdsArray,
array('description' => 'New desc'),
Mage_Core_Model_App::ADMIN_STORE_ID
);
However, I can find no such interface. Does anyone know an efficient and safe way to bulk update orders?
Research
Product bulk update
The product bulk update seems to boil down to Mage_Catalog_Model_Resource_Abstract::_saveAttributeValue
. This won't help me for Mage_Sales
entities. I could however use it as inspiration for a direct sql level access. However this is something I would hope Magento handles.
Order bulk cancel
I know that you can mark many orders as "cancelled" through the mass action in the magento admin panel. However under the hood it is in fact doing
public function massCancelAction()
{
$orderIds = $this->getRequest()->getPost('order_ids', array());
foreach ($orderIds as $orderId) {
$order = Mage::getModel('sales/order')->load($orderId);
if ($order->canCancel()) {
$order->cancel()
->save();
}
}
That is pretty grim. It's not even loading all the orders in a collection before trying to cancel. It's calling load
within a for loop. Ew.
Does anyone know of anything clever I can be doing here?
Responses
@Raphael thanks for your answer. Popping this in my entities' resource model seemed to work okay.
private function getOrderTable()
{
return $this->getTable('sales/order');
}
public function updateOrders(array $orderIds, $value)
{
if (empty($orderIds) || !is_string($value)) {
return $this;
}
$adapter = $this->_getWriteAdapter();
try {
$adapter->beginTransaction();
$data = array(
'new_attribute_code' => $value,
);
$condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $orderIds);
$this->_getWriteAdapter()->update($this->getOrderTable(), $data, $condition);
$adapter->commit();
} catch (Exception $e) {
$adapter->rollBack();
throw $e;
}
return $this;
}