0

I would like to add a column after "Product" to have a link to the product on the front end so it is easy to identify the product. Nearly all our products are one off items so I'm looking for an easy way to identify which product exactly the person has ordered, which the front end view of the product provides.

I followed the instructions here https://github.com/magento/magento2/pull/11076, which I found from here Magento2 How to custom column in sales order item renderer in admin order view and I'm getting an error stating my template is invalid

    1 exception(s):
Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'Vendor_Module::test.phtml' in module: 'Magento_Sales' block's name: 'column_test_column'

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'Vendor_Module::test.phtml' in module: 'Magento_Sales' block's name: 'column_test_column'
#0 /var/www/html/magento/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\View\Element\Template->fetchView(false)
#1 /var/www/html/magento/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn\Interceptor->___callParent('fetchView', Array)
#2 /var/www/html/magento/vendor/salesigniter/releasedamagewaiver2/Plugin/Catalog/Template.php(84): Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn\Interceptor->Magento\Framework\Interception\{closure}(false)

That continues on for some time.

So for the sales_order_view.xml, what have I got wrong?

<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_items">
            <arguments>
                <argument name="columns" xsi:type="array">
                    <item name="test_column" xsi:type="string" translate="true">Test Header</item>
                </argument>
            </arguments>
            <referenceBlock name="default_order_items_renderer">
                <arguments>
                    <argument name="columns" xsi:type="array">
                        <item name="test_column" xsi:type="string" translate="true">col-test</item>
                    </argument>
                </arguments>
            </referenceBlock>

            <block class="Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn"
                   name="column_test_column"
                   template="Vendor_Module::test.phtml" group="column" />
        </referenceBlock>
    </body>
</page>

My test.phtml, as the link above has, simply had "test column" within it, no more code/text.

Secondly, how can I pull the url of the product on the frontend to put in this column? Is there a way I can grab the product id stored in the product_id column of the sales_order_item table to use this to get the front end url? As I haven't been able to get past the custom column step I haven't been able to try getting the url but would love any advice.

  • What is the location of this test.phtml file? – ABHISHEK TRIPATHI Feb 25 at 3:41
2

May be the location of your template file is not correct.
Try using the following code and modify it according to your requirement.

app/code/Anshu/OrderItemLink/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Anshu_OrderItemLink',
    __DIR__
);

app/code/Anshu/OrderItemLink/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Anshu_OrderItemLink">
        <sequence>
            <module name="Magento_Sales" />
        </sequence>
    </module>
</config>

app/code/Anshu/OrderItemLink/view/adminhtml/layout/sales_order_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order_items">
            <arguments>
                <argument name="columns" xsi:type="array">
                    <item name="plink" xsi:type="string" translate="true">Product Link</item>
                </argument>
            </arguments>
            <referenceBlock name="default_order_items_renderer">
                <arguments>
                    <argument name="columns" xsi:type="array">
                        <item name="plink" xsi:type="string" translate="false">product-link</item>
                    </argument>
                </arguments>
            </referenceBlock>
            <block class="Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn" name="column_plink" template="Anshu_OrderItemLink::item_link.phtml" group="column" />
        </referenceBlock>
    </body>
</page>

app/code/Anshu/OrderItemLink/view/adminhtml/templates/item_link.phtml

<?php if ($_item = $block->getItem()): ?>
    <div class="product-link-block">
        <a href="<?= $_item->getProduct()->getProductUrl() ?>" target="_blank"><?= $block->escapeHtml(__('Link to Product')) ?></a>
    </div>
<?php endif; ?>

Here, Anshu is my namespace and OrderItemLink is my module name.

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.