简体   繁体   中英

Magento Block not Loading

I'm building my first module in Magento and have a couple question related to the process.

Before I attempted a module, I just had a template and was loading it into app\\design\\frontend\\rwd\\default\\layout\\local.xml with this code

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

Life was good and everything worked until I decided I need to extend the Mage_Checkout_Block_Cart_Sidebar class.

So I create a Module to do so. Here is my code.

Block 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>

New local.xml app\\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>

Module xml app\\etc\\modules\\mgw_Cart.xml

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

Now my template won't load. I've checked the Admin to see if my Module is loading and it is listed. So why won't my template load?

My questions are:

  • Can I even extend a core block like I'm trying?
  • How can I get my Block/Template to load?

I am new to Magento and i can help you with your problem. First of all i would recommend to use a clear and simply nomenclature for your custom modules and files, "ModalCart.php" can be renamed as "Modalcart.php", avoiding any reference problem.

If you want to extend/override a Mage class you need to specify it in the config.xml of your module, like this:

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

in the above code you are declaring that you are going to rewrite the checkout/cart_sidebar block with your new class.

So this was the config.xml, now you are going to create the class that overrides. In your module's Block directory create the class file .php that extends/rewrite the core class:

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

and for your last question about how to set a specific Template or Layout: Before start coding my personal advice is to study the same thing 3 or 4 times from different sources and then code it for 3 or 4 times untill you can understand and memorize all the content. So for the layout/template section i suggest to read this Alan Storm thread: https://alanstorm.com/layouts_blocks_and_templates/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM