2012-02-02 62 views
0

我爲客戶端Magento網站開發了一個集合模塊。除此之外,該模塊還在類別列表頁面上提供產品詳細信息(媒體,說明,屬性)。我遇到的問題是我的塊不在客戶站點(Magento EE 1.8)上呈現,即使所有東西都在本地工作(Magento CE 1.6)。自定義塊不被呈現

開發者模式已被激活,但我看不到頁面上的錯誤,並且我知道Magento正在看到該模塊,因爲它已正確顯示在管理員的系統>配置>高級下。

我們app/code/local/Mycompany/Collections/etc/config.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Mycompany_Collections> 
      <version>0.1.0</version> 
     </Mycompany_Collections> 
    </modules> 
    <global> 
     <blocks> 
      <mycompany_collections> 
       <class>Mycompany_Collections_Block</class> 
      </mycompany_collections> 
     </blocks> 
    </global> 
</config> 

設置的基本塊類名,我們在app/design/frontend/enterprise/mytheme/layout/local.xml

<?xml version="1.0"?> 
<layout version="0.1.0"> 
    <catalog_category_view> 
     <reference name="product_list"> 
      <block type="mycompany_collections/collection" name="collection" template="collections/collection.phtml"> 
       <block type="mycompany_collections/product" name="add-to-cart" template="collections/product/add-to-cart.phtml" /> 
       <block type="mycompany_collections/product" name="description" template="catalog/product/view/description.phtml" /> 
       <block type="mycompany_collections/product_attributes" name="attributes" template="catalog/product/view/attributes.phtml" /> 
       <block type="mycompany_collections/product_media" name="media" template="catalog/product/view/media.phtml" /> 
      </block> 
     </reference> 
    </catalog_category_view> 
</layout> 

mycompany_collections/collection塊擴展Mage_Catalog_Block_Product_List插入我們的塊的佈局和重新加載的產品,以確保我們要取所有來自數據庫的相關數據。

class Mycompany_Collections_Block_Collection extends Mage_Catalog_Block_Product_List { 
    public function reloadProducts() { 
     // Fully reload each of the products in this category so that we have 
     // all the information required to display product details. 

     // TODO: find a more efficient way to grab all the info for all the 
     // products, as this would seem to add 1 (or more) additional 
     // query per product. 

     $reloaded = array(); 
     foreach($this->getParentBlock()->getLoadedProductCollection() as $product){ 
      $reloaded[] = Mage::getModel('catalog/product')->load($product->getId()); 
     } 
     return $reloaded; 
    } 
} 

mycompany_collections/product塊擴展Mage_Catalog_Block_Product_Abstract定製的方法,以使我們能夠明確地設定塊上的產品,並返回該產品不從註冊表中拉出。

class Mycompany_Collections_Block_Product extends Mage_Catalog_Block_Product_Abstract { 
    private $_product = null; 

    public function _prepareLayout() { 
     // We don't need to do anything here. 
    } 

    // NOTE: Must be called before ->toHtml() 
    public function setProduct($product) { 
     $this->_product = $product; 
     return $this; 
    } 

    public function getProduct() { 
     return $this->_product; 
    } 
} 

無論是mycompany_collections/product_attributesmycompany_collections/product_media塊做同樣的GET/set_product覆蓋其等同抽象父類。

裏面我們collection.phtml模板,我們稱之爲$this->reloadProducts()和迭代的產品列表中顯示的產品細節和購買收藏的彈出窗口(這些是實現點擊激活JS收藏夾)

<?php 
    $_productCollection = $this->reloadProducts(); 
?> 

<!-- Buy collection popup --> 
<div id="buy-collection" class="no-display"> 
    <h1>Buy Collection</h1> 
    <?php foreach($_productCollection as $_product): ?> 
     <div id="buy-collection-product-<?php echo $_product->getId(); ?>" class="product"> 
      <div class="media"><?php echo $this->getChild('media')->setProduct($_product)->toHtml(); ?></div> 
      <?php /* 
      <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(100); ?>" width="100" height="100" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a> 
      */ ?> 
      <a href="<?php echo $_product->getProductUrl() ?>" ><?php echo $_product->getName(); ?></a> 
      <?php if($_product->isSaleable()): ?> 
       <div class="add-to-cart"><?php echo $this->getChild('add-to-cart')->setProduct($_product)->toHtml(); ?></div> 
      <?php else: ?> 
       <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div> 
      <?php endif; ?> 
      <div class="details"> 
       <div class="description"> 
        <?php echo $this->getChild('description')->setProduct($_product)->toHtml(); ?> 
       </div> 
       <div class="attributes"> 
        <?php echo $this->getChild('attributes')->setProduct($_product)->toHtml(); ?> 
       </div> 
      </div> 
     </div> 
    <?php endforeach; ?> 
</div> 

<!-- Product detail popups --> 
<div id="product-details" class="no-display"> 
    <?php foreach ($_productCollection as $_product): ?> 
    <div id="product-detail-<?php echo $_product->getId(); ?>" class="product"> 
     <div class="media"><?php echo $this->getChild('media')->setProduct($_product)->toHtml(); ?></div> 
     <div class="description"><?php echo $this->getChild('description')->setProduct($_product)->toHtml(); ?></div> 
     <div class="attributes"><?php echo $this->getChild('attributes')->setProduct($_product)->toHtml(); ?></div> 
    </div> 
    <?php endforeach; ?> 
</div> 

要真正解僱所有對於我們希望顯示爲集合的任何產品類別,我們會覆蓋管理中該類別的「自定義設計」選項卡中的category.listingproduct_list模板。這些模板包含設計更改,並且product/list.phtml調用收集塊。

<reference name="product_list"> 
    <action method="setTemplate"> 
     <template>catalog/collections/product/list.phtml</template> 
    </action> 
</reference> 

<reference name="category.products"> 
    <action method="setTemplate"> 
     <template>catalog/collections/category/view.phtml</template> 
    </action> 
</reference> 

裏面product/list.phtml我們所說的集合塊用一個簡單的echo $this->getChildHtml('collection');。這條線上沒有返回。沒有模板,沒有PHP錯誤,沒有。如上所述,這一切都在我的本地開發環境中運行良好。

這是關於如何設置的概述。以下是我所做的調試:

使用Alan Storm的Layoutviewer模塊,我已確認我的塊在?showLayout=page上列出,並且?showLayout=handles中列出了「catalog_category_view」句柄。但是,當我在product/list.phtml中打印$this->getSortedChildren()時,收集塊未列出。

如果我用一個超級簡單的core/text塊替換layout.xml中的集合塊,它確實渲染。

<?xml version="1.0"?> 
<layout version="0.1.0"> 
    <catalog_category_view> 
     <reference name="product_list"> 
      <block type="core/text" name="collection"><action method="setText"><text>This is a test</text></action></block> 
     </reference> 
    </catalog_category_view> 
</layout> 

這使我相信這個問題是在我自己的代碼更深,所以我簡化,都幾乎要基本...我從Mycompany_Collections_Block_Collection刪除reloadProducts方法,減少collections.phtml文本單行,看看模板或子塊中的某些內容導致了問題。不幸的是,這沒有影響,我仍然沒有輸出。

我真的不知道爲什麼這不起作用。我首先想到的可能是企業版和社區版之間的差異,但佈局/區塊系統的基本特徵不可能在它們之間有所不同。顯然似乎缺少了某些東西,我希望有人能夠指出正確的方向。

謝謝!

+0

您是否清除了magento緩存?總是要檢查的第一件事。 – ShaunOReilly 2012-02-02 02:46:46

+0

是的,緩存是完全禁用的。 – dmpayton 2012-02-02 02:58:40

+1

可能是這樣,但仍然清除/ var/cache folderanyway。刪除它中的所有文件。 – ShaunOReilly 2012-02-02 03:07:27

回答

0

您是否檢查Mycompany_Collections.xml可用於/app/etc/modules部分並啓用了代碼池?

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Mycompany_Collections> 
      <active>true</active> 
      <codePool>local</codePool> 
     </Mycompany_Collections> 
    </modules> 
</config> 
+0

是的,我已將'Mycompany_Collections'添加到'app/etc/modules/Mycompany_All.xml',其中active設置爲true,codePool設置爲local。 – dmpayton 2012-02-02 18:11:35

+0

如果我沒有弄錯,你的模塊名稱爲Mycompany_Collections。因此,你的模塊應該在'/ app/code/local/Mymodule/Collections'目錄中可用,並且xml配置文件必須是'app/etc/modules'文件夾中的'Mymodule_Collections.xml'。 – 2012-02-02 19:09:11