1

I want to override the buttons.phtml file in my module can anyone help me out

please. For more info, please find the below screenshot

enter image description here

  • Please write more description about your question – Ronak Chauhan Nov 28 '17 at 5:06
  • can you please take a look at screenshot @Ronak Chauhan – Learing_Coder Nov 28 '17 at 5:12
1

Flow below steps:

Step:1 First copy base file buttons.phtml at location vendor\magento\module-sales\view\frontend\templates\order\info\buttons.phtml to your current theme for eg. app\design\frontend\{Your_theme_name}\default\Magento_Sales\templates\order\info\buttons.phtml

Step:2 After change code in your theme file (app\design\frontend\{Your_theme_name}\default\Magento_Sales\templates\order\info\buttons.phtml)

Setp:3 Run Compiler command php bin/magento setup:di:compile and check your changes

Second method:

Setp:1

Create Buttons.php file (app\code\{Your-Vendor-name}\{Your-Module-Name}\Block\Order\Info\Buttons)

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Block of links in Order view page
 */
namespace {Your-Vendor-name}\{Your-Module-Name}\Block\Order\Info;

use Magento\Customer\Model\Context;

class Buttons extends \Magento\Sales\Block\Order\Info\Buttons
{
    /**
     * @var string
     */
    protected $_template = '{Your-Vendor-name}\{Your-Module-Name}::order/info/buttons.phtml';

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
        $this->_isScopePrivate = true;
    }

    /**
     * Retrieve current order model instance
     *
     * @return \Magento\Sales\Model\Order
     */
    public function getOrder()
    {
        return $this->_coreRegistry->registry('current_order');
    }

    /**
     * Get url for printing order
     *
     * @param \Magento\Sales\Model\Order $order
     * @return string
     */
    public function getPrintUrl($order)
    {
        if (!$this->httpContext->getValue(Context::CONTEXT_AUTH)) {
            return $this->getUrl('sales/guest/print', ['order_id' => $order->getId()]);
        }
        return $this->getUrl('sales/order/print', ['order_id' => $order->getId()]);
    }

    /**
     * Get url for reorder action
     *
     * @param \Magento\Sales\Model\Order $order
     * @return string
     */
    public function getReorderUrl($order)
    {
        if (!$this->httpContext->getValue(Context::CONTEXT_AUTH)) {
            return $this->getUrl('sales/guest/reorder', ['order_id' => $order->getId()]);
        }
        return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
    }
}

Setp:2

Create buttons.phtml file (app\code\{Your-Vendor-name}\{Your-Module-Name}\view\frontend\templates\order\info\buttons.phtml)

<div class="actions">
    <?php  $_order = $block->getOrder() ?>
    <?php if ($this->helper('Magento\Sales\Helper\Reorder')->canReorder($_order->getEntityId())) : ?>
        <a href="#" data-post='<?php /* @escapeNotVerified */ echo
        $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)
            ->getPostData($block->getReorderUrl($_order))
        ?>' class="action order">
            <span><?php /* @escapeNotVerified */ echo __('Reorder1') ?></span>
        </a>
    <?php endif ?>
    <a class="action print"
       href="<?php /* @escapeNotVerified */ echo $block->getPrintUrl($_order) ?>"
       onclick="this.target='_blank';">
        <span><?php /* @escapeNotVerified */ echo __('Print Order2') ?></span>
    </a>
    <?php echo $block->getChildHtml(); ?>
</div>

Setp:3 Create di.xml file (app\code\{Your-Vendor-name}\{Your-Module-Name}\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\Info\Buttons" type="{Your-Vendor-name}\{Your-Module-Name}\Block\Order\Info\Buttons" />
</config>

Setp:4

Run commands php bin/magento setup:upgrade & php bin/magento setup:di:compile and check your changes

  • if i direct copy the same location in my app/code/Mymodule will it works @Abdul – Learing_Coder Nov 28 '17 at 5:37
  • @Pranay K It is not work. Please check my updates (Secomd method) – Abdul Nov 28 '17 at 5:39
  • Can it be possible to override every template files like this with corresponding block @Abdul – Learing_Coder Nov 28 '17 at 8:45
  • Yes it is possible – Abdul Nov 28 '17 at 9:45
1

You can override buttons.phtml by override the Magento\Sales\Block\Order\Info\Buttons.php class

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\Info\Buttons" type="<Vendor>\<Module>\Block\Order\Info\Buttons"/>
</config>

<Vendor>\<Modulename>\Block\Order\Info\Buttons.php

<?php
namespace <Vendor>\<ModuleName>\Block\Order\Info;

use Magento\Customer\Model\Context;

class Buttons extends \Magento\Sales\Block\Order\Info\Buttons
{
    /**
     * @var string
     */
    protected $_template = '<Vendor_ModuleName>::order/info/buttons.phtml';
}

Now you can place you custom buttons.phtml in your module as per path view/frontend/order/info/buttons.phtml

Feel free to contact!

  • Can it be possible to override every template files like this with corresponding block @Ronak Cahuhan – Learing_Coder Nov 28 '17 at 8:45
  • Yes, You can override each template but some templates called directly from class also so you need to manage it. else by using layout you can override templates – Ronak Chauhan Nov 28 '17 at 8:51
  • i tried to override list template but it is not overridding the above way.can you just give me clarity.on these. @Ronak Chauhan – Learing_Coder Nov 28 '17 at 8:53
  • you can use layout override and give your custom path to it – Ronak Chauhan Nov 28 '17 at 8:58
  • <referenceBlock name="category.products.list"> <action method="setTemplate"> <argument name="template" xsi:type="string"><Vendor_ModuleName>::catalog/product/list.phtml</argument> </action> </referenceBlock> – Ronak Chauhan Nov 28 '17 at 8:59
0

Use sequence in your module.xml in order to load phtml file from your custom module

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

app/code/Vendor/Module/view/frontend/layout/sales_order_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>
        <referenceContainer name="page.main.title">
            <container name="order.actions.container" htmlTag="div" htmlClass="actions-toolbar order-actions-toolbar">
                <block class="Magento\Sales\Block\Order\Info\Buttons" as="buttons" name="sales.order.info.buttons" cacheable="false" template="Vendor_Module::order/info/buttons.phtml"/>
            </container>
        </referenceContainer>
    </body>
</page>

Now copy buttons.phtml from

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

and paste into

app/code/Vendor/Module/view/frontend/templates/order/info/buttons.phtml

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.