0

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.

I copied

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 is 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';
   }
}
0

dinesh You can get all product attribute value from sale order item So you need to load Product from Product object Mage::getModel('catalog/product') by $item->getProductId();

  $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,
    ));

$product is give when this product is exiting magento because magento do not save product publisher attribute in sales_flat_order_item

19
  • i try this not working when i click print show blank page & no pdf is generated
    – Dinesh
    Nov 3 '14 at 8:20
  • check this and what attribute code of publisher?
    – Amit Bera
    Nov 3 '14 at 8:22
  • try this also but same problem
    – Dinesh
    Nov 3 '14 at 8:28
  • what is your attribute code of attribute
    – Amit Bera
    Nov 3 '14 at 9:46
  • how to get a attribute code of attribute
    – Dinesh
    Nov 3 '14 at 10:33
0

You can try below code.

Rewrite class Mage_Sales_Model_Order_Pdf_Items_Invoice_Default
Override method public function draw()
Add below in the function

$newAttr = $productObject->getAttributeText('attribute_code');
$lines[][] = array(
                'text' => Mage::helper('core/string')->str_split('Style : ' . $newAttr, 60, true, true),
                'font' => 'italic',
                'feed' => 35
            );
1
  • can we use condition with attribute ? for example if attribute value is Yes then that text will display and can we show this attribute text after product name like Product 1 (Attribute text) ? Please Aug 18 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.