Magento 块未加载

Magento Block not Loading

我正在 Magento 中构建我的第一个模块,有几个与该过程相关的问题。

在我尝试模块之前,我只有一个模板并使用此代码将其加载到 app\design\frontend\rwd\default\layout\local.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="after_body_start">
            <block type="checkout/cart_sidebar" template="mgw/mwCartRebuild.phtml"/>
        </reference>
    </default>
</layout>

生活很好,一切都很顺利,直到我决定需要延长 Mage_Checkout_Block_Cart_Sidebar class。

所以我为此创建了一个模块。这是我的代码。

阻止 app\code\local\mgw\Cart\Block\ModalCart.php

<?php 
class mgw_Cart_Block_Modal_Cart extends Mage_Checkout_Block_Cart_Sidebar{
    public function __construct(){
        perent::__construct();
        $this->setTemplate('mgw/mwCartRebuild.phtml');
    }
}

config.xml app\code\local\mgw\Cart\etc\config.xml

<config>
    <global>
        <modules>
            <mgw_Cart>
                <version>0.0.0</version>
            </mgw_Cart>
        </modules>
        <blocks>
            <mgw_Cart>
                <class>mgw_Cart_Block_Modal_Cart</class>
            </mgw_Cart>
        </blocks>
        <helpers>
            <cart>
                <class>mgw_Cart_Helper</class>
            </cart>
        </helpers>
    </global>
</config>

新建local.xmlapp\design\frontend\rwd\default\layout\local.xml

<layout version="0.1.0">
    <default>
        <reference name="after_body_start">
            <block type="cart/modal_cart"/>
        </reference>
    </default>
</layout>

模块 xml app\etc\modules\mgw_Cart.xml

<config>
    <modules>
        <mgw_Cart>
            <active>true</active>
            <codePool>local</codePool>
            <depends />
        </mgw_Cart>
    </modules>
</config>

现在我的模板无法加载。我检查了管理员以查看我的模块是否正在加载并且已列出。那么,为什么我的模板无法加载?

我的问题是:

我是 Magento 的新手,我可以帮助您解决问题。 首先,我建议为您的自定义模块和文件使用清晰简单的命名法,"ModalCart.php" 可以重命名为 "Modalcart.php",避免任何引用问题。

如果你想 extend/override 一个法师 class 你需要在你的模块的 config.xml 中指定它,像这样:

<blocks>
    <checkout>
        <rewrite>
            <cart_sidebar>PkgName_ModuleName_Block_YourClassThatOverrides</cart_sidebar>
        </rewrite>
    </checkout>
</blocks>

在上面的代码中,您声明要用新的 class.

重写 checkout/cart_sidebar 块

这就是 config.xml,现在您要创建覆盖的 class。在模块的 Block 目录中创建 class 文件 .php 即 extends/rewrite 核心 class:

<?php
  class PkgName_ModuleName_Block_YourClassThatOverrides extends Mage_Checkout_Block_Cart_Sidebar {
      // check for the methods to rewrite or create new methods
  }

以及关于如何设置特定模板或布局的最后一个问题: 在开始编码之前,我个人的建议是从不同的来源研究同一个东西 3 到 4 次,然后编码 3 到 4 次,直到你能理解并记住所有内容。 所以对于 layout/template 部分,我建议阅读这个 Alan Storm 线程: https://alanstorm.com/layouts_blocks_and_templates/