Back again. I'm almost there with my observer to update "Manage Stock" on child items when the parent configurable item is changed, but I've run into a memory problem. First, I tried updating all the children when the memory limit was set to 64MB, then raised it to 256MB and finally to 512MB...each time, I hit the limit when executing the code. As a test, I made it to where the script would exit after updating just one child product, but it still hit the limit. This leads me to believe that I'm doing something wrong, as I'm pretty sure Magento wouldn't need more than half a gig of memory allocated to update two fields of one child. My observer is as follows:
<?php
class MyCompany_Catalog_Model_Observer
{
public function mc_update_child (Varien_Event_Observer $observer)
{
if (Mage::app()->getRequest()->getPost('mc_update_child_products'))
{
$productid = Mage::registry('current_product')->getId();
$product = Mage::getModel('catalog/product')->load($productid);
$productType = $product->getTypeID();
if ($productType === 'configurable')
{
$parent = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productid);
$parentMS = $parent->getData('manage_stock');
$parentUCMS = $parent->getData('use_config_manage_stock');
$children = $product->getTypeInstance()->getUsedProducts();
foreach ($children as $child)
{
$childId = $child->getId();
$childProduct = Mage::getModel('catalog/product')->load($childId);
$childStock = $childProduct->getStockItem();
$childStock->setData('manage_stock', $parentMS);
$childStock->setData('use_config_manage_stock', $parentUCMS);
$childStock->save();
$childProduct->save();
Mage::log($childId);
exit;
}
}
}
}
}
As you can see, I was hoping to log the id of the child product that got updated, and exit, but no log entry is made, and I get this in the Apache error log:
[Wed Mar 19 16:45:03 2014] [error] [client 192.168.1.120] PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 32 bytes) in /var/www/magento-www/docs/lib/Zend/Db/Statement/Pdo.php on line 290, referer: https://www.mycompany.com.local/index.php/admin/catalog_product/edit/id/9407/back/edit/tab/product_info_tabs_inventory/key/d32f1b287f7eff4f68951efbd09418265067fabe9adb78541b070adb748dbfd6/
Hopefully someone can see a mistake I'm making. TIA.
memory_get_usage()
at each of those steps to see at which point your memory explodes.$childProduct->save()
. And since the product in your registry hasn't changed, this function will be run again. You would get the same childId and you'd have an infinite loop.