Skip to content

Customising Magento PDF Invoices to show personalised terms of sale

Magento is, as everyone knows, an ecommerce platform for the web. But it can also be used as a tool to tie your offline sales to your online sales. I’ve recently been working with a traditional “offline” retailer based in near Bath who have mainly sold to business customers – the standard Business-to-business (B2B) model. Typically in this environment, individual customers may have different payment terms that would be shown on invoices.

This presents a problem for Magento, as it’s not really geared up for this sort of situation. So I needed to make a few modifications so my client could service both sales to the general public (B2C) and to their business customers through Magento.

The first step was modifying the Customer model to add a new field to each customer record. I added a  Customer Terms textarea field to the Customer admin panel, so the client could enter the specific terms of sale for each customer.

Then we needed to modify the Invoice PDFs to pull this data out of the Customer object and into the PDF. It’s easiest to do this by duplicating the Mage_Sales_Model_Order_Pdf_Invoice class in to the /app/code/local folder and adding a simple method call


$page = $this->insertCustomerTerms($page, $order);

Duplicating the Mage_Sales_Model_Order_Pdf_Abstract class into /app/code/local and adding this utility method seemed to make most sense. Essentially all it needs to do is find the customer, get the contents of my Customer Terms field, and add it to the PDF. The only issue I came up against was that the string was typically longer than a single line, and Zend_Pdf sadly doesn’t support automatic line wrapping (yet). Some psuedo-ish code is shown below…


protected function insertCustomerTerms(&$page, $order, $store = null)
    {

        /* @var $customer Mage_Customer_Model_Customer */
        $customer = Mage::getModel("customer/customer")->load( $order->getCustomerId() );
        $terms = $customer->getCustomerterms();

        if( strlen($terms) < 10 ){
            return;
        }

        //Convert terms to array - each element is a line. Utility method not shown...
        $terms = $this->_formatTerms($terms);

        foreach ($terms as $term) {
            // Write each line of terms to $page
        }
    }

Looking for a Freelance Magento ecommerce developer to help you with your website? We’re experienced web developers based in Bradford on Avon. Within easy commuting distance of Bath and Bristol, we can work onsite in your offices, or remotely. We’d love to help you, so please get in contact or give us a call on 01380 830224…

This entry was posted in Magento and tagged , , , , . Bookmark the permalink.

Comments

Sorry, comments are closed on this page.