按价格列出Magento产品,但最终价格为零

时间:2013-03-25 14:16:25

标签: php magento

产品搜索排序列表中是否有一种方法可以按最高价格到最低价格订购产品,然后在最后显示零价格的产品。

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

  

产品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 price下拉列表时才会运行。

现在的问题是,我不知道如何影响$this->_productCollection = $layer->getProductCollection();的值,以便根据需要进行返回。

4 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:0)

请看这个问题: Magento, custom product list 这可能符合您的需求

答案 2 :(得分: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子句正常工作。

答案 3 :(得分: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>

  2. 其中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;
      }
      }
      
    2. 对不起,我没有正确地重新检查这段代码,但是已经从真正的工作观察者那里复制了大部分代码。 但是可能存在一些错误或错过的字符)