3

产品搜索排序列表中是否有一种方法可以按最高价格到最低价格对产品进行排序,但最后会显示零价格的产品。

正常排序工作正常(从低到高或从高到低),但真的希望能够在最后包含零价格产品,如何列出的示例如下:

产品 1:3 英镑

产品 2:18 英镑

产品 3:0 英镑

产品 4:0 英镑

我还没有找到从这里或谷歌搜索的方法,而且对 Magento 不太熟悉,我不确定我会在哪里改变它。如果有人有答案或可以将我指向查询或正确的文件让我自己编辑,我不介意自己去。

使用此处的一些帮助和其他问题,我编辑了_getProductCollection()文件中的方法/app/code/core/Mage/Catalog/Block/Product/List.php(不用担心我已经复制到本地)并添加了以下几行:

$orderFilterType = $this->getRequest()->getParam('order');
$dirFilterType = $this->getRequest()->getParam('dir');

if( isset( $orderFilterType ) && $orderFilterType == 'price' && isset( $dirFilterType ) && $dirFilterType == 'asc' ) {

$this->_productCollection = $layer->getProductCollection()->addAttributeToSort( 'price', 'DESC' );


} else { 

$this->_productCollection = $layer->getProductCollection();

}

这意味着我在这段代码中所做的任何事情都只会在有人使用升序选项选择 orderby 价格下拉菜单后运行。

现在的问题是我不知道该怎么做才能影响$this->_productCollection = $layer->getProductCollection();我想要的返回值。

4

4 回答 4

1

我知道的派对晚了 4 年,但我刚刚以比我找到的其他解决方案更好的方式解决了这个问题,可能会对某人有所帮助。

在 Catalog/Block/Product/List/Toolbar.php 中替换

$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());

$zeroPriceLast = new Zend_Db_Expr('`price_index`.`price` = 0 ASC, `price_index`.`price`  ASC');

$this->_collection->getSelect()->order($zeroPriceLast);

并包装在条件中,以便仅在按价格排序时应用新逻辑。

于 2017-04-20T15:52:13.873 回答
0

看看这里这个问题:Magento, custom product list这可能适合您的需要

于 2013-03-30T23:19:50.543 回答
0

我想你需要这样的东西

  1. 为每个产品列表创建一个观察者:

    <events> <catalog_block_product_list_collection> <observers> <rebuild_collection> <type>singleton</type> <class>XXX_YYY_Model_Catalog_Observer</class> <method>rebuildCollection</method> </rebuild_collection> </observers> </catalog_block_product_list_collection></events>

其中 XXX - 你的命名空间 YYY - 你的模块

  1. 创建.php文件

    <?php
    class XXX_YYY_Model_Catalog_Observer extends Mage_Catalog_Model_Observer
    {
    public function rebuildCollection($observer)
    {
    $event = $observer->getEvent();
    /** @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
    $collection = $event->getCollection();
    
    $toolbar = Mage::getBlockSingleton('catalog/product_list_toolbar');
    
            // here you can add some attributes to collection but it's not required
            // ex. $collection->addAttributeToSelect('brand')
            //    ->addAttributeToSelect('style');
            // you can also force visibility filter Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
    
             // and here is main part for your logic
            // getting current sort order
            $arr = $collection->getSelect()->getPart(Zend_Db_Select::ORDER);
            // probably here you will need to add condition like  IF ORDERING BY PRICE
            $r = Mage::app()->getRequest()->getRequestString();
    
            $order_field = 'new_price_for_sort';
            $collection->getSelect()->joinLeft(array('a' => 'catalog_product_entity_price'), // or here you will need Price Index Table Name if you have Groupped or configurable products. But need to remember that non-simple products always have 0 price and MAX_PRICE/MIN_PRICE should be used then 
                            'a.product_id=e.entity_id AND a.attibute_id=PRICE or SPECIAL PRICE ATTRIBUTE ID', // or here you will not require attribute_id condition if you are using INDEX TABLE
                            array(
                                'new_price_for_sort' => new Zend_Db_Expr('IF(a.value > 0,a.value, 9999999)'), // which mean that in new field we will have price if it's not 0, and will have big integer is real price is zero. 
                            ));
                    }
    // now we are reseting current order by
                    $collection->getSelect()->reset(Zend_Db_Select::ORDER);
    
    // and creating new order by new field, but with saving direction of order
    $dir = $arr['dir'];
                    if (!$collection->isEnabledFlat()) {
                        $collection->setOrder($order_field, $dir);
                    } else {
                        $collection->getSelect()->order($order_field . ' ' . $dir);
                    }
    
    
                }
          //check in log if collection real built well
            Mage::log((string)$collection->getSelect(), null, 'selects.log');
          // add collection to list toolbar
            $toolbar->setCollection($collection);
        }
    return $collection;
    }
    }
    

PS 抱歉,我没有正确地重新检查此代码,而是从真正的工作观察者那里复制了大部分代码。但可能有一些错误类型或遗漏的字符)

于 2013-03-31T11:45:10.587 回答
0

在原始 SQL 中执行此操作如下所示:

SELECT main.*, price.value as price FROM catalog_product_entity AS main
    JOIN catalog_product_entity_decimal AS price ON price.entity_id = main.entity_id
    JOIN eav_attribute AS attr_price ON attr_price = 'price' AND price.attribute_id = attr_price.attribute_id
ORDER BY price == 0, price

您可能必须覆盖核心块才能使该ORDER BY price ==0, price子句正常工作。

于 2013-03-31T05:50:12.403 回答