How to Edit Packing Slips in Magento?

The default packing slip that comes in Magento is brief. However there are certain methods that can be used to make packing slips elaborate and attractive. Here is how it can be done.

For editing the packing slips in Magento, we need to override the files in model and controller folder.

1.Access the config.xml file from app\code\local\Namespace\Modulename\etc\ config.xml and use the below code.
For Model Files:

<sales>
<rewrite>
<order_pdf_shipment>
Namespace_Modlulename_Model_Order_Pdf_Shipment
</order_pdf_shipment>
<order_pdf_abstract>
Namespace_Modlulename_Model_Order_Pdf_Abstract
</order_pdf_abstract>
</rewrite>
</sales>

For controllers Files:

</admin>
</routers>
</adminhtml>
</use>admin</use>
</args>
</modules>
</Namespace_ModuleName before=”Mage_Adminhtml”>Namespace_ModuleName_Adminhtml</Namespace_ModuleName>
</modules>
</args>
</adminhtml>
</routers>
</admin>

Packing Slip

Then, we will have to override the OrderController.php file, which can be accessed from the below path
app\code\core\Mage\Adminhtml\controllers\Sales\OrderController.php

Read: How To Add Or Edit Top Links Sections In Magento

Frontend controller:

2. Now you will have to create a controller file called filename.php. This file should be created in
app\code\local\Namespace\Modulename\controllers\OrderController.php

Here write pdfshipAction() and call the getPdf() function.

public function pdfshipAction() {
//This is not an increment ID. It is just an order id.
$orderId = $this->getRequest()->getParam(‘order_id’);
$flag = false;
if (!empty($orderId)) {
$shipments = Mage::getResourceModel(‘sales/order_shipment_collection’)
->setOrderFilter($orderId)
->load();
if ($shipments->getSize()) {
$flag = true;
if (!isset($pdf)){
$pdf = Mage::getModel(‘modulename/order_pdf_shipment’)->getPdf($shipments);
} else {
$pages = Mage::getModel(‘modulename/order_pdf_shipment’)->getPdf($shipments);
$pdf->pages = array_merge ($pdf->pages, $pages->pages);
}
}
if ($flag) {
return $this->_prepareDownloadResponse(
‘packingslip’.Mage::getSingleton(‘core/date’)->date(‘Y-m-d_H-i-s’).’.pdf’, $pdf->render(),
‘application/pdf’
);
} else {
Mage::getSingleton(‘core/session’)->addError($this->__(‘There are no printable documents related to selected orders.’));
$this->_redirect(‘*/order/manage’);
}
}
$this->_redirect(‘*/order/manage’);
$shipmentId = $this->getRequest()->getParam(‘shipmentId’);
if (!empty($shipmentId)) {
$shipment = Mage::getModel(‘sales/order_shipment’)->load($shipmentId);
$shipmentData = $shipment->getData();
if (!empty($shipmentData)) {
$pdf = Mage::getModel(‘sales/order_pdf_shipment’)->getPdf(array($shipment));
$fileName = ‘-PackingSlip.pdf’;
$PakingSlip = date(“d-m-Y”).$fileName;
return $this->_prepareDownloadResponse($PakingSlip, $pdf->render(),’application/pdf’);
}else {
Mage::getSingleton(‘core/session’)->addError($this->__(‘There are no printable documents related to selected orders.’));
$this->_redirect(‘*/order/manage’);
}
}
else{
Mage::getSingleton(‘core/session’)->addError($this->__(‘At least one order must be selected’));
}
$this->_redirect(‘*/order/manage’);
}

Admin controller:

Create an OrderController.php file in app\code\local\Namespace\ModuleName\controllers\Adminhtml\Sales\OrderController.php.

Here change the order_pdf_shipment Module name to your Module name.

require_once ‘Mage/Adminhtml/controllers/Sales/OrderController.php’;
class Namespace_Modulename_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Controller_Action {
public function pdfshipmentsAction(){
/**
For a better readability the remaining code has not been shown.
**/
$pdf = Mage::getModel(‘ModuleName/order_pdf_shipment’)->getPdf($shipments);
} }

3. Now access the Abstract.php file from the path below: app\code\local\Namespace\Modulename\Model\Order\Pdf\Abstract.php and edit the insertOrder() function as

protected function insertOrder(&$page, $obj, $putOrderId = true)
{
/**
Here you need to write the syntax for giving details such as the shipping address, billing address, payment methods and lot more.
**/
}

4. Then access the Shipment.php file from
app\code\local\Namespace\Modulename\Model\Order\Pdf\Shipment.php.

In the getPdf() function you should mention the details that you want to display in the packing slips.

public function getPdf($shipments = array()){
/**
Here you need to write the syntax for editing the product details, subtotal, order total, etc.
**/
}

To make your packing slip informative and attractive you can incorporate an appropriate function.

Given below is a function for sample purpose.

drawRectangle($x1, $y1, $x2, $y2,$fillType=Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
Class Zend_Pdf_Page ->provides a set of drawing primitives
SHAPE_DRAW_FILL_AND_STROKE ->fill rectangle and stroke (default))
Use drawText($text, $x, $y, $charEncoding = ‘ ‘); -> To write some text in the specified
Use setFont(Zend_Pdf_Resource_Font $font, $fontSize); -> To set Font
Use $color1 = new Zend_Pdf_Color_Html(‘#3366FF’); -> To set color

Finally to print the packing slip
Click Sale->order ->Select any order (the order should have been completed) ->Select Print packing slips in Action.

By implementing the above procedure you can edit the Magento packing slips successfully. In case you face any problems while editing the Magento packing slips, get in touch with us.

Realated Posts

2 thoughts on “How to Edit Packing Slips in Magento?

  1. Ashish Nayyar

    Great write-up, exactly to the point and help create packing slips in few minutes.

    • ramanathan

      Hi Ashish,

      It’s great to hear from you. Also check out other technical stuffs too.

      Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">