Adding Custom Information to Magento Packing Slip PDF
Magento provides customizable packing slips for managing your shipments efficiently. If you’re using integrated label paper, you might want to add additional information like the customer’s delivery address or order totals directly onto the packing slip PDF. Here’s a step-by-step guide on how to achieve this.
Understanding Magento Packing Slip PDF Structure
Magento uses the Mage_Sales_Model_Order_Pdf class to generate the packing slip PDF. The key methods involved in this process are:
getPdf(): This method initializes and returns the packing slip PDF.drawHeader($pdf): Draws the header of the packing slip, which includes the store name and date.drawPackingSlip($senderInfo, $items, $page, $order, $isEmailAttach = false): Draws the main content of the packing slip, including sender information and order items.
Modifying the Packing Slip Footer
To add additional information to the footer of your packing slip PDF, you need to modify the drawFooter($pdf) method located in app/code/core/Mage/Sales/Model/Order/Pdf.php. Here’s how:
- Open the file
app/code/core/Mage/Sales/Model/Order/Pdf.phpand find thedrawFooter($page)function. - Add your custom information after this line:
$page->setFooter( array('text' => $page->getStoreConfig('sales/PDF/invoice_footer'), 'font_size' => 10, 'font' => 'regular', 'color' => array(0, 0, 0)) ); - For example, you can add the customer’s delivery address like this:
$page->drawText($order->getShippingAddress()->getName()."\n".$order->getShippingAddress()->getStreet1().", ".$order->getShippingAddress()->getCity(), 250, $y, 'UTF-8');
- Save the file and clear your cache.
Calculating Total Weight for the Entire Shipment
To calculate the total weight of all items within a single shipment, you need to modify the drawItems($items, $page) function in the same file. Here’s how:
- Inside this function, initialize a variable
$totalWeight = 0;at the beginning. - For each item in the loop, add its calculated weight to
$totalWeight:
$itemWeight = $item->getWeight() * $item->getQty();
$itemWeight += $item->getWeight() * $item->getParentItem()->getQty();
$totalWeight += $itemWeight;
- After the loop, add a line to draw the total weight on the packing slip:
$page->drawText("Total Weight: ".$totalWeight." kg", 250, $y, 'UTF-8');
- Save the file and clear your cache.
Conclusion
By following these steps, you can easily add custom information to both the footer and the main content of your Magento packing slip PDF. This allows you to include additional details like delivery addresses or order totals directly on the label for better organization and efficiency in your shipping process. As always, remember to test your changes thoroughly before deploying them to a production environment.
For more advanced customizations and optimization techniques, I recommend checking out our guide on Magento Performance Optimization on the Laravel Company blog.