Home > Magento > Adding Customer Comments on Invoice PDFs in Magento (using OneStepCheckout)

Adding Customer Comments on Invoice PDFs in Magento (using OneStepCheckout)

January 18th, 2010

I’ve recently installed OneStepCheckout (http://www.onestepcheckout.com/) on a couple of Magento installations. The extension is very nice, really simple to integrate and I expect to see better conversion rates on the checkout process.

One cool thing is that it comes with the option of activating Customer Order Comments – it adds a textarea field on the checkout page, and a box with the customer comments in the admin, when viewing the order.

However, one of my clients requested I added these comments in the invoice PDF’s. So, here’s how to do it:

Step 1
Copy app/code/core/Mage/Sales/Model/Order/Pdf/Invoice.php to app/code/local/Mage/Sales/Model/Order/Pdf/Invoice.php

Step 2
Open the new file and create a new method:

	function insertOscComments(&$page, $order) {
		if( !$order->getOnestepcheckoutCustomercomment() ) { return; }
		$this->y -= 20;
		$page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
		$page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
		$page->setLineWidth(0.5);
		$page->drawRectangle(25, $this->y, 570, $this->y - 20);
		$page->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 1));
		$page->drawRectangle(25, $this->y - 20, 570, $this->y - 40);
		
		$page->setFillColor(new Zend_Pdf_Color_RGB(0.1, 0.1, 0.1));
		$page->drawText(Mage::helper('onestepcheckout')->__('Customer Comments'), 35, $this->y - 13, 'UTF-8');
		$page->drawText($order->getOnestepcheckoutCustomercomment(), 33, $this->y - 33, 'UTF-8');
		$this->y -= 50;
	}

Step 3
At the end of method getPdf add a call to the new method you created:

  /* Add totals */
  $this->insertTotals($page, $invoice);

  /* Add OneStepCheckout Customer Comments */
  $this->insertOscComments($page, $order);
}

And that’s all you need.

  1. | #1

    hey there Bruno,
    this was very helpful and a good idea adding comments on an inovice .

    Thanks for it .
    Greetings,
    Valentin

  2. Cristi
    | #2

    yves, i don’t think this has anything to do with the magento version. i remember having this for a while and it never broke during upgrades, as long as I updated the Invoice.php file to match the new one. I re-read the article now (it’s been more than 5 years since I wrote this) and my recommendation is to do it slightly different. I’d rather use rewrites now than drop-in replacements. In other words, I’d create a new local extension in which I would rewrite Mage_Sales_Model_Order_Pdf_Invoice and then in `getPdf` i would call parent::getPdf(); $this->getOscComments();

    another suggestion would be to use a plugin (especially if you don’t like the way the default pdf’s look). I’m using Fooman Pdf Customizer and it’s really awesome – it works with OSC out of the box (plus a few others), it makes the invoices look cool, it has a ton of useful features and it’s easy to customize it.

  3. | #3

    Hello,

    I’m looking to add the customer comment on the invoice PDF of magento ver. 1.7.0.2.
    I’m following the code on this page, but it does’nt work. Problem with the current version?

    The pdf is ok, but the comment does’nt appear.

    Is there another way to add the comment?

    Regards,

  4. Cristi
    | #4

    true, rewriting the model would be much safer and cleaner. copying files to local is a quick solution though. dirty, but quick.

  5. Dave Wilson
    | #5

    Don’t copy core files to local! Use a module with a rewrite instead.

  6. | #6

    Nice tutorial for freshers to start magento extension development, well explained and documented.

  7. | #7

    Please how to add background image in facture pdf

    I do this but it doesn’t work

    protected function insertLogo(&$page, $store = null)
    {
    $image = Mage::getStoreConfig(‘sales/identity/logo’, $store);
    $image1 = Mage::getStoreConfig(‘sales/identity/logo’, $store);
    if ($image&&$image1) {
    $image = Mage::getStoreConfig(‘system/filesystem/media’, $store) . ‘/sales/store/logo/’ . $image;
    $image1 = Mage::getStoreConfig(‘system/filesystem/media’, $store) . ‘/sales/store/logo/’ . $image1;
    if (is_file($image) && is_file($image1)) {
    $image = Zend_Pdf_Image::imageWithPath($image);
    $image1 = Zend_Pdf_Image::imageWithPath($image1);
    //$page->drawImage($image, 35, 800, 125, 825);
    $page->drawImage($image, 60, 800, 125, 825);
    $page->drawImage($image1, 100, 600, 600, 600);
    //$page->drawImage($image, 500, -60, 600, 40);
    }
    }
    }

  8. Luke
    | #8

    Great post.

    If it helps you can call OneStepCheckout into a custom invoice e.g. .phtml just add:
    getOrder();?>
    getOnestepcheckoutCustomercomment() ?>

  9. emon
    | #9

    hey thanks for the posts. Really helpful.

  10. | #10

    Thank you very much. we go it working great!

  11. Airbox
    | #11

    thanks christian! we got it working great!

    if you or any of your users are interested, here is the code to display it on the order email templates:

    Order Comments:

    {{var order.getOnestepcheckoutCustomercomment()}}

  12. Cristi
    | #12

    Here are a few things to check:
    – place an order and add customer comments – i’m assuming they’re enabled from the admin
    – check the order in the admin and make sure they appear on the view order page
    – if they don’t, check the database to see if the comments get stored
    – if they do, see if the field is still named “onestepcheckout_customercomments” – it really important, cause right now you’re probably calling $order->getOnestepcheckoutCustomerComments()
    – if everything so far looks good, but the comments still don’t show up on the invoice, add a print_R($order); die(); and try printing an invoice – check the dump and see if the comments are in there.
    – if they are, it’s a pdf/spooling problem – most probably a color or coordinate issue

  13. Airbox
    | #13

    No error message, just nothing being displayed after the order totals. You are correct, it looks like nothing has changed since the 1.3 versions of magento, but maybe the module for OneStepCheckout??? I am happy to contact you via your contact form on the site if you like i can send you the file that we created and you can take a look at if you re able.

  14. Cristi
    | #14

    This was developed on Mage 1.3.x, but I don’t see any major differences between Invoice.php v1.3 and v.1.4. insertOscComments should stay the same and should work and the call to it should go after insert totals.
    What’s the error message saying?

  15. Airbox
    | #15

    thank you for this great article! we have tried to implement this solution for one of our clients as well, but have not been able to get it to work, do you know if this will work on 1.4.0.1? if so would it be possible for you to post your full working invoice.php file so that we can compare ours to see if we have made a mistake?

  16. | #16

    hey thanks,

    this has worked and helped me a lot to print a new field in invoice pdf

    but one thing I modified was function insertOscComments(&$page, $order) to function insertOscComments($page, $order).
    no ‘&’ in the first argument 😀

  17. | #17

    It’s not there by default, but I think it’s a failure from Magento… There are extensions that add it to the Order page, like:

    http://cl.ly/1gGT

    and in my Abstract.php page I did this:

    http://pastebin.com/JHsjRaEs

    It’s easy to change the 3 lines into infinite lines, just by refactoring the code, when the client pays for it, I will do it and place in the same link the correct code 🙂

    In the spirit of sharing open source, I hope this helps someone.

    Once again Chris, Thank you for the first push as I had no idea where to start 🙂

  18. Cristi
    | #18

    I can’t seen to find the print button/action, but I assume that it should be the same thing as with the invoice. As far as I remember, at some point one was able to print an order to pdf or html. For both of them those customer comments can be added. In html is simpler and it’s almost identical to editing sales/order/view/tab/info.phtml. For the pdf, it shouldn’t be harder than adding the code from the invoice pdf. This is just the top of my head, so I might be wrong 🙂

  19. | #19

    Hi Cristi,

    Thank you for the post, but I wonder, how would you add the comments to the “Print Order” instead being specific an “Invoice”?

    Thank you.

  20. Cristi
    | #20

    The basic idea is to see how many characters the text has. So, you would first strip unwanted stuff (html, line breaks) and then do a quick check for the lenght of your text – there’s a method for estimating the lenght of a string, widthForStringUsingFontSize (defined in Mage_Sales_Model_Order_Pdf_Abstract). I think you could use this to see how long the string is and split it on several lines – don’t forget to also update the y coordinate ($this->y).
    There are, of course, other ways of fixing – for example, decreasing the font size or decreasing the font size and splitting it on multiple lines, changing the line height and so on. Hope this helps.

  21. Costa
    | #21

    Hello,

    Thanks for posting this.
    I found one problem, when the comment is long , say 2-3 sentences, then it does not display correctly, basically it does not wrap around, and the comment gets cut off after one line, how do i fix this? thanks

  1. No trackbacks yet.