-
13-12-2019 - |
题
我注意到“特色产品”属性存在于最新版本的Magento中。如何在RWD主题中的主页上显示我的特色产品?
谢谢
解决方案
我没有将此属性视为默认的默认值添加到magento 1.9.x,但非常容易根据自定义产品属性i.e.'fereure_product'创建产品集合。
一旦您构建了集合,您可以通过模板文件,即单个产品功能或多产品滑块创建要显示的HTML。
执行此操作的步骤是
- 创建一个新的自定义产品集合方法
- 创建一个新的布局块
- 创建一个新模板
例如,如果我想使用属性'特色_product'为可见产品创建产品集合,我在Magento / app / code / local / mage / coperog / block / product
<?php
class Mage_Catalog_Block_Product_Featured extends mage_Catalog_Block_Product_Abstract
{
protected $_productsCount = null;
protected $_categoryId = null;
const DEFAULT_PRODUCTS_COUNT = 12;
/**
* Initialize block's cache
*/
protected function _construct()
{
parent::_construct();
$this->addColumnCountLayoutDepend('empty', 6)
->addColumnCountLayoutDepend('one_column', 5)
->addColumnCountLayoutDepend('two_columns_left', 4)
->addColumnCountLayoutDepend('two_columns_right', 4)
->addColumnCountLayoutDepend('three_columns', 3);
$this->addData(array(
'cache_lifetime' => 86400,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
));
}
/**
* Get Key pieces for caching block content
*
* @return array
*/
public function getCacheKeyInfo()
{
return array(
'CATALOG_PRODUCT_NEW',
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
'template' => $this->getTemplate(),
$this->getProductsCount(),
Mage::app()->getStore()->getCurrentCurrencyCode(),
empty($this->_categoryId) ? "ALL" : $this->_categoryId
);
}
/**
* Prepare collection with new products and applied page limits.
*
* return Mage_Catalog_Block_Product_New
*/
protected function _beforeToHtml()
{
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToSelect('featured_product')
->addFieldToFilter(array(
array('attribute'=>'ext_enable','eq'=>'1'),
))
->addAttributeToSort('name', 'desc')
->setPageSize($this->getProductsCount())
->setCurPage(1)
;
// Limit to a specific category
//if(!empty($this->_categoryId)) $collection->addCategoryFilter(Mage::getModel("catalog/category")->load($this->_categoryId));
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
/**
* Set a specific category to take the products from.
*
* @param $catid
* @return Mage_Catalog_Block_Product_New
*/
public function setCategory($catid)
{
$this->_categoryId = (int)$catid;
return $this;
}
/**
* Set how much product should be displayed at once.
*
* @param $count
* @return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
/**
* Get how much products should be displayed at once.
*
* @return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
}
.
然后我将以下内容添加到我的布局中,例如,如果我希望块出现在主页上,则可以将其放在CMS_INDEX_INDEX部分
中 <block type="catalog/product_featured" name="catalog.product.featured" alias="featured_products">
<action method="setTemplate"><template>catalog/product/featured.phtml</template></action>
</block>
.
然后在我的模板中我在目录/产品中创建一个名为texture.phtml的新文件,这里我可以通过产品集合迭代并创建我想要显示的HTML,例如,这是一个简单的项目符号列表。
<?php
$_helper = $this->helper('catalog/output');
$_productCollection = $this->getProductCollection();
?>
<div class="featured-products">
<p>Featured Products - <?php echo count($_productCollection) ?></p>
<ol class="featured-products-list" id="featured-products-list">
<?php foreach ($_productCollection as $_product): ?>
<li><?php echo $_product->getName() ?></li>
<?php endforeach; ?>
</ol>
</div>
.
当我刷新我的商店缓存并转到主页时,将显示特色产品HTML。
有很多模块会帮助您执行此操作,但您可以通过此示例快速实现它,也可以通过创建自己的模块来快速实现。
这里是来自magento wiki的类似解决方案的链接 https://wiki.magento.com/display/m1wiki/how anyto+create +a + featured +Product
其他提示
按照此链接获取主页生成的功能产品
http://inchoo.net/magento/featured-products-on-Magento-FrontPage /
本教程我正在解释如何在Magento主页上使用产品。
在phtml文件中:
<?php
$featuredProducts = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('featured_product', 1)
->addAttributeToFilter('status', 1);
.
?> 在local.xml
<cms_index_index>
<reference name="content">
<block type="core/template" name="home.featured.product" template="custom/featuredproduct.phtml"/>
</reference>
.
请通过步骤说明来引用TOTROIAL。 http://www.pearlbells.co.uk/adding-Custom-Product-Attributes-In-Magento /