1

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.

1 Answer 1

1

Calling load and save on products is kind of expensive specially if used in loops.

I think you can try a different approach...the other way around. I mean, since you only need to work on one category, why not add products to it like you did it from the admin panel when editing a category?. Right now you are doing it like you save each product that you want to remove and that you want to add to the category.

You can even do it from one step, without having to remove everything from that category first then add everything else.

Here goes.

$categoryId = 8;
//get the category
$category = Mage::getModel('catalog/category')->load($categoryId);
$newProducts = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter('neuheiten', array('neq' => '') )
        ->addAttributeToSort('neuheiten', 'desc')
        ->addStoreFilter()
        ->setPage(0, 100);
//build an array with all the products you need.
$productsToAdd = array();
$index = 0;
foreach ($newProducts as $product) {
    $productsToAdd[$product->getId()] = $index; //$index is the position in the category
    $index ++;
}
//set the new products to the category
$category->setPostedProducts($productsToAdd);
//save the category
$category->save();

You don't need to worry about deleting the old products from the category. It will be done automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.