0

I'm trying to add some content of my block into the sales/order/history part.

I have this:

<?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">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">My_Module::history.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

My_Module::history.phtml is adding some text just for the moment, but I want to add content that is on my block. How can I do that ?

2
  • What do you mean by My_Module::history.phtml is adding some text just for the moment, ? please clarify what you exactly want to do, you have some informations in My_Module::history.phtml and you want to display that informations in sales.order.history ?
    – PЯINCƎ
    Jun 1, 2018 at 0:43
  • History.phtml display "helloworld" on the page of the order history. I want to add more than that, with content from a block so that i can show data from a webservice on the existing order history page Jun 1, 2018 at 12:14

2 Answers 2

2

The <action> node has been deprecated for a while now, you should stop using it right away. In fact, in Magento 2.2.4 (maybe even earlier) the node doesn't work anymore. With that being said, here are two different ways to accomplish your goal.

<?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" template="My_Module::history.phtml" />
    </body>
</page>

OR

<?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">
            <arguments>
                <argument name="template" xsi:type="string">My_Module::history.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

To add content into your block, you can add additional arguments into the layout xml file. You can also refer to your block by name inside of a plugin. This is the best answer I can give without further clarification to your question.


Update after getting clarification of question. Here is how a child block should be added.

<?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" template="My_Module::history.phtml">
            <arguments>
                <argument name="template" xsi:type="string">My_Module::history.phtml</argument>
            </arguments>
            <block class="Class\Of\My\Block" name="some.block.name" template="My_Module::some/template.phtml" />
        </referenceBlock>
    </body>
</page>

Inside of the file My_Module::history.phtml, you will need to have add one of these lines:

#this will render ALL child blocks in the same place
<?php echo $block->getChildHtml(); ?>

OR

#this will render only the specific child block you added
<?php echo $block->getChildHtml('some.block.name'); ?>
1
  • How do i add a block inside that ? Like <argument name="block" xsi:type="string">My_Module::Block/Block.php</argument> </arguments> ? May 31, 2018 at 16:01
0

I don't know if this is what you're looking for, you told that you want to add more than that, with content from a block. So you have to create a block with some function then you get the result funtion in your phtml from the block:

app/code/Vendor/Module/Block/Myblock.php

<?php
namespace Vendor\Module\Block;

use Magento\Checkout\Model\Session;

class Myblock extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        Session $session,
        array $data = []
    ) {
        $this->_session = $session;
        parent::__construct($context, $data);
    }

    public function getWebservice() {
        $content = //your logic here
        return $content;
    }
}

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

<?php $webserviceContent = $block->getWebservice(); ?> //You get what you returned from `public function getWebservice()`

If you want to add a new template

If you want to add a this content block in a new phtml inside history.phtml you can get it like this:

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

<?php $webserviceContent = $block->getWebservice(); ?> //You get what you returned from `public function getWebservice()`

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

echo $this->getLayout()->createBlock("Vendor\Module\Block\Myblock")->setTemplate("My_Module::history-suite.phtml")->toHtml();

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.