【问题标题】:Magento - Sort Products by IDMagento - 按 ID 对产品进行排序
【发布时间】:2012-01-13 17:56:33
【问题描述】:

我正在尝试设置我的 Magento 商店,以便按照产品添加到目录的顺序(按产品 ID)对产品进行排序。但是,我还没有找到一个很好的例子来说明如何设置它。在我的大多数产品类别中似乎默认这样做,但不是全部。

我认为前端的“位置”选项可以做到这一点,但它似乎不适用于我的所有类别。我正在使用社区版 1.6.1。

提前致谢!

【问题讨论】:

    标签: sorting magento product


    【解决方案1】:

    复制:
    app/code/core/Mage/Catalog/Block/Product/List.php

    到(创建适当的文件夹):
    app/code/local/Mage/Catalog/Block/Product/List.php

    在 List.php 中找到以下行:

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

    在下面添加以下行:

    $this->_productCollection->joinField('category_product', 'catalog/category_product', 'product_id', 'product_id=entity_id', array('store_id'=> Mage::app()->getStore()->getId()), 'left');
    
    // Here is the explain
    /*
    * @param string $alias 'category_product'
    * @param string $table 'catalog/category_product'
    * @param string $field 'name'
    * @param string $bind 'PK(product_id)=FK(entity_id)'
    * @param string|array $cond
    * @param string $joinType 'left'
    *
    * default definition
    * joinField($alias, $table, $field, $bind, $cond=null, $joinType='inner') 
    *
    */
    

    复制
    app/code/core/Mage/Catalog/Model/Config.php


    app/code/local/Mage/Catalog/Model/Config.php

    在 Config.php 中找到以下行:

    'position'  => Mage::helper('catalog')->__('Position')
    

    替换为:

    $options = array(
       'position'  => Mage::helper('catalog')->__('Position'),
       'product_id' => Mage::helper('catalog')->__('Product ID')
    ); 
    

    PS:我是在家里写这篇文章,我的机器上没有安装 Magento,所以我没有测试,但结构还可以。如果您遇到任何问题,请确保字段和表名。

    【讨论】:

    • 感谢您的快速回复。不幸的是,您的代码似乎对产品订单没有任何影响。在某些类别中,它仍然没有按 id 排序。如果有帮助,通常仅在该类别中有超过 9 或 10 个产品时才会发生。再次感谢!
    • @BWDesign 你的意思是,只有在一个类别中有超过 10 个产品时才进行排序,对吧?
    • 是的。如果一个类别中显示的产品超过 9 或 10 个,则排序不正确。我还注意到,如果按“位置”排序,更改排序方向确实会影响项目。我认为它会按 ID DESC 排序,然后更改为 ID ASC,但显然不是。
    • @BWDesign 如果您不介意,您能否回显或打印出 $this 变量?我很好奇返回值是什么。
    • 您的意思是在 List.php 文件中回显它吗?我不确定在哪里添加回声,因为整个脚本是一个函数。
    【解决方案2】:

    以防万一有人需要: 改变这个:

    'product_id' => Mage::helper('catalog')->__('Product ID')
    

    到这里:

    'entity_id' => Mage::helper('catalog')->__('Product ID')
    

    【讨论】:

      【解决方案3】:

      复制

      app/code/core/Mage/Catalog/Model/Config.php
      

      app/code/local/Mage/Catalog/Model/Config.php
      

      Config.php 中找到以下行:

      $options = array(
          'position'  => Mage::helper('catalog')->__('Position')
      );
      

      替换为:

          $options = array(
      //        'position'  => Mage::helper('catalog')->__('Position')
              'entity_id' => Mage::helper('catalog')->__('Last')
          );
      

      然后将默认排序方向更改为降序: 打开app/design/frontend/your theme/your layout/layout/catalog.xml 添加行

      <action method="setDefaultDirection"><dir>desc</dir></action>
      

      在块中

      <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
         <block type="page/html_pager" name="product_list_toolbar_pager"/>
            <!-- The following code shows how to set your own pager increments -->
      here
      
         </block>
      </block>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-09
        • 2015-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多