Manipulate collections of products in Magento
Retrieve a collection of products
1 2 3 |
$Products = Mage :: getResourceModel ('catalog/product_collection'); / * Or * / $Products = Mage :: getModel ('catalog/product')->GetCollection(); |
Selecting a specific attribute
1 2 3 |
$Products = Mage::getResourceModel('catalog/product_collection') ->AddAttributeToSelect('name') ->AddAttributeToSelect('weight'); |
Selecting all attributes
1 2 |
$Products = Mage::getResourceModel ('catalog / product_collection') -> AddAttributeToSelect ('*'); |
Filters on product attributes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$Products = Mage::getResourceModel ('catalog / product_collection') / * Products activated */ -> AddAttributeToFilter ('status', 1) / * Products whose identifiers are X, Y * / -> AddAttributeToFilter ('entity_id', array ('in' => array (2,4))) / * Products whose identifiers are not X, Y * / -> AddAttributeToFilter ('entity_id', array ('nin' => array (1,3))) / * Product prices between X and Y * / -> AddAttributeToFilter ('price', array ('from' => 10, 'to' => 100)) / * Product weight from X * / -> AddAttributeToFilter ('weight', array ('from' => 100)) / * Special Price (promotion) is not equal to 0 * / -> AddAttributeToFilter ('special_price', array ('neq' => 0)) / * The reference contains * / -> AddAttributeToFilter ('sku', array ('like' => '% P%')) / * The reference does not contain * / -> AddAttributeToFilter ('sku', array ('Nlike' => '% A%')) / * Products added between dd / mm / yyyy and dd / mm / yyyy * / -> AddAttributeToFilter ('created_at', array ('from' => '2009-01-01 ',' to '=> '2011-01-01')) / * Visible products in the catalog and search * / -> AddAttributeToFilter ('visibility', array ('in' => Mage_Catalog_Model_Product_Visibility :: VISIBILITY_BOTH)); |
Filters on product categories
1 2 3 4 5 |
$Category = Mage::getModel ('catalog/category')->load (1); $Products = Mage::getResourceModel ('catalog/product_collection') ->AddAttributeToSelect('name') ->AddCategoryFilter($category); |
Limit the number of results
1 2 3 |
$Products = Mage::getResourceModel ('catalog/product_collection') -> AddAttributeToSelect('name') -> SetPageSize(10); |
Sort results
/ * Sort on a specific attribute * /
1 2 3 |
$Products = Mage::getResourceModel ('catalog/product_collection') -> AddAttributeToSelect ('name') -> AddAttributeToSort ('price', 'asc'); |
/ * Sort randomly (Rand) * /
1 2 3 4 |
$Products = Mage::getResourceModel('catalog/product_collection') ->AddAttributeToSelect('name'); $Products->getSelect()->order(new Zend_Db_Expr ('RAND()')); |
Other useful methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$Products = Mage::getResourceModel('catalog/product_collection') / * Products whose identifiers are X, Y * / -> AddIdFilter(array (2,4)) / * Products store during consultation * / -> AddStoreFilter() / * Store products whose identifier is 1 * / -> AddStoreFilter(1) / * Select the final price calculated * / -> AddFinalPrice() / * Select the lowest prices (with product options) * / -> AddMinimalPrice(); |
Collection of products with statistics (Reports)
1 2 3 4 5 6 7 8 9 10 11 12 |
$Products = Mage::getResourceModel ('reports/product_collection') / * Total product sales * / -> AddOrderedQty () / * Total baskets including product * / -> AddCartsCount () / * Total bellies between dd / mm / yyyy and dd / mm / yyyy * / -> AddOrdersCount ('2009-01-01', '2011-01-01') / * Total Product views between dd / mm / yyyy and dd / mm / yyyy * / -> AddViewsCount ('2009-01-01', '2011-01-01'); |
List of products offers
1 2 3 4 5 6 7 8 9 10 11 |
$Now = Mage::getModel ('core/Date')->timestamp(time()); $Date = date('Ymd h: i: s', $now); $Products = Mage::getResourceModel ('catalog/product_collection') ->AddAttributeToSelect ('name') ->AddAttributeToFilter ('special_price', array ('neq' => 0)) ->AddAttributeToFilter ('special_from_date', array ('date' => true, 'to' => $ date)) ->AddAttributeToFilter (array ( array ('attribute' => 'special_to_date', 'date' => true, 'from' => $ date) array ('attribute' => 'special_to_date', 'is' => new Zend_Db_Expr ('null')) )); |
List of news
1 2 3 4 5 6 7 8 9 10 |
$Now = Mage::getModel('core/Date')->timestamp(time ()); $Date= date('Ymd h: i: s', $now); $Products = Mage::getResourceModel ('catalog/product_collection') ->AddAttributeToSelect('name') ->AddAttributeToFilter('news_from_date', array ('date' => true, 'to' => $ date)) ->AddAttributeToFilter (array ( array ('attribute' => 'news_to_date', 'date' => true, 'from' => $ date) array ('attribute' => 'news_to_date', 'is' => new Zend_Db_Expr ('null')) )); |
Top 5 best selling products
1 2 3 4 5 |
$Products = Mage::getResourceModel('reports / product_collection') -> AddAttributeToSelect('name') -> AddOrderedQty() -> AddAttributeToSort('ordered_qty', 'desc') -> SetPageSize(5); |