Reputation: 3726
I'm having a weird issue with bulk-updating products in Magento programmatically.
I have a script which gets a category model, then a product collection of products in that category filtered by a manufacturer (so I'm updating all products by a manufacturer in a given category). I then traverse the products and update an attribute (a sizing table), and save the product.
Here's the function which does this:
<?php
function updateManufacturerSizingTable($store, $categoryId, $manufacturerId, $tableHtml) {
$categoryObject = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($categoryObject)
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', $manufacturerId)
->setStore($store)
->addStoreFilter($store)
->setOrder('name')
->load();
foreach ($collection as $product) {
$data = $product->getData();
echo '--> Updating ' . $data['name']. PHP_EOL;
$product->setData('sizing', $tableHtml);
$product->save();
}
}
?>
And it works fine, but it's having weird side effects on the Magento categories (the ones involved in the update script) on the front end. Seemingly arbitrarily, products will disappear from some (not all) of the categories they belong to. For example, a product will belong to Category A and Category B, where B is a child of A, and will appear in A but not B after the mass-update of the sizing attribute.
I think this is a problem to do with indexing. I've tried doing a command line reindex with
php path/to/mage/shell/indexer.php reindexall
And it seems to execute fine, and updates the timestamps in the index_process table. However, all the indexes show as 'Pending' and the .lock files exist in var/locks, and there have been no entries to index_event reporting anything about the reindex.
I've added code to the bulk-update script which turns indexes to manual before updating, doing the update, performing a reindexAll and turning the indexes back to real time. It worked (as in, no errors reported) but the category listings are still broken/weird on the frontend.
<?php
/* Set indexing to manual for the updating */
echo '== Disabling indexing before the import ==' . PHP_EOL;
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
echo '== Beginning update ==' . PHP_EOL;
/* In a loop call the updateManufacturerSizingTable function */
echo '== Reindexing and re-enabling indexing ==' . PHP_EOL;
/* Set indexing back to on-save and reindex */
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
?>
And just as I've finished writing this post and refreshed a category page which previously had no products showing (due to this issue), the products have returned. There are no new entries in index_event and index_process still reports the same timestamps that were set when the command line reindexall was run.
Is it possible that the reindexing isn't actually being run in the reindexall process and that it might be executing elsewhere? That might explain why it's progressively restoring the broken categories after the script has run (quite a while after, we're talking almost an hour), but Magento isn't reporting anywhere that the indexes are processing?
Upvotes: 2
Views: 1924
Reputation: 17656
You need to set the store id to Admin when updating products, try adding
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
Upvotes: 1