7

In Magento 2 I want to call my custom phtml file order success page, but without overriding block. Is there any way to achieve this ?

I don't want to edit Magento success.phtml as I am creating module.

Any help will appreciated

Thanks.

12

You can use any of Magento's layout file in your module to insert your custom block. In your case you need to use checkout_onepage_success.xml layout file in your custom module which will be active when a customer is on order success page. In the layout file you need to specify where you want to add your custom template using referenceContainer or referenceBlock.

For you I've created a simple module and tested it which works fine.

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Amit_Orderpage',
    __DIR__
);

composer.json

{
  "name": "amit/orderpage-module",
  "description": "Simple Magento 2 module that adds a new template on order success page.",
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/framework": "~100.0"
  },
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "Amit\\Orderpage\\": ""
    }
  }
}

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Amit_Orderpage" setup_version="2.0.0"/>
</config>

Block/Success.php

<?php
namespace Amit\Orderpage\Block;
class Success  extends \Magento\Framework\View\Element\Template
{
    public function getSomething()
    {
        return 'returned something from custom block.';
    }
}

view/frontend/layout/checkout_onepage_success.xml

<?xml version="1.0"?>
<body>
    <referenceContainer name="order.success.additional.info">
        <block class="Amit\Orderpage\Block\Success"
               name="amit.order.success"
               template="Amit_Orderpage::order/success.phtml"
               after="-">
        </block>
    </referenceContainer>
</body>

Specify before or after tag to specify where you want to add your template before or after order.success.additional.info container. Here after="-" will add your your template to order.success.additional.info container after all other containers present in the same section.

view/frontend/templates/order/success.phtml

<?php /* @var $block \Amit\Orderpage\Block\Success */?>
<?php echo __('Custom template file content.'); ?>
<?php echo $block->getSomething(); ?>
  • I don't want to edit magento success.phtml , I want to achieve this by xml or event as I am creating extension. – Smith Sep 17 '16 at 9:41
  • @Smith answer updated as per your comment. – amitshree Sep 18 '16 at 11:25
  • This helped a lot. +1 :) – Shoaib Munir May 13 at 6:03
2

If you would like to utilize/access the current $order object in your block/html file, without overriding original order success block/template. Here is the complete solution:

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MageAj_OrderSuccess',
    __DIR__
);

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="MageAj_OrderSuccess" setup_version="2.0.0" />
</config>

app/code/MageAj/OrderSuccess/view/frontend/layout/checkout_onepage_success.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="order.success.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="mageaj.success" template="MageAj_OrderSuccess::checkout/onepage/success.phtml" cacheable="false">
                <arguments>
                    <argument name="view_model" xsi:type="object">MageAj\OrderSuccess\ViewModel\Checkout\Onepage\Success</argument>
                </arguments>
            </block>
        </referenceContainer>        
    </body>    
</page>

app/code/MageAj/OrderSuccess/ViewModel/Checkout/Onepage/Success.php

<?php

namespace MageAj\OrderSuccess\ViewModel\Checkout\Onepage;

class Success implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    protected $_checkoutSession;
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession        
    ) {        
        $this->_checkoutSession = $checkoutSession;       
    }

    public function getOrder()
    {
        return $this->_checkoutSession->getLastRealOrder();
    }
}

app/code/MageAj/OrderSuccess/view/frontend/templates/order/success.phtml

<?php $order = $block->getViewModel()->getOrder(); ?>
2

If you dont want to create new module, you can do it in your theme as well. Follow below steps:

1.Create checkout_onepage_success.xml in your theme. For example:

app/design/frontend///Magento_Checkout/layout/checkout_onepage_success.xml.

The content of file is:

<?xml version="1.0"?>
<body>
    <referenceContainer name="order.success.additional.info">
        <block class="Magento\Checkout\Block\Onepage\Success"
               name="custom_info.order.success"
               template="Magento_Checkout::order/success/custom_info.phtml"
               after="-">
        </block>
    </referenceContainer>
</body>

2.Then you need to create the template file:

app/design/frontend///Magento_Checkout/templates/order/success/custom_info.phtml

You can add any content in the template. For example:

<?php /* @var $block \Magento\Checkout\Block\Onepage\Success */?>
<?php echo __('Custom template file content.'); ?>
<?php echo $block->getSomething(); ?>

Clear the cache and you are done.

0

In checkout_onepage_success.xml:-

<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::test.phtml"/>

Missed my code before , can you check my answer now.

fschmengler

  • Welcome to Magento SE. Can you add a bit more information how to use it? As it stands the answer is not very useful. – Fabian Schmengler Sep 17 '16 at 11:22
-1

you can used referenceContainer or referenceBlock to add your phtml file in order sucess page.

<referenceBlock name="checkout.success">
      <block class="[Company]\[Vendor]\Block\[Yourblock]" template="Company_Vendor::yourfile.phtml" name="[Yourblock Name]"/>
</referenceBlock>
  • 1
    Can you add a bit more information how to use it? As it stands the answer is not very useful. – Fabian Schmengler Sep 17 '16 at 7:40

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.