Created
April 3, 2019 14:33
-
-
Save AndresInSpace/7d1dbb2e4e933143bd4ae8da5236cd5b to your computer and use it in GitHub Desktop.
Magento 1 Bestsellers from Order Data (Not aggregated data)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Best Sellers Category */ | |
$today = time(); | |
$from = date('Y-m-d', strtotime("-12 months",$today));//pull best sellers from 12 months prior order data | |
$to = date("Y-m-d", $today); | |
$resource = Mage::getSingleton('core/resource'); | |
$readConnection = $resource->getConnection('core_read'); | |
$orderTableAliasName = $readConnection->quoteIdentifier('order'); | |
$orderJoinCondition = array( | |
$orderTableAliasName . '.entity_id = order_items.order_id', | |
$readConnection->quoteInto("{$orderTableAliasName}.state <> ?", Mage_Sales_Model_Order::STATE_CANCELED), | |
);//ensure we don't count canceled orders.. we count refunds/returns tho, since the sale was actually made. | |
if ($from != '' && $to != '') { | |
$fieldName = $orderTableAliasName . '.created_at'; | |
$orderJoinCondition[] = sprintf('(%s BETWEEN %s AND %s)', | |
$fieldName, | |
$readConnection->quote($from), | |
$readConnection->quote($to) | |
); | |
} | |
$ids = []; | |
$parents = []; | |
$select = $readConnection->select()->reset() | |
->from( | |
array('order_items' => $resource->getTableName('sales/order_item')), | |
array( | |
'ordered_qty' => 'SUM(order_items.qty_ordered)', | |
'product_id' => 'order_items.product_id', | |
'product_type', | |
'name' | |
)) | |
->joinInner( | |
array('order' => $resource->getTableName('sales/order')), | |
implode(' AND ', $orderJoinCondition), | |
array()) | |
->joinLeft( | |
array('super' => $resource->getTableName('catalog/product_super_link')), | |
'super.product_id = order_items.product_id', | |
array('product_super_id' => 'super.parent_id') | |
) | |
->where('order_items.parent_item_id IS NULL AND order_items.sku NOT LIKE ?','%GWP%') | |
->group('product_id') | |
->having('SUM(order_items.qty_ordered) > ?', 0) | |
->order('SUM(order_items.qty_ordered) DESC') | |
->limit(58);//i limit to 58 so I can pull a few extra just in case some get removed during lint | |
$results = $readConnection->fetchAll($select); | |
foreach($results as $row){ | |
$ids[] = $row['product_id']; | |
if($row['product_super_id']){ | |
$parents[$row['product_id']] = $row['product_super_id']; //has parent | |
} | |
} | |
$simples = array_diff($parents,$ids); //lint the simples if parents already included | |
$filteredIds = array_merge($ids,$simples); //merge all unique simples with rest of IDs into one array | |
/* we now have items that are not simples of any configs already pulled */ | |
$collection = Mage::getResourceModel('catalog/product_collection') | |
->setStoreId($this->getStoreId()) | |
->addAttributeToFilter('entity_id', array('in' => $filteredIds)); | |
$collection->getSelect()->order(new Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $filteredIds).')')); | |
$collection->getSelect()->limit(48); | |
return $collection; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment