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

1在catalog_block_product_list_collection事件上创建观察者

namespace_module/observer

collectionList

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值