一个得到添加属性(下拉条)brandss,某一特定值下的所有产品的例子

来源:互联网 发布:淘宝促销价格怎么添加 编辑:程序博客网 时间:2024/06/08 10:02

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

$eavAttributeTable1 = $resource->getTableName('eav/attribute_option_value'); 

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

->join(array('ea1'=>$eavAttributeTable1),'pei.value=ea1.option_id',array())  

 

                       ->where('cp.category_id=?', $categoryId)  

                    //   ->where('pei.value=1')

->where('ea1.value="brand1"') 

                       ->where('ea.attribute_code="brandss"');  

 

                       $rows = $read->fetchAll($select);  

        $ids = array(); 

        foreach($rows AS $row) {  

          $ids[] = $row['product_id'];  

        }  

        $ret = implode(',', $ids);  

        return $ids;  

    }  

}  

 

 

?>  

原创粉丝点击