如何将特色产品添加到产品集合中
-
13-12-2019 - |
题
我正在尝试修改一些代码以输出“特色”属性设置为“是”的产品
这是我正在处理的内容:
<?php
class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract{
public function __construct(){
parent::__construct();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'featured');
$attributeId = $attribute->getId();
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize(16)->setCurPage(1);
$this->setProductCollection($products);
}
}
这会输出产品,但不会输出特色产品。
- 如何更改查询以使其仅选择特色产品
- 看起来它正在将所有属性添加到选择中,这是正确的&如何在我的 phtml 模板中输出属性值?
解决方案
尝试做喜欢的事
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
->addAttributeToFilter('featured',array('eq'=>true))
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount();
还要确保您的属性代码是 featured
在 phtml 文件中,您可以获得如下属性
<?php $_helper = $this->helper('catalog/output'); ?>
<?php echo $_helper->productAttribute($_product, $_product->getAttributeCode(), 'attribute_code') ?>
其他提示
请尝试使用bellow code
public function __construct()
{
parent::__construct();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection = $this->_addProductAttributesAndPrices($collection)
->addAttributeToSelect('featured')
->addStoreFilter()
->addAttributeToFilter('featured',true)
->setPageSize(16)
->setCurPage(1);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
$collection->setPageSize(16)
->setCurPage(1);
$this->setProductCollection($collection);
}
.