سؤال

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))
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top