按类别对产品进行排序(在父类别视图中)

时间:2015-09-28 13:45:55

标签: php magento e-commerce magento-1.9.1

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

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

  

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

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

  

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

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

  

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

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

3 个答案:

答案 0 :(得分:1)

我做了类似的事情。

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

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

答案 1 :(得分:0)

我认为你应该从admin创建一个属性,

管理员 - >目录 - >属性 - >管理属性创建属性custom_order

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

单独为每个产品分配位置值。

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

选择一个类别,单击“显示设置”选项卡,

设置“默认商品详情排序依据”custom_order

答案 2 :(得分:0)

1在catalog_block_product_list_collection事件上创建观察者

{"constants": {"value1":"x","value2":"y"},"\u045618n" {"data1":"x", "data2":"y"}}

2创建类Namespace_Module_Model_Observer

    <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>

这里我们按子类别名称设置集合排序,您可以将其更改为类别ID或添加到集合任何类别属性并按此属性排序

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');
    }
}

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