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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 按类别对产品进行排序(在父类别视图中)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个非常独特的问题.

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

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

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

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

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

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

->order('child_category_name DESC');

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 按类别对产品进行排序(在父类别视图中)全部内容,希望文章能够帮你解决php – 按类别对产品进行排序(在父类别视图中)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。