【问题标题】:List Products In Magento By Price But Have Zero Prices At The End在 Magento 中按价格列出产品,但最后价格为零
【发布时间】:2013-03-25 14:16:25
【问题描述】:

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

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

产品 1:3 英镑

产品 2:18 英镑

产品 3:0 英镑

产品 4:0 英镑

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

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

$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(); 的值,以便按照我的意愿返回。

【问题讨论】:

  • 你也应该发布一些代码。无论您尝试过什么
  • 嗨,Deepanshu,我已经更新了我现在的位置。

标签: php magento


【解决方案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);

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

【讨论】:

    【解决方案2】:

    看看这个问题:Magento, custom product list这可能符合你的需要

    【讨论】:

      【解决方案3】:

      在原始 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 子句正常工作。

      【讨论】:

        【解决方案4】:

        我觉得你需要这样的东西

        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 抱歉,我没有正确地重新检查此代码,而是从实际工作的观察者那里复制了大部分代码。 但是可能会有一些错误类型或丢失的字符)

        【讨论】:

        • 你好@TaganPablo,谢谢你,它看起来很有趣。但是,由于对 Magento 了解不多,我什至不知道从哪里开始实现它或为产品列表创建观察者、命名空间或模块
        • 这是magentocommerce.com/wiki/5_-modules_and_development/0-_module_development_in_magento/customizing_magento_using_event-observer_method 的有用手册
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多