Manipulate collections of products in Magento





Retrieve a collection of products
[php]
$Products = Mage :: getResourceModel (‘catalog/product_collection’);
/ * Or * /
$Products = Mage :: getModel (‘catalog/product’)->GetCollection();
[/php]

Selecting a specific attribute
[php]
$Products = Mage::getResourceModel(‘catalog/product_collection’)
->AddAttributeToSelect(‘name’)
->AddAttributeToSelect(‘weight’);
[/php]

Selecting all attributes
[php]
$Products = Mage::getResourceModel (‘catalog / product_collection’)
-> AddAttributeToSelect (‘*’);
[/php]

Filters on product attributes
[php]
$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));
[/php]

Filters on product categories
[php]
$Category = Mage::getModel (‘catalog/category’)->load (1);

$Products = Mage::getResourceModel (‘catalog/product_collection’)
->AddAttributeToSelect(‘name’)
->AddCategoryFilter($category);
[/php]

Limit the number of results
[php]
$Products = Mage::getResourceModel (‘catalog/product_collection’)
-> AddAttributeToSelect(‘name’)
-> SetPageSize(10);
[/php]

Sort results
/ * Sort on a specific attribute * /
[php]
$Products = Mage::getResourceModel (‘catalog/product_collection’)
-> AddAttributeToSelect (‘name’)
-> AddAttributeToSort (‘price’, ‘asc’);
[/php]

/ * Sort randomly (Rand) * /
[php]
$Products = Mage::getResourceModel(‘catalog/product_collection’)
->AddAttributeToSelect(‘name’);

$Products->getSelect()->order(new Zend_Db_Expr (‘RAND()’));
[/php]

Other useful methods
[php]
$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();
[/php]

Collection of products with statistics (Reports)
[php]
$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’);
[/php]

List of products offers
[php]
$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’))
));
[/php]

List of news
[php]
$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’))
));
[/php]

Top 5 best selling products
[php]
$Products = Mage::getResourceModel(‘reports / product_collection’)
-> AddAttributeToSelect(‘name’)
-> AddOrderedQty()
-> AddAttributeToSort(‘ordered_qty’, ‘desc’)
-> SetPageSize(5);
[/php]

Leave a Comment

Your email address will not be published. Required fields are marked *

*