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
- August 12, 2015 @ 17:24:51 [Current Revision] by admin
- August 12, 2015 @ 17:24:51 by admin
- August 12, 2015 @ 16:39:28 by admin
- August 12, 2015 @ 16:36:40 by admin
Revision Differences
August 12, 2015 @ 16:39:28 | Current 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)& 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)& 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: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
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_Abstract | 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_Abstract | ||
Unchanged: { | Unchanged: { | ||
Unchanged: /** | Unchanged: /** | ||
Unchanged: * Draw item line | Unchanged: * Draw item line | ||
Unchanged: */ | Unchanged: */ | ||
Unchanged: public function draw() | Unchanged: public function draw() | ||
Unchanged: { | Unchanged: { | ||
Unchanged: $order = $this->getOrder(); | Unchanged: $order = $this->getOrder(); | ||
Unchanged: $item = $this->getItem(); | Unchanged: $item = $this->getItem(); | ||
Unchanged: $pdf = $this->getPdf(); | Unchanged: $pdf = $this->getPdf(); | ||
Unchanged: $page = $this->getPage(); | Unchanged: $page = $this->getPage(); | ||
Unchanged: $lines = array(); | Unchanged: $lines = array(); | ||
Unchanged: $Publisher = $this->getPublisherValue($item); | Unchanged: $Publisher = $this->getPublisherValue($item); | ||
Unchanged: // draw Product name | Unchanged: // draw Product name | ||
Unchanged: $lines[0] = array(array( | Unchanged: $lines[0] = array(array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($item- >getName(), 35, true, true), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($item- >getName(), 35, true, true), | ||
Unchanged: 'feed' => 35, | Unchanged: 'feed' => 35, | ||
Unchanged: )); | Unchanged: )); | ||
Unchanged: // draw publisher name | Unchanged: // draw publisher name | ||
Unchanged: $lines[1][] = array( | Unchanged: $lines[1][] = array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($Publisher, 35), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($Publisher, 35), | ||
Unchanged: 'feed' => 35 | Unchanged: 'feed' => 35 | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // draw SKU | Unchanged: // draw SKU | ||
Unchanged: $lines[0][] = array( | Unchanged: $lines[0][] = array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($this- >getSku($item), 17), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($this- >getSku($item), 17), | ||
Unchanged: 'feed' => 290, | Unchanged: 'feed' => 290, | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // draw QTY | Unchanged: // draw QTY | ||
Unchanged: $lines[0][] = array( | Unchanged: $lines[0][] = array( | ||
Unchanged: 'text' => $item->getQty() * 1, | Unchanged: 'text' => $item->getQty() * 1, | ||
Unchanged: 'feed' => 435, | Unchanged: 'feed' => 435, | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // draw item Prices | Unchanged: // draw item Prices | ||
Unchanged: $i = 0; | Unchanged: $i = 0; | ||
Unchanged: $prices = $this->getItemPricesForDisplay(); | Unchanged: $prices = $this->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 label | Unchanged: // draw Price label | ||
Unchanged: $lines[$i][] = array( | Unchanged: $lines[$i][] = array( | ||
Unchanged: 'text' => $priceData['label'], | Unchanged: 'text' => $priceData['label'], | ||
Unchanged: 'feed' => $feedPrice, | Unchanged: 'feed' => $feedPrice, | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // draw Subtotal label | Unchanged: // draw Subtotal label | ||
Unchanged: $lines[$i][] = array( | Unchanged: $lines[$i][] = array( | ||
Unchanged: 'text' => $priceData['label'], | Unchanged: 'text' => $priceData['label'], | ||
Unchanged: 'feed' => $feedSubtotal, | Unchanged: 'feed' => $feedSubtotal, | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: $i++; | Unchanged: $i++; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // draw Price | Unchanged: // draw Price | ||
Unchanged: $lines[$i][] = array( | Unchanged: $lines[$i][] = array( | ||
Unchanged: 'text' => $priceData['price'], | Unchanged: 'text' => $priceData['price'], | ||
Unchanged: 'feed' => $feedPrice, | Unchanged: 'feed' => $feedPrice, | ||
Unchanged: 'font' => 'bold', | Unchanged: 'font' => 'bold', | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // draw Subtotal | Unchanged: // draw Subtotal | ||
Unchanged: $lines[$i][] = array( | Unchanged: $lines[$i][] = array( | ||
Unchanged: 'text' => $priceData['subtotal'], | Unchanged: 'text' => $priceData['subtotal'], | ||
Unchanged: 'feed' => $feedSubtotal, | Unchanged: 'feed' => $feedSubtotal, | ||
Unchanged: 'font' => 'bold', | Unchanged: 'font' => 'bold', | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: $i++; | Unchanged: $i++; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // draw Tax | Unchanged: // draw Tax | ||
Unchanged: $lines[0][] = array( | Unchanged: $lines[0][] = array( | ||
Unchanged: 'text' => $order->formatPriceTxt( $item->getTaxAmount()), | Unchanged: 'text' => $order->formatPriceTxt( $item->getTaxAmount()), | ||
Unchanged: 'feed' => 495, | Unchanged: 'feed' => 495, | ||
Unchanged: 'font' => 'bold', | Unchanged: 'font' => 'bold', | ||
Unchanged: 'align' => 'right' | Unchanged: 'align' => 'right' | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: // custom options | Unchanged: // custom options | ||
Unchanged: $options = $this->getItemOptions(); | Unchanged: $options = $this->getItemOptions(); | ||
Unchanged: if ($options) { | Unchanged: if ($options) { | ||
Unchanged: foreach ($options as $option) { | Unchanged: foreach ($options as $option) { | ||
Unchanged: // draw options label | Unchanged: // draw options label | ||
Unchanged: $lines[][] = array( | Unchanged: $lines[][] = array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split(strip_ tags($option['label']), 40, true, true), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split(strip_ tags($option['label']), 40, true, true), | ||
Unchanged: 'font' => 'italic', | Unchanged: 'font' => 'italic', | ||
Unchanged: 'feed' => 35 | Unchanged: 'feed' => 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(', ', $_printValue); | Unchanged: $values = explode(', ', $_printValue); | ||
Unchanged: foreach ($values as $value) { | Unchanged: foreach ($values as $value) { | ||
Unchanged: $lines[][] = array( | Unchanged: $lines[][] = array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($value, 30, true, true), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($value, 30, true, true), | ||
Unchanged: 'feed' => 40 | Unchanged: 'feed' => 40 | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: } | Unchanged: } | ||
Unchanged: } | Unchanged: } | ||
Unchanged: } | Unchanged: } | ||
Unchanged: } | Unchanged: } | ||
Unchanged: $lineBlock = array( | Unchanged: $lineBlock = array( | ||
Unchanged: 'lines' => $lines, | Unchanged: 'lines' => $lines, | ||
Unchanged: 'height' => 20 | Unchanged: 'height' => 20 | ||
Unchanged: ); | Unchanged: ); | ||
Unchanged: $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true)); | Unchanged: $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true)); | ||
Unchanged: $this->setPage($page); | Unchanged: $this->setPage($page); | ||
Unchanged: } | Unchanged: } | ||
Unchanged: private function getPublisherValue($item) | Unchanged: private function getPublisherValue($item) | ||
Unchanged: { | Unchanged: { | ||
Unchanged: $prod = Mage::getModel( 'catalog/ product' )->load($item- >getProductId()); | Unchanged: $prod = Mage::getModel( 'catalog/ product' )->load($item- >getProductId()); | ||
Unchanged: if(($return_publisher = $prod->getpublisher())) | Unchanged: if(($return_publisher = $prod->getpublisher())) | ||
Unchanged: return $return_publisher; | Unchanged: return $return_publisher; | ||
Unchanged: else | Unchanged: else | ||
Unchanged: return 'N/A'; | Unchanged: return 'N/A'; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: }</code></pre> | Unchanged: }</code></pre> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
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( 'catalog/ product') ->load($item- >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( 'catalog/ product') ->load($item- >getProductId()); | ||
Unchanged: if($product->getId()){ | Unchanged: if($product->getId()){ | ||
Unchanged: /* product is exiting in magento | Unchanged: /* product is exiting in magento | ||
Unchanged: $publisher=$product- >getPublisher(); | Unchanged: $publisher=$product- >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='None'; | Unchanged: $publisher='None'; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: $lines[1] = array(array( | Unchanged: $lines[1] = array(array( | ||
Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($publisher, 17), | Unchanged: 'text' => Mage::helper( 'core/string')-> str_split($publisher, 17), | ||
Unchanged: 'feed' => 35, | Unchanged: 'feed' => 35, | ||
Unchanged: ));</code></pre> | Unchanged: ));</code></pre> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.