magento 主页中的特色产品 [英] Featured products in magento home page
问题描述
我想在主页上显示特色产品.所以我在app/code/local/FeaturedProduct/Catalog/Block/Product/Featured.php
中创建了自定义模块和块文件代码:
class FeaturedProduct_Catalog_Block_Product_Featured 扩展 Mage_Catalog_Block_Product_List{受保护的 $_productCollection;公共函数 fetchProducts(){$this->productCollection =Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addFieldToFilter('featured_products', array('eq' =>'1'));返回 $this->productCollection;}}
在app/code/local/FeaturedProduct/Catalog/etc/config.xml
中创建了一个配置文件代码:
<配置><模块><特色产品目录><version>1.0.0</version></featuredproduct_catalog></模块><全球><块><特色产品><class>FeaturedProduct_Catalog_Block</class></特色产品></块></全球><前端><布局><更新><特色产品><file>featuredproduct.xml</file></特色产品></更新></布局></前端></config>
在位置 app/design/frontend/rwd/default/layout/featuredproduct.xml
中创建了 features代码:
<layout version="0.1.0"><cms_index_index translate="label"><参考名称=内容"><block type="featuredproducts/products_featured" name="featured_product_collection" template="catalog/product/featured.phtml"><action method="setColumnCount"><columns>4</columns></动作></参考></cms_index_index></布局>
创建视图文件app/design/frontend/rwd/default/template/catalog/product/featured.phtml"
代码:
fetchProducts();$_helper = $this->helper('目录/输出');?><div><!--?php if (!$_featuredProductCollection--->count()): ?><h2><p><!--?php echo $this--->__('特色商品列表中没有商品!!') ?></p></h2><!--?php 其他:?--><div class="category-products-grid"><!--?php//网格模式 ?--><!--?php $_collectionSize = $_featuredProductCollection--->count() ?><!--?php $_columnCount = $this--->getColumnCount();?><!--?php $i = 0;foreach ($_featuredProductCollection as $_product): ?--><!--?php if ($i++ % $_columnCount == 0): ?--><ul class="products-grid first lastdd"><!--?php endif ?--><li class="item<?php if (($i - 1) % $_columnCount == 0): ?><?php elseif ($i % $_columnCount == 0): ?>laststripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo$this->helper('catalog/image')->init($_product,'small_image')->resize(135);?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'),null, true) ?>"><h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>"title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><!--?php echo$_helper--->productAttribute($_product, $_product->getName(), 'name') ?></a></h3></div><!--?php if ($_product--->getRatingSummary()): ?><!--?php echo $this--->getReviewsSummaryHtml($_product, 'short') ?><!--?php endif;?--><!--?php echo $this--->getPriceHtml($_product, true) ?><!--?php if ($_product--->isSaleable()): ?><button type="button" title="<?php echo $this->__('Add to Cart') ?>"class="button btn-cart" onclick="setLocation('<?php echo$this->getAddToCartUrl($_product) ?>')"><span class="btn-cart-container"><!--?php echo $this--->__('加入购物车') ?></span>按钮><!--?php 其他:?--><p class="库存缺货"><span><!--?php echo $this--->__('Out of stock') ?></span></p><!--?php endif;?--><!--?php if ($i % $_columnCount == 0 || $i == $_collectionSize): ?--><!--?php endif ?--><!--?php endforeach ?-->
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last'])</script>
<!--?php endif;?-->
在 Magento 后端使用商店所有者的目录输入类型下拉列表中的是/否属性创建了一个特色属性,然后将其包含在属性集中并从目录->管理产品中将一个产品指定为特色是".在我加载主页后,它什么也不显示.不知道我在哪里犯了错误.我不想更改 local.xml,所以采用了这种方法.我还需要做些什么或创建其他文件吗?请帮忙.
块文件
class FeaturedProduct_Catalog_Block_Product_Featured 扩展 Mage_Catalog_Block_Product_Abstract{公共函数 __construct(){$this->setLimit(40);$sort_by = '姓名';$this->setItemsPerRow(4);开关($sort_by){案例0:$this->setSortBy("rand()");休息;情况1:$this->setSortBy("created_at desc");休息;默认:$this->setSortBy("rand()");}}受保护的函数 _beforeToHtml(){$collection = Mage::getResourceModel('catalog/product_collection');$attributes = Mage::getSingleton('目录/配置')->getProductAttributes();$collection->addAttributeToSelect($attributes)->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addAttributeToFilter('featured_products', 1, 'left')->addStoreFilter()->getSelect()->order($this->getSortBy())->limit($this->getLimit());Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);$this->_productCollection = $collection;$this->setProductCollection($collection);return parent::_beforeToHtml();}}
app/design/frontend/rwd/default/template/catalog/product/featured.phtml
getProductCollection()) && $_products->getSize()): ?><div class="clear"></div><div class="new_product_slider"><div id="演示"><div class="text_left"><?php echo Mage::helper('featuredproducts')->getConfigHeading();?></div><div class="text_right"><?php echo $this->__('Fragtfri ved køb over 130 kr.')?>
<div class="clear"></div><div id="owl-demo1" class="owl-carousel"><?php $i=1;foreach ($_products->getItems() as $_product): ?><?php if($i > 1 ) { $j=2;} else { $j=1;} ?><div class="item"><ul><li><a href="<?php echo $_product->getProductUrl() ?>"title="<?php echo $this->htmlEscape($_product->getName()) ?>"class="prod"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(183,183) ?width="183" height="183" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/></a><div 类=封面"<a href="<?php echo $_product->getProductUrl() ?>"title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo strip_tags(ucfirst($this->htmlEscape($_product->getName()))) ?></a></div><h4><?php echo $this->getPriceHtml($_product, true, '-new') ?></h4><aside class="læs_btn"><a href="<?php echo $_product->getProductUrl() ?>"><span><?php echo $this->__('Læs mere');?></span></a></aside><div class="clear"></div>