自定义块未呈现

时间:2012-02-02 02:27:31

标签: magento

我为客户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上,句柄“catalog_category_view”列在?showLayout=handles中。但是,当我在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简化为单行文本以查看是否模板或子块中的某些内容导致问题。不幸的是,这没有任何影响,我仍然没有输出。

我真的不知道为什么这不起作用。我首先想到的可能是企业版和社区版之间的差异,但是布局/块系统的基本内容在它们之间不太可能不同。显然似乎缺少某些东西,我希望有人能指出正确的方向。

谢谢!

1 个答案:

答案 0 :(得分: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>