How to add custom product attribute in PDF invoice Magento

i am trying to add custom attribute , product(book) publisher name below product name in PDF invoice(like in image)& publisher is custom attribute created by me by copy app/code/core/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php to app/code/local/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php with following code in default.php but this not working

 

 

class Mage_Sales_Model_Order_Pdf_Items_Invoice_Default extends Mage_Sales_Model_Order_Pdf_Items_Abstract
{
    /**
     * Draw item line
     */
    public function draw()
    {
        $order  = $this->getOrder();
        $item   = $this->getItem();
        $pdf    = $this->getPdf();
        $page   = $this->getPage();
        $lines  = array();
        $Publisher = $this->getPublisherValue($item);


        // draw Product name
        $lines[0] = array(array(
           'text' => Mage::helper('core/string')->str_split($item->getName(), 35, true, true),
            'feed' => 35,
        ));

        // draw publisher name
          $lines[1][] = array(
            'text'  => Mage::helper('core/string')->str_split($Publisher, 35),
            'feed'  => 35
        );



        // draw SKU
        $lines[0][] = array(
           'text'  => Mage::helper('core/string')->str_split($this->getSku($item), 17),
           'feed'  => 290,
            'align' => 'right'
        );

        // draw QTY
        $lines[0][] = array(
            'text'  => $item->getQty() * 1,
            'feed'  => 435,
            'align' => 'right'
        );

        // draw item Prices
        $i = 0;
        $prices = $this->getItemPricesForDisplay();
        $feedPrice = 395;
        $feedSubtotal = $feedPrice + 170;
        foreach ($prices as $priceData){
            if (isset($priceData['label'])) {
                // draw Price label
                $lines[$i][] = array(
                    'text'  => $priceData['label'],
                    'feed'  => $feedPrice,
                    'align' => 'right'
                );
                // draw Subtotal label
                $lines[$i][] = array(
                    'text'  => $priceData['label'],
                    'feed'  => $feedSubtotal,
                    'align' => 'right'
                );
                $i++;
            }
            // draw Price
            $lines[$i][] = array(
                'text'  => $priceData['price'],
                'feed'  => $feedPrice,
                'font'  => 'bold',
                'align' => 'right'
            );
            // draw Subtotal
            $lines[$i][] = array(
                'text'  => $priceData['subtotal'],
                'feed'  => $feedSubtotal,
                'font'  => 'bold',
                'align' => 'right'
            );
            $i++;
        }

        // draw Tax
        $lines[0][] = array(
            'text'  => $order->formatPriceTxt($item->getTaxAmount()),
            'feed'  => 495,
            'font'  => 'bold',
            'align' => 'right'
        );

        // custom options
        $options = $this->getItemOptions();
        if ($options) {
            foreach ($options as $option) {
                // draw options label
                $lines[][] = array(
                    'text' => Mage::helper('core/string')->str_split(strip_tags($option['label']), 40, true, true),
                    'font' => 'italic',
                    'feed' => 35
                );

                if ($option['value']) {
                    if (isset($option['print_value'])) {
                        $_printValue = $option['print_value'];
                    } else {
                        $_printValue = strip_tags($option['value']);
                    }
                    $values = explode(', ', $_printValue);
                    foreach ($values as $value) {
                        $lines[][] = array(
                            'text' => Mage::helper('core/string')->str_split($value, 30, true, true),
                            'feed' => 40
                        );
                    }
                }
            }
        }

        $lineBlock = array(
            'lines'  => $lines,
            'height' => 20
        );

        $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true));
        $this->setPage($page);
    }

       private function getPublisherValue($item)
    {
        $prod = Mage::getModel('catalog/product')->load($item->getProductId());

        if(($return_publisher = $prod->getpublisher()))
            return $return_publisher;
        else
            return 'N/A';
   }
}

 

===========================================

Here is the solution:

You can use the following code to get all product attributes from sale order item table.

$product=Mage::getModel('catalog/product')->load($item->getProductId());
  if($product->getId()){
  /* product is exiting in magento
  $publisher=$product->getPublisher();
  }else{
    /* product has been deleted from  magento  so you can not get product data */

  $publisher='None';
  }

  $lines[1] = array(array(
        'text'  => Mage::helper('core/string')->str_split($publisher, 17),
        'feed' => 35,
    ));

 

 

Revisions

No comments yet.

Leave a Reply