4

How to create custom address renderer with the following template in one line {{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}

I can't change the native address templates, because they are used for order view pages, pdf, etc.

When i try

$renderer = Mage::helper('customer/address')->getRenderer(Mage_Customer_Model_Address_Config::DEFAULT_ADDRESS_RENDERER);
$renderer->render($address, $format);

i catch Fatal error: Call to a member function getCode() on a non-object in <b>app/code/core/Mage/Customer/Block/Address/Renderer/Default.php on line 85

3

You need to specify renderer type one of:

const OUTPUT_FORMAT_JSON    = 'json';
const OUTPUT_FORMAT_TEXT    = 'text';
const OUTPUT_FORMAT_HTML    = 'html';
const OUTPUT_FORMAT_PDF     = 'pdf';
const OUTPUT_FORMAT_ONELINE = 'oneline';

Use helper below:

<?php

class Your_Module_Helper_Address extends Mage_Core_Helper_Abstract
{
    const ADDRESS_FORMAT = '{{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}';

    public function render($address, $format = null)
    {
        if ($format === null) {
            $format = self::ADDRESS_FORMAT;
        }
        return $this->getRenderer()->render($this->getOrder()->getBillingAddress(), $format);
    }

    /**
     * @return Mage_Customer_Block_Address_Renderer_Default
     */
    protected function _getRenderer()
    {
        $renderer = Mage::helper('customer/address')->getRenderer(Mage_Customer_Model_Address_Config::DEFAULT_ADDRESS_RENDERER);
        $renderer->setType($this->getType());
        return $renderer;
    }

    /**
     * @return Varien_Object
     */
    protected function _getType()
    {
        $type = new Varien_Object(
            array(
                'code' => Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE,
            )
        );
        return $type;
    }
}

Usage Mage::helper('your_module/address')->render($address, $format).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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