0

im work on print order to print order comment when we do print order from front end,

My Orders->Print Order

when click on Print Order the same comment print on PDF, for that i have created Module using override core files

registration.php, module.xml

Here is a code,

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Tms\Printcomment\Model\Rewrite\Order\Pdf\Invoice" />
</config>

Tms\Printcomment\Model\Rewrite\Order\Pdf\Invoice.php

app/code/Tms/Printcomment/view/frontend/templates/order/order_comments.phtml

<?php $_history = $block->getOrder()->getVisibleStatusHistory() ?>
<?php if (!empty($_history)) : ?>
    <div class="block block-order-details-comments">
        <div class="block-title"><strong><?= $block->escapeHtml(__('About Your Order')) ?></strong></div>
        <div class="block-content">
            <dl class="order-comments">
                <?php foreach ($_history as $_historyItem) : ?>
                    <dt class="comment-date">
                        <?= /* @noEscape */
                        $block->formatDate($_historyItem->getCreatedAt(), \IntlDateFormatter::MEDIUM, true) ?>
                    </dt>
                    <dd class="comment-content"><?= $block->escapeHtml($_historyItem->getComment()) ?></dd>
                <?php endforeach; ?>
            </dl>
        </div>
    </div>
<?php endif; ?>

app/code/Tms/Printcomment/view/frontend/layout/sales_order_print.xml

 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <!-- <referenceBlock name="additional.product.info"> 
            <block class="Tms\Printcomment\Order\View" 
                name="order.comments"
                template="Namespace_Sales::order/order_comments.phtml" 
               cacheable="false" /> 
         </referenceBlock> -->
        <!-- <block class="Magento\Sales\Block\Order\View" name="order.comments" template="Magento_Sales::order/order_comments.phtml" />-->
        <referenceBlock name="order.comments" template="Tms_Printcomment::order/order_comments.phtml" />
    </body>
</page>

But its not work, comment not print in PDF, what shoud I do wrong? please help me on this

1 Answer 1

1

For this, you can create an extension. because you need to override files. First create file app\code\Vendor\Extension\etc\di.xml

<preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor\Extension\Model\Rewrite\Order\Pdf\Invoice" />

Now override file app\code\Vendor\Extension\Model\Rewrite\Order\Pdf\ Invoice.php in function getPdf() use code

foreach ($invoice->getOrder()->getStatusHistoryCollection() as $status){
            $textChunk = wordwrap(strip_tags($status->getComment()), 120, "\n");
            foreach(explode("\n", $textChunk) as $textLine){
                if ($textLine!=='') {
                    $page->drawText(strip_tags(ltrim($textLine)), 35, $this->y, 'UTF-8');
                    $this->y -= 15;
                }
            }
        }

For adding custom template to any block follow

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="order.comments">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Vendor_Extension::order/comments/view.phtml</argument>
        </action>
    </referenceBlock>
</body>

18
  • i update my Question with New Code...but its not work can u plesase suggest me what mistakes i do? Override file is not working
    – rocky9310
    May 6, 2020 at 6:42
  • You need to add code in the file which you are using for printing PDF. like this app\code\Vendor\Extension\Model\Rewrite\Order\Pdf\Invoice.pdf May 6, 2020 at 7:00
  • that is ok, file is on the right place, but whole module not work
    – rocky9310
    May 6, 2020 at 7:08
  • core file is not override. i have done any mistakes?
    – rocky9310
    May 6, 2020 at 7:10
  • you are taking reference in wrong way May 6, 2020 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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