我正在创建一个事件观察者模块,它将按类别对产品进行分组,
下面是我的模块的model / observer.php文件,
public function updateUpsells($observer)
{
$iCurrentCategory = Mage::registry('current_category')->getId();
$event = $observer->getEvent();
$oUpsellCollection = $event->getCollection();
foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
$aCategoriesIds = $oUpsellProduct->getCategoryIds();
if (!in_array($iCurrentCategory, $aCategoriesIds)) {
$oUpsellCollection->removeItemByKey($key);
}
}
}
}
启用此模块后,我可以通过echo upsell product's category id's
upsell.phtml
文件中加入$this->getItemCollection()->getItems()
因此我得到了,
Array
(
[170] => Mage_Catalog_Model_Product Object
(
.........................
[_data:protected] => Array
(
[entity_id] => 170
[entity_type_id] => 10
[attribute_set_id] => 44
[type_id] => simple
[category_ids] => Array
(
[0] => 12
[1] => 26
)
.......................
)
[171] => Mage_Catalog_Model_Product Object
(
.........................
[_data:protected] => Array
(
[entity_id] => 171
[entity_type_id] => 10
[attribute_set_id] => 44
[type_id] => simple
[category_ids] => Array
(
[0] => 16
[1] => 24
)
.......................
)
)
所以现在我如何按类别对此数组结果进行分组。示例提到了2个分销到4个不同类别的追加销售产品,因此我需要对产品进行分组,例如,
Category id 12
product1 | Product2
Category id 26
product1 | Product2