我想在主页上显示特色产品。所以我在 app / code / local / FeaturedProduct / Catalog / Block / Product / Featured.php
中创建了自定义模块和块文件代码:
class FeaturedProduct_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function __construct()
{
$this->setLimit(40);
$sort_by = 'name';
$this->setItemsPerRow(4);
switch ($sort_by) {
case 0:
$this->setSortBy("rand()");
break;
case 1:
$this->setSortBy("created_at desc");
break;
default:
$this->setSortBy("rand()");
}
}
protected function _beforeToHtml()
{
$collection = Mage::getResourceModel('catalog/product_collection');
$attributes = Mage::getSingleton('catalog/config')
->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 / code / local / FeaturedProduct / Catalog / etc / config.xml
中创建配置文件代码:
<!--?xml version="1.0" encoding="UTF-8"?-->
<config>
<modules>
<featuredproduct_catalog>
<version>1.0.0</version>
</featuredproduct_catalog>
</modules>
<global>
<blocks>
<featuredproducts>
<class>FeaturedProduct_Catalog_Block</class>
</featuredproducts>
</blocks>
</global>
<frontend>
<layout>
<updates>
<featuredproducts>
<file>featuredproduct.xml</file>
</featuredproducts>
</updates>
</layout>
</frontend>
</config>
在位置 app / design / frontend / rwd / default / layout / featuredproduct.xml
中创建了featuredproducts.xml代码:
<!--?xml version="1.0" encoding="UTF-8"?-->
<layout version="0.1.0">
<cms_index_index translate="label">
<reference name="content">
<block type="featuredproducts/products_featured" name="featured_product_collection" template="catalog/product/featured.phtml">
<action method="setColumnCount">
<columns>4</columns>
</action>
</block>
</reference>
</cms_index_index>
</layout>
创建了一个视图文件“app / design / frontend / rwd / default / template / catalog / product / featured.phtml”
代码:
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="clear"></div>
<div class="new_product_slider">
<div id="demo">
<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>
<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 class="cover"
<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>
</li>
</ul>
</div>
<?php $i++; if($i%5==0) { $i=1; ?>
<?php } ?>
<?php endforeach; ?>
</div>
</div>
</div>
在Magento后端创建一个特色属性,使用商店所有者下拉列表的目录输入类型中的是/否属性,然后将其包含在属性集中,并将一个产品指定为目录 - >管理产品中的精选“是”。
将此添加到主页cms
{{block type="featuredproduct/product_collection" name="product_collection" as="product_collection" template="catalog/product/featured.phtml"}}
在我加载主页后,它什么都没显示。我不知道我在哪里犯了错误。我不想改变local.xml所以拿了这个aproach.Is还有什么我需要做的或者创建其他文件吗?请帮忙。
修改
创建模块配置文件app / etc / modules / FeaturedProduct_Catalog.xml,现在显示在admin -conf-adva
<?xml version="1.0"?>
<config>
<modules>
<FeaturedProduct_Catalog>
<active>true</active>
<codePool>local</codePool>
</FeaturedProduct_Catalog>
</modules>
</config>
答案 0 :(得分:0)
这是特色产品的工作模块,请遵循它。
应用程序的/ etc /模块/ Solsint_Featured.xml
<?xml version="1.0"?>
<config>
<modules>
<Solsint_Featured>
<active>true</active>
<codePool>local</codePool>
</Solsint_Featured>
</modules>
</config>
应用程序/代码/本地/ Solsint /精选的/ etc / config.xml中
<!--?xml version="1.0" encoding="UTF-8"?-->
<config>
<modules>
<Solsint_Featured>
<version>1.0.0</version>
</Solsint_Featured>
</modules>
<global>
<blocks>
<featured>
<class>Solsint_Featured_Block</class>
</featured>
</blocks>
</global>
</config>
应用程序/代码/本地/ Solsint /精选/砌块/产品/ Featured.php
<?php
class Solsint_Featured_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
{
public function __construct()
{
$this->setLimit(40);
$sort_by = 'name';
$this->setItemsPerRow(4);
switch ($sort_by) {
case 0:
$this->setSortBy("rand()");
break;
case 1:
$this->setSortBy("created_at desc");
break;
default:
$this->setSortBy("rand()");
}
}
protected function _beforeToHtml()
{
$collection = Mage::getResourceModel('catalog/product_collection');
$attributes = Mage::getSingleton('catalog/config')
->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();
}
}
应用程序/设计/前端/碱/默认/模板/目录/产品/ featured.phtml
<?php
if ($_products = $this->getProductCollection()): ?>
<div class="clear"></div>
<div class="new_product_slider">
<div id="demo">
<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>
<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 class="cover"
<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>
</li>
</ul>
</div>
<?php $i++; if($i%5==0) { $i=1; ?>
<?php } ?>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
cms页面代码
{{block type="featured/product_featured" name="product_collection" as="product_collection" template="catalog/product/featured.phtml"}}