Biblia para consultas de collections no magento
Magento
There are two basic ways of loading your product collection. Either call method getCollection() on your product model instance or load collection class in the factory method ‘getResourceModel’.
$collection = Mage::getResourceModel('catalog/product_collection');
The product model is an enormous data set, and the more attributes we include in the load the longer it will take. By default, Magento will only load the basic data found in the ‘catalog_product_entity’ table (ID, SKU, Entity Type ID etc…)
ADD ALL PRODUCT ATTRIBUTES
$collection->getAttributeToSelect('*');
ADD INDIVIDUAL PRODUCT ATTRIBUTES
$collection->addAttributeToSelect('name', 'description', 'brand');
ADD SPECIFIC PRICE ATTRIBUTES
$collection->addMinimalPrice();
$collection->addFinalPrice();
$collection->addTaxPercents();
ADD CATEGORY IDS TO PRODUCTS
$collection->addCategoryIds();
ADD TIER PRICING INFO
$collection->addTierPriceData();
ADD PRODUCT URL REWRITES
$collection->addUrlRewrite();
#FILTER COLLECTION
Being an EAV entity type means that we have access to the wonderful ‘addAttributeToFilter()’ method in addition to the standard ‘addFieldToFilter()’, which can take a larger range of arguments. The following are basic conditional filters, like you’d use in a MySQL query:
IS EQUAL TO
$collection->addAttributeToFilter('status', array('eq' => 1));
IS NOT EQUAL TO
$collection->addAttributeToFilter('visibility', array('neq' => 1));
GREATER THAN
$collection->addAttributeToFilter('price', array('gt' => 3));
LESS THAN
$collection->addAttributeToFilter('price', array('lt' => 3));
GREATER THAN OR EQUAL TO
$collection->addAttributeToFilter('price', array('gteq' => 4));
LESS THAN OR EQUAL TO
$collection->addAttributeToFilter('price', array('lteq' => 4));
CONTAINS – WITH % WILDCARDS
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));
DOES NOT CONTAIN – WITH % WILDCARDS
$collection->addAttributeToFilter('sku', array('nlike' => 'ABC%'));
IN ARRAY
$collection->addAttributeToFilter('entity_id', array('in' => array(1,3,12)));
NOT IN ARRAY
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,12)));
IS NULL
$collection->addAttributeToFilter('description', 'null');
IS NOT NULL
$collection->addAttributeToFilter('description', 'notnull');
#FURTHER FILTERING There are also more general filters that can be applied:
FILTER BY PRODUCT IDS
$collection->addIdFilter(array(4,5,6));
FILTER BY CURRENT STORE
$collection->addStoreFilter();
FILTER BY CURRENT WEBSITE
$collection->addWebsiteFilter();
FILTER BY CATEGORY
$collection->setStoreId($id)->addCategoryFilter($category)
Again, as with MySQL you can sort your collection by a chosen attribute in either ascending or descending order.
ORDER BY ATTRIBUTE ASCENDING
$collection->setOrder('price', 'ASC');
ORDER BY ATTRIBUTE DESCENDING
$collection->setOrder('name', 'DESC');
RANDOM ORDER
$collection->setOrder('rand()');
#LIMIT COLLECTION If you’re in a situation where you want your product collection, but want to limit the number of results to a certain amount you can use the ‘setPageSize()’ method, and simply pass in your limit.
$collection->setPageSize(10);
Always useful when checking if any results have been returned.
$collection->count();
#RETURN ONLY PRODUCT IDS Sometimes you’ll just need to get product IDs from your collection, rather than the rest of the product data. By Using ‘getAllIds’ you will receive a nice little array of product IDs for you to do with as you wish!
$collection->getAllIds();
#DEBUG COLLECTION Want to know why your collection keeps returning results? Using the ‘getSelect()’ method on your collection you can print the MySQL query that is getting sent to the database.
$collection->getSelect();
#ADD FILTER IN STOCK
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
#ADD FILTER IS ACTIVE
Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
#ADD FILTER IS VISIBLE
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);