Slow and memory problems: Refresh category with 100 products
-
30-10-2019 - |
Question
I have a category with 100 new products. After an import I remove all products from this category and add 100 new products. This is very slow and cause memory problems, so I am doing something wrong. Here is the code:
public function removeAllFromNew()
{
$category_id = 8;
$products = Mage::getModel('catalog/product')->getCollection()
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
foreach($products as $product)
{
$categories = $product->getCategoryIds();
if(in_array($category_id, $categories))
{
$product_load = Mage::getSingleton('catalog/product')->load($product->getId());
$categories = array_diff($categories, array($category_id));
$product_load->setCategoryIds( $categories );
$product_load->save();
}
}
}
public function updateNew()
{
self::removeAllFromNew();
$category_id = 8;
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('neuheiten', array('neq' => '') )
->addAttributeToSort('neuheiten', 'desc')
->addStoreFilter()
->setPage(0, 100);
foreach($products as $product)
{
$categories = $product->getCategoryIds();
$categories[] = $category_id;
$product_load = Mage::getSingleton('catalog/product')->load($product->getId());
$product_load->setCategoryIds( $categories );
$product_load->save();
}
}
How can I do this any better?
I think at first I should not query all products in removeAllFromNew()
. How can I limit this to products in a category?
Update I added a addCategoryFilter
in removeAllFromNew()
but it looks like adding a product to the category is the slow part.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange