You are viewing an old revision of this post, from August 12, 2015 @ 16:39:28. See below for differences between this version and the current revision.

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

Revision Differences

August 12, 2015 @ 16:39:28Current Revision
Content
Unchanged: <p>Unchanged: <p>
Unchanged: <span style="color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 13px;">i am trying to add custom attribute , product(book) publisher name below product name in PDF invoice(like in image)&amp; 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</span>Unchanged: <span style="color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 13px;">i am trying to add custom attribute , product(book) publisher name below product name in PDF invoice(like in image)&amp; 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</span>
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <pre style="margin-top: 0px; padding: 5px; border: 0px; overflow: auto; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; color: rgb(34, 34, 34); background-color: rgb(238, 238, 238);">Unchanged: <pre style="margin-top: 0px; padding: 5px; border: 0px; overflow: auto; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; color: rgb(34, 34, 34); background-color: rgb(238, 238, 238);">
Unchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">class Mage_Sales_Model_ Order_Pdf_Items_ Invoice_Default extends Mage_Sales_Model_ Order_Pdf_Items_AbstractUnchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">class Mage_Sales_Model_ Order_Pdf_Items_ Invoice_Default extends Mage_Sales_Model_ Order_Pdf_Items_Abstract
Unchanged: {Unchanged: {
Unchanged: /**Unchanged: /**
Unchanged: * Draw item lineUnchanged: * Draw item line
Unchanged: */Unchanged: */
Unchanged: public function draw()Unchanged: public function draw()
Unchanged: {Unchanged: {
Unchanged: $order = $this-&gt;getOrder();Unchanged: $order = $this-&gt;getOrder();
Unchanged: $item = $this-&gt;getItem();Unchanged: $item = $this-&gt;getItem();
Unchanged: $pdf = $this-&gt;getPdf();Unchanged: $pdf = $this-&gt;getPdf();
Unchanged: $page = $this-&gt;getPage();Unchanged: $page = $this-&gt;getPage();
Unchanged: $lines = array();Unchanged: $lines = array();
Unchanged: $Publisher = $this-&gt;getPublisherValue($item);Unchanged: $Publisher = $this-&gt;getPublisherValue($item);
Unchanged: // draw Product nameUnchanged: // draw Product name
Unchanged: $lines[0] = array(array(Unchanged: $lines[0] = array(array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($item- &gt;getName(), 35, true, true),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($item- &gt;getName(), 35, true, true),
Unchanged: &#39;feed&#39; =&gt; 35,Unchanged: &#39;feed&#39; =&gt; 35,
Unchanged: ));Unchanged: ));
Unchanged: // draw publisher nameUnchanged: // draw publisher name
Unchanged: $lines[1][] = array(Unchanged: $lines[1][] = array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($Publisher, 35),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($Publisher, 35),
Unchanged: &#39;feed&#39; =&gt; 35Unchanged: &#39;feed&#39; =&gt; 35
Unchanged: );Unchanged: );
Unchanged: // draw SKUUnchanged: // draw SKU
Unchanged: $lines[0][] = array(Unchanged: $lines[0][] = array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($this- &gt;getSku($item), 17),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($this- &gt;getSku($item), 17),
Unchanged: &#39;feed&#39; =&gt; 290,Unchanged: &#39;feed&#39; =&gt; 290,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: // draw QTYUnchanged: // draw QTY
Unchanged: $lines[0][] = array(Unchanged: $lines[0][] = array(
Unchanged: &#39;text&#39; =&gt; $item-&gt;getQty() * 1,Unchanged: &#39;text&#39; =&gt; $item-&gt;getQty() * 1,
Unchanged: &#39;feed&#39; =&gt; 435,Unchanged: &#39;feed&#39; =&gt; 435,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: // draw item PricesUnchanged: // draw item Prices
Unchanged: $i = 0;Unchanged: $i = 0;
Unchanged: $prices = $this-&gt;getItemPricesForDisplay();Unchanged: $prices = $this-&gt;getItemPricesForDisplay();
Unchanged: $feedPrice = 395;Unchanged: $feedPrice = 395;
Unchanged: $feedSubtotal = $feedPrice + 170;Unchanged: $feedSubtotal = $feedPrice + 170;
Unchanged: foreach ($prices as $priceData){Unchanged: foreach ($prices as $priceData){
Unchanged: if (isset($priceData['label'])) {Unchanged: if (isset($priceData['label'])) {
Unchanged: // draw Price labelUnchanged: // draw Price label
Unchanged: $lines[$i][] = array(Unchanged: $lines[$i][] = array(
Unchanged: &#39;text&#39; =&gt; $priceData['label'],Unchanged: &#39;text&#39; =&gt; $priceData['label'],
Unchanged: &#39;feed&#39; =&gt; $feedPrice,Unchanged: &#39;feed&#39; =&gt; $feedPrice,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: // draw Subtotal labelUnchanged: // draw Subtotal label
Unchanged: $lines[$i][] = array(Unchanged: $lines[$i][] = array(
Unchanged: &#39;text&#39; =&gt; $priceData['label'],Unchanged: &#39;text&#39; =&gt; $priceData['label'],
Unchanged: &#39;feed&#39; =&gt; $feedSubtotal,Unchanged: &#39;feed&#39; =&gt; $feedSubtotal,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: $i++;Unchanged: $i++;
Unchanged: }Unchanged: }
Unchanged: // draw PriceUnchanged: // draw Price
Unchanged: $lines[$i][] = array(Unchanged: $lines[$i][] = array(
Unchanged: &#39;text&#39; =&gt; $priceData['price'],Unchanged: &#39;text&#39; =&gt; $priceData['price'],
Unchanged: &#39;feed&#39; =&gt; $feedPrice,Unchanged: &#39;feed&#39; =&gt; $feedPrice,
Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: // draw SubtotalUnchanged: // draw Subtotal
Unchanged: $lines[$i][] = array(Unchanged: $lines[$i][] = array(
Unchanged: &#39;text&#39; =&gt; $priceData['subtotal'],Unchanged: &#39;text&#39; =&gt; $priceData['subtotal'],
Unchanged: &#39;feed&#39; =&gt; $feedSubtotal,Unchanged: &#39;feed&#39; =&gt; $feedSubtotal,
Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: $i++;Unchanged: $i++;
Unchanged: }Unchanged: }
Unchanged: // draw TaxUnchanged: // draw Tax
Unchanged: $lines[0][] = array(Unchanged: $lines[0][] = array(
Unchanged: &#39;text&#39; =&gt; $order-&gt;formatPriceTxt( $item-&gt;getTaxAmount()),Unchanged: &#39;text&#39; =&gt; $order-&gt;formatPriceTxt( $item-&gt;getTaxAmount()),
Unchanged: &#39;feed&#39; =&gt; 495,Unchanged: &#39;feed&#39; =&gt; 495,
Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,Unchanged: &#39;font&#39; =&gt; &#39;bold&#39;,
Unchanged: &#39;align&#39; =&gt; &#39;right&#39;Unchanged: &#39;align&#39; =&gt; &#39;right&#39;
Unchanged: );Unchanged: );
Unchanged: // custom optionsUnchanged: // custom options
Unchanged: $options = $this-&gt;getItemOptions();Unchanged: $options = $this-&gt;getItemOptions();
Unchanged: if ($options) {Unchanged: if ($options) {
Unchanged: foreach ($options as $option) {Unchanged: foreach ($options as $option) {
Unchanged: // draw options labelUnchanged: // draw options label
Unchanged: $lines[][] = array(Unchanged: $lines[][] = array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split(strip_ tags($option['label']), 40, true, true),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split(strip_ tags($option['label']), 40, true, true),
Unchanged: &#39;font&#39; =&gt; &#39;italic&#39;,Unchanged: &#39;font&#39; =&gt; &#39;italic&#39;,
Unchanged: &#39;feed&#39; =&gt; 35Unchanged: &#39;feed&#39; =&gt; 35
Unchanged: );Unchanged: );
Unchanged: if ($option['value']) {Unchanged: if ($option['value']) {
Unchanged: if (isset($option['print_value'])) {Unchanged: if (isset($option['print_value'])) {
Unchanged: $_printValue = $option['print_value'];Unchanged: $_printValue = $option['print_value'];
Unchanged: } else {Unchanged: } else {
Unchanged: $_printValue = strip_tags($option['value']);Unchanged: $_printValue = strip_tags($option['value']);
Unchanged: }Unchanged: }
Unchanged: $values = explode(&#39;, &#39;, $_printValue);Unchanged: $values = explode(&#39;, &#39;, $_printValue);
Unchanged: foreach ($values as $value) {Unchanged: foreach ($values as $value) {
Unchanged: $lines[][] = array(Unchanged: $lines[][] = array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($value, 30, true, true),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($value, 30, true, true),
Unchanged: &#39;feed&#39; =&gt; 40Unchanged: &#39;feed&#39; =&gt; 40
Unchanged: );Unchanged: );
Unchanged: }Unchanged: }
Unchanged: }Unchanged: }
Unchanged: }Unchanged: }
Unchanged: }Unchanged: }
Unchanged: $lineBlock = array(Unchanged: $lineBlock = array(
Unchanged: &#39;lines&#39; =&gt; $lines,Unchanged: &#39;lines&#39; =&gt; $lines,
Unchanged: &#39;height&#39; =&gt; 20Unchanged: &#39;height&#39; =&gt; 20
Unchanged: );Unchanged: );
Unchanged: $page = $pdf-&gt;drawLineBlocks($page, array($lineBlock), array(&#39;table_header&#39; =&gt; true));Unchanged: $page = $pdf-&gt;drawLineBlocks($page, array($lineBlock), array(&#39;table_header&#39; =&gt; true));
Unchanged: $this-&gt;setPage($page);Unchanged: $this-&gt;setPage($page);
Unchanged: }Unchanged: }
Unchanged: private function getPublisherValue($item)Unchanged: private function getPublisherValue($item)
Unchanged: {Unchanged: {
Unchanged: $prod = Mage::getModel( &#39;catalog/ product&#39; )-&gt;load($item- &gt;getProductId());Unchanged: $prod = Mage::getModel( &#39;catalog/ product&#39; )-&gt;load($item- &gt;getProductId());
Unchanged: if(($return_publisher = $prod-&gt;getpublisher()))Unchanged: if(($return_publisher = $prod-&gt;getpublisher()))
Unchanged: return $return_publisher;Unchanged: return $return_publisher;
Unchanged: elseUnchanged: else
Unchanged: return &#39;N/A&#39;;Unchanged: return &#39;N/A&#39;;
Unchanged: }Unchanged: }
Unchanged: }</code></pre>Unchanged: }</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">=== ============= ============ ===============</code>Unchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">=== ============= ============ ===============</code>
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: <strong>Here is the solution:</strong>Unchanged: <strong>Here is the solution:</strong>
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Deleted: 1. At first, you need to convert product attribute become order attribute. Please view this post:Added: You can use the following code to get all product attributes from sale order item table.
Deleted: </p> 
Deleted: <p> 
Deleted: http://www.gentotech.net/ wiki/adding-custom-product- attribute-to- quote-and-order- items-in-magento/ 
Deleted: </p> 
Deleted: <p> 
Deleted: 2. After converting product attribute becomes order attribute, you can use the following code to get all product attributes from sale order item table. 
Unchanged: </p>Unchanged: </p>
Unchanged: <pre style="margin-top: 0px; padding: 5px; border: 0px; overflow: auto; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; color: rgb(34, 34, 34); background-color: rgb(238, 238, 238);">Unchanged: <pre style="margin-top: 0px; padding: 5px; border: 0px; overflow: auto; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; word-wrap: normal; color: rgb(34, 34, 34); background-color: rgb(238, 238, 238);">
Unchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">$product= Mage::getModel( &#39;catalog/ product&#39;) -&gt;load($item- &gt;getProductId());Unchanged: <code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">$product= Mage::getModel( &#39;catalog/ product&#39;) -&gt;load($item- &gt;getProductId());
Unchanged: if($product-&gt;getId()){Unchanged: if($product-&gt;getId()){
Unchanged: /* product is exiting in magentoUnchanged: /* product is exiting in magento
Unchanged: $publisher=$product- &gt;getPublisher();Unchanged: $publisher=$product- &gt;getPublisher();
Unchanged: }else{Unchanged: }else{
Unchanged: /* product has been deleted from magento so you can not get product data */Unchanged: /* product has been deleted from magento so you can not get product data */
Unchanged: $publisher=&#39;None&#39;;Unchanged: $publisher=&#39;None&#39;;
Unchanged: }Unchanged: }
Unchanged: $lines[1] = array(array(Unchanged: $lines[1] = array(array(
Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($publisher, 17),Unchanged: &#39;text&#39; =&gt; Mage::helper( &#39;core/string&#39;)-&gt; str_split($publisher, 17),
Unchanged: &#39;feed&#39; =&gt; 35,Unchanged: &#39;feed&#39; =&gt; 35,
Unchanged: ));</code></pre>Unchanged: ));</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply