magento 主页中的特色产品 [英] Featured products in magento home page

查看:50
本文介绍了magento 主页中的特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在主页上显示特色产品.所以我在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>

<?php $i++;if($i%5==0) { $i=1;?><?php } ?><?php endforeach;?>

I want to show featured product in home page.So I Created custom module and block file in app/code/local/FeaturedProduct/Catalog/Block/Product/Featured.php

Code :

class FeaturedProduct_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_List
{
protected $_productCollection;
public function fetchProducts()
{
   $this->productCollection =Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('featured_products', array('eq' => '1'));
  return $this->productCollection;
}
}

Created a configuration file in app/code/local/FeaturedProduct/Catalog/etc/config.xml

Code:

<!--?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>

Created featuredproducts.xml in location app/design/frontend/rwd/default/layout/featuredproduct.xml

Code:

<!--?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>

Created a view file "app/design/frontend/rwd/default/template/catalog/product/featured.phtml"

Code:

<!--?php
$_featuredProductCollection = $this--->fetchProducts();
$_helper = $this->helper('catalog/output');
?>
<div>
<!--?php if (!$_featuredProductCollection--->count()): ?>
<h2><p><!--?php echo $this--->__('There are no products in the featured product list!!') ?></p></h2>
<!--?php else: ?-->
<div class="category-products-grid">
 <!--?php // Grid Mode ?-->
 <!--?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 last odd">
    <!--?php endif ?-->
    <li class="item<?php if (($i - 1) % $_columnCount == 0): ?> first
               <?php elseif ($i % $_columnCount == 0): ?> last<?php endif; ?>">
    <div class="productGrid-description">
    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo 
       $this->stripTags($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) ?>"></a>
     <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--->__('Add to Cart') ?></span>
   </button>
 <!--?php else: ?-->
     <p class="availability out-of-stock">
       <span><!--?php echo $this--->__('Out of stock') ?></span></p>
 <!--?php endif; ?-->
     </li>
         <!--?php if ($i % $_columnCount == 0 || $i == $_collectionSize): ?-->
 </ul>
<!--?php endif ?-->
<!--?php endforeach ?-->
</div>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last'])</script>
 </div>
 <!--?php endif; ?-->

Created a Featured Attribute in Magento Backend with yes/no property from Catalog Input Type for Store Owner dropdown, then include it in attribute set and assign one product as Featured "yes" from Catalog->Manage Products. And after I load the home page it displays nothing.No idea where i have made mistake.I do not want to change local.xml so took this aproach.Is there any thing more I have to do or create other files? Please help .

解决方案

block file

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/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 主页中的特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆