1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php class Namespace_Modulename_Block_Bestseller extends Mage_Catalog_Block_Product_Abstract{ /** * actually this method works with catalog_*_flat off * but you will get errors when You'll turn them on */ public function notWorkingBestseller(){ $visibility = array( Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG ); $storeId= Mage::app()->getStore()->getId(); $collection= Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->addOrderedQty() ->addAttributeToFilter('visibility',$visibility) ->setOrder('ordered_qty','desc'); return $collection; } } |
The code above, as I wrote in comments, will works (almost) fine if You don’t enable flat_products. Now look at this, full working method (explanations in the code):
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php //tested on Magento 1.8.x class Namespace_Modulename_Block_Bestseller extends Mage_Catalog_Block_Product_Abstract { public function fullWorkingBestSeller() { /** * @var $visibility array * $visibility = Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds(); * is equal to: * $visibility = array( * Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, * Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG * ); */ $visibility = Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds(); $storeId = Mage::app()->getStore()->getId(); /** * we don't need to use * wildcard, just use what You need, query will be faster * @var $attributesToSelect array */ $attributesToSelect = array('name', 'small_image','short_description','price'); /** * Using this method we get right flat table name * @var $productFlatTable string */ $productFlatTable = Mage::getResourceSingleton('catalog/product_flat')->getFlatTableName($storeId); try{ /** * init resource singleton collection */ $resourceCollection = Mage::getResourceSingleton('reports/product_collection') /** * problems starts here * @see \Mage_Reports_Model_Resource_Product_Collection::addOrderedQty */ ->addOrderedQty(); /** * So We need to add this check to make it works if catalog_product_flat is enabled. * If catalog_product_flat is enabled then we join the missing table catalo_product_flat_x */ if(Mage::helper('catalog/product_flat')->isEnabled()){ /** * we add this join adding common $AttributesToSelect * @see \Mage_Eav_Model_Entity_Collection_Abstract::joinTable Doc about joinTable method */ $resourceCollection->joinTable(array('flat_table'=>$productFlatTable),'entity_id=entity_id', $attributesToSelect); }else{ /** * if is disabled we add attributes to the collection using the usual * addAttributeToSelect method */ $resourceCollection->addAttributeToSelect($attributesToSelect); } $resourceCollection /** Use the set visibility method instead of addAttributeToFilter */ ->setVisibility($visibility) /** Add store filter! */ ->addStoreFilter($storeId) ->setPageSize($this->_itemLimits) ->setOrder('ordered_qty','desc'); return $resourceCollection; }catch (Exception $e){ Mage::logException($e->getMessage()); } } } |
Scrivi un commento