0

OK, so we have this site that has a lot of custom options and we'd like to ideally have the label and value on the same line when the invoice PDF is created. I have located the file responsible at

app/code/core/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php

and have made a copy at

app/code/local/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php

but my PHP is not good enough for me to figure out how to have the label and value on the same line.

This is the code in question:

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

Ideally, I'd like to keep the checks and just have the 'text' be in this form: 'label' - 'value'

Thanks!

1

First of all, copying files that can just be overridden from the Core to Local is not best practise as it can possibly give you more work when upgrading Magento.

I have tested your case and found your solution:

First of all I rewrite it in my own extension:

 <models>
            <sales>
                <rewrite><order_pdf_items_invoice_default>MycompanyName_Myextensionname_Model_Order_Pdf_Items_Invoice_Default</order_pdf_items_invoice_default></rewrite>
            </sales>
 </models>

Then I override the function you mentioned above:

 $options = $this->getItemOptions();
    if ($options) {
        foreach ($options as $option) {
            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(strip_tags($option['label'] . " - " . $value), 40, true, true),
                        'feed' => 40
                    );
                }
            }
        }
    }

What I have done here is just removed the part where you draw the label and added it to the part where you print the value.

If you have long labels/values and you want to keep it on one line, I would increase the value in str_split.

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.