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. Costa
    June 28th, 2010 at 08:18 | #1

    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

  2. Cristi
    June 28th, 2010 at 16:49 | #2

    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.

  3. July 9th, 2010 at 11:40 | #3

    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.

  4. Cristi
    July 9th, 2010 at 13:37 | #4

    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 :)

  5. July 17th, 2010 at 10:16 | #5

    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 :)

  6. October 21st, 2010 at 11:54 | #6

    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 :D

  7. Airbox
    November 3rd, 2010 at 00:48 | #7

    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?

  8. Cristi
    November 3rd, 2010 at 10:40 | #8

    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?

  9. Airbox
    November 4th, 2010 at 03:52 | #9

    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.

  10. Cristi
    November 4th, 2010 at 12:27 | #10

    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

  11. Airbox
    November 5th, 2010 at 02:12 | #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. May 20th, 2011 at 05:41 | #12

    Thank you very much. we go it working great!

  13. emon
    September 23rd, 2011 at 18:42 | #13

    hey thanks for the posts. Really helpful.

  14. Luke
    August 28th, 2012 at 18:22 | #14

    Great post.

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

  15. December 15th, 2012 at 09:11 | #15

    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);
    }
    }
    }

  16. September 18th, 2013 at 05:00 | #16

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

  1. No trackbacks yet.