3

a have a question to some basic stuff of filtering and limiting product collections. I searched the web for the solution but nothing worked. I hope to get some help here. So, the task is to limit the results with the limit() function. What i want is to set the last product id and to query the next 100 products. The code is called by cron jobs and i need a solution to query every time the next bunch of products. Here my code:

$simpleProducts = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter('type_id', array('eq' => 'simple'))
        ->addAttributeToFilter('sku', array("notnull" => true))
        ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
        ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
        ->addAttributeToSelect('entity_id')
        ->addAttributeToSelect('search_sku_textfield')
        ->addAttributeToSelect('sku')
        ->limit(100,$lastProdId);

I also have on some products an attribute set (with the attribute set). It would be nice to query just the products which have the attribute (assigned truh the attribute set). This doesn't work

         ->addFieldToFilter('search_sku_textfield', array("null" => true))
1
  • what about filtering by attribute_set_id? Also you should be careful with using a ID in limit, as delete a product would have unintended consequences. Instead you should ->limit(100)->where( entity_id > $lastProdId) or something similar. An order('entity_id') call should also be done to be sure about order.
    – Flyingmana
    Commented Feb 28, 2014 at 1:18

2 Answers 2

7

What about something like this:

    Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter('type_id', array('eq' => 'simple'))
        ->addAttributeToFilter('sku', array("notnull" => true))
        ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
        ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
        //->addAttributeToSelect('entity_id')   // you don't need this
        ->addAttributeToSelect('search_sku_textfield')
        //->addAttributeToSelect('sku')         // static attributes are always loaded
        ->addFieldToFilter('entity_id', array('gt' => $productId))
        ->addFieldToFilter('attribute_set_id', $attributeSetId)
        ->setOrder('entity_id')
        ->setPageSize(100);
2
  • hey Enrique, can i pass an array of attributeSetIds?
    – Florin P.
    Commented Mar 3, 2014 at 14:48
  • yes of course, it's just a filter like any other, just use ->addFieldToFilter('attribute_set_id', array('in' => $attributeSetIds))
    – Enrique
    Commented Mar 3, 2014 at 16:07
0

Hey I think you need something like this. Jus find the products which have id greater than the current provided id and set page size to 100 it will return next 100 records from the current one.

Remember to set the product order also.

$_product->addFieldToFilter('entity_id',array('gt' => $productId))
->setOrder('entity_id')
->setPageSize(100);
1
  • Thanks a lot guys, i will try your solutions and come back with more info. Greetz
    – Florin P.
    Commented Mar 3, 2014 at 14:46

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.