2015-11-20 101 views
1

我想表明在家裏page.So特色產品我創建的自定義模塊和模塊文件中應用程序/代碼/本地/ FeaturedProduct /目錄/座/產品/ Featured.php特色產品在Magento主頁

代碼:

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; 
} 
} 

創建於應用程序/代碼/本地/ FeaturedProduct /目錄的配置文件/ 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> 

在位置應用程序/設計/前端/ RWD /默認/佈局創建featuredproducts.xml/featuredproduct.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> 

創建一個視圖文件「應用程序/設計/前端/ rwd /默認/模板/目錄/產品/ featured.phtml「

代碼:

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

創建於Magento的後端一個特色屬性與店鋪業主下拉目錄輸入類型是/否屬性,那麼它包含在屬性中設置並分配一個產品爲特色的「yes」目錄 - >管理產品。 加載主頁後,它什麼也沒有顯示。不知道我犯了什麼錯誤。我不想更改local.xml,所以採取了這個方法。是否還有更多的事情需要做或創建其他文件?請幫忙 。

+0

問題,我在PHTML文件$看到這個--->刪除多餘 - 代碼將是$ this-> featuredproduct(); –

+0

我刪除了所有評論仍然沒有結果 – Melvin

+0

@QaisarSatti有幫助嗎?請 – Melvin

回答

1

塊文件

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(); 
    } 

} 

應用程序/設計/前端/ RWD /默認/模板/目錄/產品/ 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> 
+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/95934/discussion-between-melvin-and-qaisar-satti)。 – Melvin

+0

Don'tdo從移動設備登錄 –

+0

沒有變化..嘗試清除所有瀏覽器緩存..我是本地主機 – Melvin

0

@Melvin這裏是一個很好的教程,用於顯示特色產品,我認爲這會幫助你。請參考教程。

http://inchoo.net/magento/featured-products-on-magento-frontpage-tutorial/ 
+0

感謝您的鏈接:)這一個在local.xml中,我不想要的變化。 – Melvin

0

請屬性特色產品,並在HTML文件夾中的文件featured.phtml編寫基於featured.phtml特色產品屬性獲得產品的代碼。之後,在cms中的magento主頁上調用此文件。

+0

但我想將其作爲自定義模塊工作 – Melvin