Magento – How to Limit Product Collection

product-collectionresource-model

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))

Best Answer

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);
Related Topic