Add gift message to Magento’s PDF packingslip

Add gift message to Magento’s PDF packingslip

To be honest, I have never been working with gift messages in Magento, so I got very surprised once I saw that there is no output for it in any
document that customer recieves. This time I got request from client to write gift message on PDF packingslip.
Actually this is somewhat easy and goes like this…

1) Rewrite this class: Mage_Sales_Model_Order_Pdf_Shipment
Put this in your config.xml

<config>
    <global>
        <models>
            <sales>
                <rewrite>
                    <order_pdf_shipment>
                        Yourfirm_Yourmodulename_Model_Order_Pdf_Shipment
                    </order_pdf_shipment>
                </rewrite>
            </sales>
        </models>
    </global>
</config>

Put file Shipment.php inside your module directory following the same directory structure like original file.
Class should look like this:

Yourfirm_Yourmodulename_Model_Order_Pdf_Shipment extends Mage_Sales_Model_Order_Pdf_Shipment  
{
// code from original class (I have removed it for readability purposes)
 
//add this method 
public function addGiftMsg($page, $giftMessageSender, $giftMessageNote) 
    {
    	if(empty($giftMessageNote)) {
			return;
		}
		$pipfmText = $giftMessageSender ."***BREAK***"."  "."***BREAK***".wordwrap($giftMessageNote, 100, "***BREAK***", true);
 
		$pipfmTextLines = array();
		$pipfmTextLines = explode("***BREAK***", $pipfmText);
 
 
 
		$i = 0;
 
		$pipfmTextLineStartY = 300;
		foreach ($pipfmTextLines as $pipfmTextLine) 
		{
			$i ++;
 
			//Bold only the first line
			if($i == 1) 
			{
				$this->_setFontBold_Modified($page, 10);
 
 
			} else {
				$this->_setFontRegular($page, 10);
			}
 
			$page->drawText($pipfmTextLine, 60, $pipfmTextLineStartY, 'UTF-8');
			$pipfmTextLineStartY = $pipfmTextLineStartY - 10;	
 
		}
    }
 
// at some place where you want output (inside getPdf() method) for it just drop this lines:
public function getPdf($shipments = array())
 {
       // ORIGINAL CODE REMOVED FOR READABILITY PURPOSES
 
       $giftMessage = Mage::getModel("giftmessage/message")->load($order->getGiftMessageId());
       $giftMessageSender ="Message from ".$giftMessage->getSender().':';
       $giftMessageNote = $giftMessage->getMessage();
       $this->addGiftMsg($page, $giftMessageSender, $giftMessageNote);
}

Please do not use this on production without testing it first.
If you have any questions, post it here, but please note that since this is not complete module, you will need to know some Magento basics to get it work.
I hope someone can find it useful.

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

Making FedEx api show shipping estimate Toni Pap
Toni Pap, | 1

Making FedEx api show shipping estimate

When can you deliver? (FedEx) Davor Simek
Davor Simek, | 8

When can you deliver? (FedEx)

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there

13 comments

  1. $this->_setFontBold_Modified needs to be changed to $this->_setFontBold as of Mage 1.8

    Other than this works great

  2. Sorry, i have resolved the problem! I was working on a magento that already has overring that model!
    Sorry for loosing time!
    Bye
    Iacopo

  3. I have a question. I’m tring to override the model Mage_Sales_Model_Order_Pdf_Shipment but my class that extends i i not called.

    In the example above the new model (Yourfirm_Yourmodulename_Model_Order_Pdf_Shipment) is called every time the father model is colled or we can do manually? that is Mage::getModel(‘sales/order_pdf_shipment’) or Mage::getModel(‘Yourfirm_Yourmodulename_Model_Order_Pdf_Shipment’)?.

    Thank you
    Iacopo

  4. Can the same practices be applied to adding gift messages to INVOICES?

    I tried modifying the above instructions to add the gift messages to invoices (since you can’t print packing slips until the order is marked as ‘complete’), but I only got as far as causing fatal errors upon attempting to print the invoice.

    (I did see you great post on printing custom attributes on invoices, but it didn’t seem as if that information could be applied to making gift messages display on the invoice).

    On a side note: THANK YOU for all of these great posts. I’m not super-comfortable with Magento (yet) and sometimes Magento makes me want to either A) weep softly or B) knock my brain against a wall.

  5. @Prajosh

    Do you mean this extension or any extension in general?
    Well, to shut down this one, all you need to do is to go to: /app/etc/modules/Yourfirm_Yourmodulename.xml and set active directive to false. Then you can safely remove /app/code/local/Yourfirm/Yourmodulename directory. Same procedure can be applied for every other extensions, however, if you don’t know what extension actually does to your system, maybe that’s not very good idea since there are lot of extensions that modify template files directly -.-, not to mention those that are making quite a mess in your database in most weird way you can imagine, but in general, you should be able to do what I described, but be careful and make backup of everything you change. I hope it helped!

  6. Great blog guys!

    How would I add more detail to this? For example a table with some custom attributes filled out? I would like to integrate a CN22 or customs declaration form onto my packing slips, rather than having to fill one out separately? Just fields like weight, and a jpeg of a signature…

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.