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?

6
  • 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. Dec 17, 2018 at 10:16
  • Sorry, I need to be able to see the order comments in admin when viewing the invoice. Dec 17, 2018 at 14:28
  • What is the comment module you are using? Dec 17, 2018 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. Dec 17, 2018 at 15:51
  • @DenialDesign, Do you want to show order comment in admin invoice section ?
    – Pawan
    Dec 21, 2018 at 4:45

2 Answers 2

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

9
  • 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. Dec 24, 2018 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, 2018 at 6:24
  • it's still showing the original template @mydons Cache cleared. No errors. Dec 26, 2018 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, 2018 at 12:01
  • 1
    registration.php file is missing. Once you add this file your module will be listed.
    – mydons
    Dec 26, 2018 at 13:42
0
<?php
    
    namespace Rcc\ExtraFixes\Ui\Component\Listing\Column;
    
    use \Magento\Sales\Api\OrderRepositoryInterface;
    use Magento\Ui\Component\Listing\Columns\Column;
    use Magento\Framework\View\Element\UiComponent\ContextInterface;
    use Magento\Framework\View\Element\UiComponentFactory;
    use Magento\Framework\Api\SearchCriteriaBuilder;
    
    
    class OrderComment extends Column {
        protected $_orderRepository;
        protected $_searchCriteria;
    
    
        public function __construct(
            ContextInterface $context,
            UiComponentFactory $uiComponentFactory,
            OrderRepositoryInterface $orderRepository,
            SearchCriteriaBuilder $searchCriteria,
            array $components = [],
            array $data = []
        ) {
            $this->_orderRepository = $orderRepository;
            $this->_searchCriteria = $searchCriteria;
             parent::__construct($context, $uiComponentFactory, $components, $data);
        }
    
        public function prepareDataSource(array $dataSource)
        {
            if (isset($dataSource['data']['items'])) {
                foreach ($dataSource['data']['items'] as & $item) {
    
                    $order = $this->_orderRepository->get($item["entity_id"]);
                    $statusHistoryItem = $order->getStatusHistoryCollection()->getFirstItem();
                    $comment = $statusHistoryItem->getComment();
                    $item[$this->getData('name')] = $comment;
                     
                }
            }
    
            return $dataSource;
        }
    }

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="comment" class="Vemdpr\Module\Ui\Component\Listing\Column\OrderComment">
            <settings>
                <label translate="true">Last Order Comment</label>
                <visible>true</visible>
            </settings>
        </column>
    </columns>

</listing>

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.