3

I would like to add a new block in the customer registration, edit and billing forms. This block will contain a dropdown control where the customer can select if he wants to register as a company or as an individual.

At first, I edited billing.phtml and added the following:

<li class="fields">
    <?php echo $this->getLayout()->createBlock('core/template')->setTemplate('mynamespace/mymodule/customer/widget/registeras.phtml')->toHtml(); ?>
</li>

This is working fine, but instead of using 'core/template', I would like to create my custom typed block.

I wrote this in the config:

<config>
    <modules>
        <Mynamespace_Mymodule>
            <version>0.0.1</version>
        </Mynamespace_Mymodule>
    </modules>
    <global>
        <blocks>
            <mymodule>
                <class>Mynamespace_Mymodule_Block_Widget_Registeras</class>
            </mymodule>
        </blocks>
        ...
    </global>
</config>

Then I created the class of my block:

class Mynamespace_Mymodule_Block_Widget_Registeras extends Mage_Customer_Block_Widget_Abstract
{
    public function _construct()
    {
        parent::_construct();

        // default template location
        $this->setTemplate('mynamespace/mymodule/customer/widget/registeras.phtml');
    }

    /**
     * Get current customer from session
     *
     * @return Mage_Customer_Model_Customer
     */
    public function getCustomer()
    {
        return Mage::getSingleton('customer/session')->getCustomer();
    }
}

Now I replaced:

$this->getLayout()->createBlock('core/template')->setTemplate('mynamespace/mymodule/customer/widget/registeras.phtml')->toHtml(); ?>

by this:

<?php echo $this->getLayout()->createBlock('mynamespace_mymodule/widget_registeras')->setObject($this->getQuote()->getCustomer())->toHtml(); ?>

But it's not working. Here is the error I get:

exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_Mynamespace_Mymodule_Block_Widget_Registeras' in E:\myworkspace\magento\app\Mage.php:595 Stack trace: 0 E:\myworkspace\magento\app\code\core\Mage\Core\Model\Layout.php(495): Mage::throwException('Invalid block t...') 1 E:\myworkspace\magento\app\code\core\Mage\Core\Model\Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('mynamespace_my...', Array) 2 E:\myworkspace\magento\app\code\core\Mage\Core\Model\Layout.php(472): Mage_Core_Model_Layout->createBlock('mynamespace_my...', 'widget_register...') ...

I could not understand why magento adds 'Mage_' to the class name.

I also tried to load it using the layout and getChildHtml() but I could not get it work better.

Any help would be appreciated.

Thank you

2 Answers 2

0

Use this

<?php echo $this->getLayout()->createBlock('mymodule/widget_registeras')->setObject($this->getQuote()->getCustomer())->toHtml(); ?>

Hope this will solve your problem!

1
  • Thank you but it is not working. I got the same error as above ('Mage_' is added at the beginning of the class, I guess because billing.phtm is a mage block..) + I get a second error: exception 'Mage_Core_Exception' with message 'Invalid block type: Mynamespace_Mymodule_Block_Widget_Registeras_Widget_Registeras' => this time 'Mage_' is not added but '_Widget_Registeras' is written twice.. Strange!
    – max
    Commented Apr 8, 2016 at 11:54
0

Typically Magento will prepend "Mage_" if you don't have your <blocks> set up quite right in the config.xml.

Try replacing this...

    <blocks>
        <mymodule>
            <class>Mynamespace_Mymodule_Block_Widget_Registeras</class>
        </mymodule>
    </blocks>

with this...

    <blocks>
        <mymodule>
            <class>Mynamespace_Mymodule_Block</class>
        </mymodule>
    </blocks>

Also, when you are calling createBlock() you will use just "mymodule/widget_registeras" like Asish Hira says. The first part of that before the slash / will reference the <mymodule> you set up inside your <blocks> of the config.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.