Magento产品按销量排行How to sort/show products by sold quantity in Magento

2,430 人次阅读
没有评论

共计 1847 个字符,预计需要花费 5 分钟才能阅读完成。

Toolbar.php file location:
app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

Your _construct() function should be modified to look something like:
[php]
$this->_availableOrder = array(

position’ => $this->__(’Best Value’),

name’ => $this->__(’Name’),

price’ => $this->__(’Price’),

special_price’ =>$this->__(’Special Price’),

‘ordered_qty’ => $this->__(’Quantity soled’),

);

[/php]

Next we open the list.phtml inside /template/catalog/product/ folder. And you need to find the:

[php]

$_productCollection=$this->getLoadedProductCollection();

[/php]

Expression and cut => paste somewhere to the top of the file. Then we
add some additional logic to that same file. So you should write
something like

[php]

// Custom added

$orderFilterType = $this->getRequest()->getParam(’order’);

// Part of original template code, just moved to top of the file
$_productCollection=$this->getLoadedProductCollection();

// Custom added

if(isset($orderFilterType) && $orderFilterType === ‘ordered_qty’) {

$storeId = Mage::app()->getStore()->getId();

$_productCollection = null;

$_productCollection = Mage::getResourceModel(’reports/product_collection’)
->addAttributeToSelect(’*’)

->addOrderedQty()

->setOrder(’ordered_qty’, $this->getRequest()->getParam(’dir’));

}
[/php]
to the top of the list.phtml file.

File list.phtml should consists of two major parts:
•List mode, which starting with comment
•Grid mode, which starting with comment

At the beginning of each part (mode) there is a “foreach” loop like
[php]
<?php foreach ($_productCollection as $_product): ?>

[/php]

What we need to do now is to duplicate entire content inside that
foreach loop and set it in if-else statement where if statement would go
like:
[php]

<?php if(isset($orderFilterType) && $orderFilterType === ‘ordered_qty’): ?>

// default template code between inside foreach loop

<?php else: ?>

// duplicated default template code inside foreach loo

<?php endif; ?>

[/php]

Now do the same for Grid mode (since grid mode is the default one).

To test if the grid/list is returning the right stuff you might do something like

getName() .’ has been sold in qty: ‘.$_product->ordered_qty.’ –>’); ?>

inside the if statement.

正文完
 0
评论(没有评论)