3

What are methods for add a column to sales_order_history page? without edite

view/frontend/templates/order/history.phtml

can help me please ?

enter image description here

5 Answers 5

4

You don't need to touch view/frontend/templates/order/history.phtml template file to add aditional column into customer sales order history page.

copy layout view/frontend/layout/sales_order_history.xml to your theme and add addtional column header to sales.order.history.extra.column.header block and render column data using sales.order.history.extra.container block.

The solution:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <!-- This will add additional column header to order list -->
        <referenceBlock name="sales.order.history.extra.column.header">
            <block class="Magento\Framework\View\Element\Template" name="your.additional.column.header" template="Namespace_Module::columnheader.phtml"/>
        </referenceBlock>

        <!-- You can access current order using $this->getOrder() inside the template "-->
        <referenceBlock name="sales.order.history.extra.container">
            <block class="Magento\Framework\View\Element\Template" name="your.additional.column.data" template="Namespace_Module::columndata.phtml"/>
        </referenceBlock>
    </body>
</page>
3
  • I was able to add the column header like you suggest but I can't determine how to populate the column. Can you please expand on how you implemented the columndata.phtml?
    – RLTcode
    Mar 19, 2019 at 20:22
  • Nevermind I was able to figure it out.
    – RLTcode
    Mar 19, 2019 at 21:07
  • <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"> <?= $block->escapeHtml($block->getOrder()->getCreatedAt()) ?> </td> I have added into columndata.phtml file to display Date.
    – Jack
    Jul 8, 2019 at 1:45
4

In Magento 2.3.3

app/code/Vendor/Module/view/frontend/layout/sales_order_history.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <!-- This will add additional column header to order list -->
    <referenceBlock name="sales.order.history.extra.column.header">
        <block name="vendor.additional.column.header" template="Vendor_Module::history-header.phtml"/>
    </referenceBlock>

    <!-- You can access current order using $this->getOrder() inside the template "-->
    <referenceBlock name="sales.order.history.extra.container">
        <block name="vendor.additional.column.data" template="Vendor_Module::history.phtml"/>
    </referenceBlock>
</body>

app/code/Vendor/Module/view/frontend/templates/history.phtml

<?php
    /* @var $block \Magento\Sales\Block\Order\History\Container */
?>

 <td data-th="<?= $block->escapeHtml(__('Created At')) ?>" class="col created- 
 date">
  <?= $block->escapeHtml($block->getOrder()->getCreatedAt()) ?>
  </td>

app/code/Vendor/Module/view/frontend/templates/history-header.phtml

<?php
  /* @var $block \Magento\Sales\Block\Order\History\Container */
 ?>
    <th data-th="<?= $block->escapeHtml(__('Created At')) ?>" class="col created-date">
    <?= $block->escapeHtml("Created At") ?>
 </th>
2

create the 'sales_order_history.xml' into your theme (view\frontend\layout\sales_order_history.xml) or your module (Magento_Sales\layout\sales_order_history.xml)

add the following content in sales_order_history.xml file

<?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="sales.order.history.extra.container" template="Vendor_Module::OrderHistoryColumn.phtml"/>
</body>

create the OrderHistoryColumn.phtml file and add content. I have added the created column into the History

<?php
/* @var $block \Magento\Sales\Block\Order\History\Container */
?>

<td data-th="<?= $block->escapeHtml(__('Created At')) ?>" class="col date">
    <?= $block->escapeHtml($block->getOrder()->getCreatedAt()) ?>
</td>

run the following commands

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento indexer:reindex

php bin/magento cache:clean

Check the output.

enter image description here

Thanks

0

Please note that you need to edit sales order history template, without edit its not possible. The sales order history grid container being managed via

vendor/magento/module-sales/view/frontend/templates/order/history.phtml

You can override this template in your custom theme at:

app/design/frontend/Your/theme/Magento_Sales/templates/order/history.phtml

add the required column and get value for the same.

Update

Yes, we can use $block->getChildHtml('extra.column.header'); to add new column to sales order history. If you look into that template, there is code

<?php echo $block->getChildHtml('extra.column.header');?>

to add additional columns and value being populated by

<?php if ($extra): ?>
       <?php $extra->setOrder($_order); ?>
       <?php /* @noEscape */ echo $extra->getChildHtml() ?>
<?php endif; ?>

Also it is referenced in sales_order_history.xml

<referenceContainer name="content">
            <block class="Magento\Sales\Block\Order\History" name="sales.order.history" cacheable="false">
                <container name="sales.order.history.info" as="info" label="Order History Info"/>
                <container name="sales.order.history.extra.column.header" as="extra.column.header" label="Order History Extra Column Header"/>
                <block class="Magento\Sales\Block\Order\History\Container"
                       name="sales.order.history.extra.container" as="extra.container">
                    <block class="Magento\Framework\View\Element\Template"
                        name="sales.order.history.extra.container.data" as="extra.container.data"/>
                </block>
            </block>
            <block class="Magento\Customer\Block\Account\Dashboard" name="customer.account.link.back" template="account/link/back.phtml" cacheable="false"/>
        </referenceContainer>

However, I am not sure where we need to add extra columns. I checked store configuration but can't see the option to add new columns to order history. I am also curious to know more about it, will update you if get more idea on it. Please update me too in case you get anything on it :)

5
  • the detail extra.column.header "$block->getChildHtml('extra.column.header')" it's for add a column without modified the template .phtml, the probleme is exist in exam certification Magento 2 professional developer Nov 28, 2018 at 15:50
  • Yes, it could also be used but that column would be shown at the end but you need your column after "Ship to" I hope. I am not sure if you will be able to adjust column at required location using $block->getChildHtml('extra.column.header') Nov 28, 2018 at 15:52
  • add the column befor or after "Ship to" it's no problem, but really it's a question exists in exam certified Magento professionnel developer. Nov 28, 2018 at 15:55
  • I failed the first time I tried to pass the certification, I worked on memorizing the questions, the way they were formulated and the points I was not sure about. Nov 28, 2018 at 16:32
  • Oh I see! I am also preparing for the same. Best of the luck for next time (Y) Nov 28, 2018 at 16:36
0

You don't need to touch view/frontend/templates/order/history.phtml template file to add aditional column into customer sales order history page.

copy layout view/frontend/layout/sales_order_history.xml to your theme and add addtional column header to sales.order.history.extra.column.header block and render column data using sales.order.history.extra.container block.

You have all set now.

Your Answer

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

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