I am looking for collection which have number of products that ordered by specific customer. If anyone have any suggestion please share.
Magento 1.9 – Get Ordered Items from Last 10 Days for Specific Customer
customermagento-1.9ordersproducts
Related Solutions
For anyone stumbling over this in the future, I've gone for the following code as the most (to me) sensible version:
$collection = Mage::getResourceModel('sales/order_item_collection');
$collection->getSelect()->reset(Zend_Db_Select::COLUMNS)
->columns(array('SUM(qty_ordered) as order_quantity'))
->where(sprintf("product_id = %s", $row->getProductId()))
->where(sprintf('created_at > "%s" AND created_at < "%s"', $week["from"], $week["to"]));
I'm yet to figure out if it would be quicker to grab all my date ranges at once and work through them in PHP, or if it's quicker like this creating a separate block for each week.
I found out that ORDER BY RAND()
is very unperformant on a Magento collection because all data gets copied to a temp table, assigned a random number and then sorted without index. If we reduce the data to be copied to just the ID, it gets a lot faster (still not perfect because the slow sorting remains but OK for a small catalog like yours).
For this, I made a modified version of getAllIds()
:
$numberOfItems = 5;
// Step 1: Preselect ids using ORDER BY RAND()
// Filters are applied here
$productCollection = Mage::getModel('catalog/product')
->getCollection();
$productCollection
->addStoreFilter()
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$productCollection->getSelect()->order('RAND()');
$idsSelect = clone $productCollection->getSelect();
$idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
$idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
$idsSelect->reset(Zend_Db_Select::COLUMNS);
$idsSelect->columns('e.' . $productCollection->getEntity()->getIdFieldName());
$idsSelect->limit($numberOfItems, null);
$idsSelect->resetJoinLeft();
$accessor = new ReflectionObject($productCollection);
$_bindParams = $accessor->getProperty('_bindParams');
$_bindParams->setAccessible(true);
$chosenIds = $productCollection->getConnection()
->fetchCol($idsSelect, $_bindParams->getValue($productCollection));
Then I could load the products by these random ids:
// Step 2: Load products
// Attributes and index data are joined here
$productCollection->addIdFilter($chosenIds);
$productCollection
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
$productCollection->load();
Simpler alternative
An alternative that also works well on small catalogs (<10000 products) is to load all ids and select the random IDs with PHP:
$numberOfItems = 5;
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection
->addStoreFilter()
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$candidateIds = $productCollection->getAllIds();
$numberOfProducts = count($candidateIds);
$chosenIds = [];
while ($numberOfItems) {
$randomKey = mt_rand(0, $numberOfProducts - 1);
if (!isset($chosenIds[$randomKey])) {
chosenIds[$randomKey] = $candidateIds[$randomKey];
--$numberOfItems;
}
}
$productCollection->addIdFilter($chosenIds);
$productCollection
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
$productCollection->load();
You can read a more in depth analysis and see some benchmarks in my blog: http://www.schmengler-se.de/en/2015/09/show-random-products-in-magento-you-are-doing-it-wrong/
Best Answer
First get all order of cutomer last 10 days.
Then count number of product order on that orders.