我正在尝试调整模块,以便按特定类别为我提供特色产品。我认为它的接近但没有雪茄。显然我不能在这里使用getSingleton(“catalog / layer”)来获取当前的类别ID。目前该模块吐出特色产品,但它们与您所在的特定类别无关。
有关获取当前类别的提示吗?您将看到我在下面尝试的内容/ *这是我的补充* /评论以按当前类别提取产品。
class Adodis_Featuredproducts_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
protected $_priceBlockDefaultTemplate = 'catalog/product/price_fix.phtml';
public $totproduct,$opt;
public function __construct()
{
$totproduct=Mage::getStoreConfig("adodis_featuredproducts/adodis_block/adodis_featuredproduct_block_product_number");
$opt=Mage::getStoreConfig('adodis_featuredproducts/adodis_general/adodis_featuredproducts_display_type');
parent::__construct();
/* THIS IS MY ADDITION */
$layer = Mage::getSingleton(‘catalog/layer’);
var_dump($layer);
$_category = $layer->getCurrentCategory();
/* END MY ADDITION */
$currentCategoryId= $_category->getId();
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
/* @var $product Mage_Catalog_Model_Product */
$todayDate = $product->getResource()->formatDate(time());
$products = $product->setStoreId($storeId)->getCollection()
->addAttributeToSelect('status')
->addAttributeToFilter('featured_products', array('Yes'=>true))
/* THIS IS MY ADDITION */
->addAttributeToFilter('category_id', array('in' => $currentCategoryId))
/* END MY ADDITION */
->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left');
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
if($opt=='block'){
$products->setOrder('hot_deals')->setPageSize($totproduct)->setCurPage(1);
}
else{
$products->setOrder('hot_deals')->setPageSize(4)->setCurPage(1);
}
$this->setProductCollection($products);
}
}
答案 0 :(得分:0)
在此处发布解决方案:
/* THIS IS MY ADDITION */
$_category = Mage::registry('current_category');
/* END MY ADDITION */
$currentCategoryId= $_category->getId();
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
/* @var $product Mage_Catalog_Model_Product */
$todayDate = $product->getResource()->formatDate(time());
$products = $product->setStoreId($storeId)->getCollection()
->addAttributeToSelect('status')
->addAttributeToFilter('featured_products', array('Yes'=>true))
/* THIS IS MY ADDITION */
->addCategoryFilter($_category)
/* END MY ADDITION */