【问题标题】:Sort products by category (in parent category view)按类别对产品进行排序(在父类别视图中)
【发布时间】:2015-09-28 13:45:55
【问题描述】:

我有一个非常独特的问题。

我有一家商店,在这样的设置中有多个类别

收藏
.... 短裤(产品:小号 16-红色和小号 20-蓝色)
....连衣裙(产品:蓝色:16,绿色19)

如果我在商店里打开收藏,我会得到这样的物品

蓝色 16
绿色 19
小 16 - 红色
小 20 - 蓝色

我希望我的输出是这样的:

小号 16 - 红色
小 20 - 蓝色
蓝色 16
绿色 19

我怎样才能得到这个结果?很抱歉我没有提供任何代码,因为我不知道应该如何实现这一点

【问题讨论】:

    标签: php magento e-commerce magento-1.9.1


    【解决方案1】:

    我做了类似的事情。

    在 Magento admin 中,您可以手动设置产品在类别页面上的显示顺序。

    • 目录 -> 管理类别(选择您的类别)
    • 在“类别产品”选项卡下,您将看到一个表格,其中包含分配给该类别的所有产品,在最右侧有一个名为“位置”的列。您可以在此处输入 int 值,数字越小产品在类别页面上的显示位置越高。

    【讨论】:

      【解决方案2】:

      1 在 catalog_block_product_list_collection 事件上创建观察者

          <events>
              <catalog_block_product_list_collection>
                  <observers>
                      <namespace_module>
                          <class> namespace_module/observer</class>
                          <method>collectionList</method>
                      </namespace_module >
                  </observers>
              </catalog_block_product_list_collection>
          </events>
      

      2 创建类 Namespace_Module_Model_Observer

      class Namespace_Module_Model_Observer
      {
          public function collectionList($observer)
          {
              /** @var Mage_Catalog_Model_Category $currentCategory */
              $currentCategory = Mage::registry('current_category');
      
              $children = Mage::getResourceModel('catalog/category')->getChildrenIds($currentCategory);
              if (!$children) {
                  return $this;
              }
              $children = implode(',', $children);
              /** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
              $collection = $observer->getCollection();
      
              $attr = $this->_getAttribute('name');
      
              $collection->getSelect()
                  ->join(
                      array('c' => $this->_getResource()->getTableName('catalog_category_product')),
                      "c.product_id = e.entity_id AND c.category_id IN ($children)",
                      array('child_category_id' => 'category_id')
                      )
                  ->join(
                      array('ac' => $this->_getResource()->getTableName('catalog_category_entity_' . $attr['backend_type'])),
                      "c.category_id = ac.entity_id AND ac.attribute_id = {$attr['attribute_id']}",
                      array('child_category_name' => 'value')
                  )
              ->order('child_category_name DESC');
      
              return $this;
          }
      
          protected function _getAttribute($attributeCode, $static = true, $entityTypeId = 3)
          {
              $readAdapter = $this->_getReadAdapter();
              $select = $readAdapter->select()
                  ->from($this->_getResource()->getTableName('eav/attribute'))
                  ->reset(Zend_Db_Select::COLUMNS)
                  ->columns(array('attribute_id', 'backend_type'))
                  ->where('entity_type_id = ?', $entityTypeId)
                  ->where('attribute_code = ?', $attributeCode)
                  ->limit(1);
              if (!$static) {
                  $select->where('backend_type != ?', 'static');
              }
      
              $entityId = $readAdapter->query($select)->fetch();
              return $entityId;
          }
      
          protected function _getResource()
          {
              return Mage::getSingleton('core/resource');
          }
      
          protected function _getReadAdapter()
          {
              return $this->_getResource()->getConnection('core_read');
          }
      }
      

      这里我们设置collection sort by child category name,你可以将其更改为category id或添加到collection中的任何category属性并以此属性排序

      ->order('child_category_name DESC');
      

      这只是按子类别快速排序产品集合的示例,当然您可以在工具栏中添加选项并动态排序集合

      【讨论】:

        【解决方案3】:

        我认为你应该从管理员创建一个属性,

        Admin->Catalog->Attributes->Manage Attributes创建属性custom_order

        设置在产品列表中使用 = 是 用于产品列表中的排序 = 是

        分别为每个产品分配位置值。

        然后转到管理->目录->管理类别

        选择一个类别,点击显示设置选项卡,

        设置“默认产品列表排序方式”custom_order

        【讨论】:

        • 我将如何/在哪里制作这种排序的代码?
        • 不需要给你编码。默认情况下,其获取结果为升序。
        • 当我看到你的回复时,我一定是累了——对不起。当产品发生变化时,这似乎需要做很多工作,必须有一种方法可以使用自定义属性或更自定义的东西来实现这一点
        猜你喜欢
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        相关资源
        最近更新 更多