2

I'm trying to show the order comments history on the invoice (Magento 2.2.6)

I've written the following override:

app/code/Denial/InvoiceComments/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Comments" type="Denial\InvoiceComments\Block\Rewrite\Order\Comments" />
</config>

And

app/code/Denial/InvoiceComments/Block/Rewrite/Order/Comments.php

<?php

namespace Denial\InvoiceComments\Block\Rewrite\Order;

class Comments extends \Magento\Framework\View\Element\Template 
{

public function getComments()

{
if ($this->_commentCollection === null) {
        $entity = $this->getEntity();
        if ($entity instanceof \Magento\Sales\Model\Order\Invoice) {
            $this->_commentCollection = $this->_invoiceCollectionFactory->create();
            $this->_commentCollection = $this->_orderCollectionFactory->create();

     }
}
return $this->_commentCollection;

}

}

Is this right? Have I missed any steps? I ran di:compile but the order comments aren't showing up, only the invoice comments. Am I adding to the commentCollection correctly?

  • Can you explain about your issue? Are you looking for order comments to place into your invoices? But need to be sure where in admin, in invoice email or under pdf. – Sukumar Gorai Dec 17 '18 at 10:16
  • Sorry, I need to be able to see the order comments in admin when viewing the invoice. – Denial Design Dec 17 '18 at 14:28
  • What is the comment module you are using? – Sukumar Gorai Dec 17 '18 at 15:14
  • IWD Checkout Suite. We can see comments on the order page, but I need to see them on the invoice too. The comments are saved in the sales_order_status table in the database as normal. – Denial Design Dec 17 '18 at 15:51
  • @DenialDesign, Do you want to show order comment in admin invoice section ? – Pawan Dec 21 '18 at 4:45
2
+50

The best approach for showing the order comments in Admin Invoice section would be to overrride the admin template file.

To override the comments template use the below code

app/code/Denial/InvoiceComments/view/adminhtml/layout/sales_order_invoice_view.xml

<?xml version="1.0"?>
<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">Denial_InvoiceComments::comments/view.phtml</argument>
        </action>
    </referenceBlock>
</body>
</page>

Copy the contents of the template file vendor/magento-sales/view/adminhtml/templates/order/comments/view.phtml to

app/code/Denial/InvoiceComments/view/adminhtml/templates/comments/view.phtml

Add the below code after line 51 just above the invoice comments section

<h3> Order Comments </h3>
<ul class="note-list">
<?php foreach ($_entity->getOrder()->getStatusHistoryCollection() as $status): ?>
      <?php  if ($status->getComment()): ?>
      <span class="note-list-date"><?= /* @noEscape */ $block->formatDate($status->getCreatedAt(), \IntlDateFormatter::MEDIUM) ?></span>
            <span class="note-list-time"><?= /* @noEscape */ $block->formatTime($status->getCreatedAt(), \IntlDateFormatter::MEDIUM) ?></span>
            <span class="note-list-customer">
                <?= /* @escapeNotVerified */ __('Customer') ?>
                <?php if ($status->getIsCustomerNotified()): ?>
                    <span class="note-list-customer-notified"><?= /* @escapeNotVerified */ __('Notified') ?></span>
                <?php else: ?>
                    <span class="note-list-customer-not-notified"><?= /* @escapeNotVerified */ __('Not Notified') ?></span>
                <?php endif; ?>
            </span>
      <div class="note-list-comment"><?= $block->escapeHtml($status->getComment(), ['b', 'br', 'strong', 'i', 'u', 'a']) ?></div>
       <?php endif; ?>
<?php endforeach; ?>
</ul>
<h3> Invoice Comments </h3>

The working solution output will look as shown in the screenshot link below

https://prnt.sc/lyvm9f

  • Thanks @mydons but it's not working for me. I've uploaded the xml file and an edited version of view.phtml to the correct folders but the invoice is still showing the default version. Do I need to run setup:compile or anything? I've cleared the cache and checked the logs. – Denial Design Dec 24 '18 at 16:49
  • 1
    Enable template path hints for adminhtml and reload the admin order view page. Check whether the template file path is showing the overrided module template or original template. Also make sure whether the order comments exists for that invoice. – mydons Dec 25 '18 at 6:24
  • it's still showing the original template @mydons Cache cleared. No errors. – Denial Design Dec 26 '18 at 11:12
  • @Denial Design Have you disabled this module temporarily before. Please check if the module is still active &enabled. If it is active try deleting the contents of generated/code folder and pub/static folder and run the setup:static-content:deploy -f command – mydons Dec 26 '18 at 12:01
  • 1
    registration.php file is missing. Once you add this file your module will be listed. – mydons Dec 26 '18 at 13:42

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.