Edit PDF address in Magento

Edit PDF address in Magento

Editing PDF in Magento can be restrictive but if you need to edit just an address you can use events.

First set up your observer in config.xml:

<adminhtml>
    	<events>
    		<customer_address_format>
    			<observers>
                    <inchoo_sales_customer_address_format_observer>
                        <type>model</type>
                        <class>inchoo_sales/observer</class>
                        <method>addAdditionalDataToAddress</method>
                    </inchoo_sales_customer_address_format_observer>
                </observers>
    		</customer_address_format>
    	</events>
</adminhtml>

After obsever is set you need to set in code and edit address templates:

/**
 * Observer printing invoices in PDF
 *
 * @category    Inchoo
 * @package     Inchoo_Sales
 */
class Inchoo_Sales_Model_Observer
{
    public function addAdditionalDataToAddress(Varien_Event_Observer $address)
    {
    	$data = $address->getEvent();
    	if($data->type['code']=="pdf")
    	{
    		$customerData = $data["address"]->getOrder()->getData();
    		$customerId = $customerData['customer_id'];
    		//we are using customer object because it give us latest user data, if you want data from user on order creation use $customerData
			$customer = Mage::getModel('customer/customer')->load($customerId);
			$customerVariable = $customer->get?????;
 
			//prevent of multiple insertion
    		if(strpos($data->type['default_format'],"Customer Variable")===false)
    		{
    		 	$stringToInsert = "{{var company}}|{{/depend}}|Customer Variable: ".$customerVariable."|";
    		 	$data->type['default_format'] = str_replace("{{var company}}|{{/depend}}",$stringToInsert, $data->type['default_format']);
    		};
    	};
    }
}

If you use customer from order in PDF you get state from user in order state, if you wont get latest user data you have to use Mage::getModel(‘customer/customer’) object.
Replace ????? with your own data.

We are fetching address template and changing that template.

Happy coding,
Goran:)

You made it all the way down here so you must have enjoyed this post! You may also like:

Calling out all Magento developers who want to improve their testing skills – visit MageTestFest! Maja Kardum
Maja Kardum, | 0

Calling out all Magento developers who want to improve their testing skills – visit MageTestFest!

Meet Magento Croatia 2018 recap with presentations and photos Maja Kardum
Maja Kardum, | 0

Meet Magento Croatia 2018 recap with presentations and photos

Victoria Yashchuk from Atwix is coming to #MM18HR stage and is ready to discuss “Latest eCommerce trends and popular features for your Magento store” Borna Kraljevic
Borna Kraljevic, | 0

Victoria Yashchuk from Atwix is coming to #MM18HR stage and is ready to discuss “Latest eCommerce trends and popular features for your Magento store”

3 comments

  1. Nice post! I’ve tried to change the file /app/code/core/Mage/Customer/etc/config.xml and change the address formats directly on that file, but without success. I would like to put the second address line besides 1st, how can I do this? Can be a solution editing core files, no problem, because I’ll never will do an update on the store I’m working it.

    Thanks for help.

  2. I know this post is a little bit old, but i use a very good Magento Invoice Extension. With this extension you are able to easily change the customer address format, by drag and drop. The Templates are completely customizable and easy to use. Our designer loves this Invoice Extension and creates incredible PDFs. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.