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.
13 comments
Hello. Can you please make a guide on how to achieve this for Magento 2.2.6?
Thanks,
Andy
How do you achieve this in Magento 2.2?
$this->_setFontBold_Modified needs to be changed to $this->_setFontBold as of Mage 1.8
Other than this works great
magento razoyo module gift wrap message does not show in Order Package slip PDF
Well, maybe you should consider 4 years since this article?
Sorry, i have resolved the problem! I was working on a magento that already has overring that model!
Sorry for loosing time!
Bye
Iacopo
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
Tomas,
Do you guys have an updated tutorial for creating simple modules (such as the above)? I could only find this in your site:
https://inchoo.net/ecommerce/magento/magento-custom-model-with-custom-validation-rules/
(found few other tutorials that were old…)
Thanks you guy for the good posts!
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.
@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!
Great article!
btw do u know how to remove an extension from magento manually ?
thanks
prajosh
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…
Thanks for the clean example Tomas. Yes, there is indeed a lot that one would expect as built in but then we discover that it is not.