按照某个属性查找所有产品,---比较实用的类!!!!!

Magento特色产品展示
本文介绍了一个Magento扩展模块的实现方式,该模块用于获取并展示特色产品。通过自定义类`MyCompany_Catalog_Block_Product_Featured`继承Magento的`Mage_Catalog_Block_Product_Abstract`,实现了特色产品的筛选和加载。具体包括从数据库中查询指定分类下被标记为特色的商品ID,并加载这些商品的详细信息。

<?php
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function getFeaturedProducts(){
$ids = $this->_getFeaturedProductsIds();

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->where('e.entity_id in (?)', $ids);
$collection->addAttributeToSelect('*');

$productList = $collection->load();

return $productList;
}

public function _getFeaturedProductsIds(){
// instantiate database connection object
$categoryId = $this->getRequest()->getParam('id', false);
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$categoryProductTable = $resource->getTableName('catalog/category_product');
//$productEntityIntTable = $resource->getTableName('catalog/product_entity_int'); // doesn't work
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix().'catalog_product_entity_int';
$eavAttributeTable = $resource->getTableName('eav/attribute');

var_dump($productEntityIntTable); exit;

// Query database for featured product
$select = $read->select()
->from(array('cp'=>$categoryProductTable))
->join(array('pei'=>$productEntityIntTable),'pei.entity_id=cp.product_id', array())
->joinNatural(array('ea'=>$eavAttributeTable))
->where('cp.category_id=?', $categoryId)
->where('pei.value=1')
->where('ea.attribute_code="terry"');

$rows = $read->fetchAll($select);
$ids = array();
foreach($rows AS $row) {
$ids[] = $row['product_id'];
}
$ret = implode(',', $ids);
return $ids;
}
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值