0

I have created a new module (to extend checkout):

<?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="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

I want to extend from the success page:

<?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\Checkout\Block\Onepage\Success"
                type="Vendor\Module\Block\Checkout\Onepage\Success"/>
</config>

And use that class in checkout.success instead of the original success class plus changing to my own success template:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.success" remove="true" />
        <block class="Vendor\Module\Block\Checkout\Onepage\Success"
               name="checkout.success"
               template="Vendor_Module::success.phtml"
               cacheable="false"/>
    </body>
</page>

The result is a checkout success page loading just fine. Yet the checkout.success block just won't render.


I have to do this to get it to work:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.success">
            <action method='setTemplate'>
                <argument name='template' xsi:type='string'>Vendor_Module::success.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Okay so I get setting the preference for the original success class will obviously make Magento take my new class for the template too.

What I do not get is why my original attempt won't work and why with this the block won't render at all. If I leave out the class attribute in the xml definition for the template. Magento will run into an error because it then tries to load an empty string as a class name. (which of course fails) The class just extends from the original class and within that class is currently nothing that would prevent it from loading. In fact, the class is never loaded itself at all (checked with xdebug).

namespace Vendor\Module\Block\Checkout\Onepage;

use Magento\Directory\Model\Currency;
use Magento\Sales\Api\Data\OrderPaymentInterface;

class Success extends \Magento\Checkout\Block\Onepage\Success
{
    private $order;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = [])
    {
        parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
        /** @var \Magento\Sales\Model\Order $order */
        $this->order = $this->_checkoutSession->getLastRealOrder();
    }

    // ...

PS: I run Magento in developer mode with symlinked sources and clear the cache and everything before I load the page.

  • So you want to call custom success Block and phtml, right? – kunj Jul 19 '18 at 5:52
  • Yes, I want to use a custom class for the checkout.success block. – steros Jul 19 '18 at 5:54
0

This is because you're removing the block, with remove="true". So even though you've added the block back the remove is still true. Depending on your version of Magento you may be able to use remove="false" on your block, but there was a bug in Magento 2 for a long time that prevented this from working.

If remove="false" doesn't work you will need to use a different name for the block.

| improve this answer | |
  • Hi Ben, I tried it without the removing too. There is no difference. – steros Jul 18 '18 at 12:17
  • Damn it, I presume you flushed cache afterwards? – Ben Crook Jul 18 '18 at 12:22
  • I killed the whole server several times (using vagrant). – steros Jul 18 '18 at 12:23
  • Could the order of checkout and block have an impact? Magento\Checkout\Block\Onepage\Success vs Vendor\Module\Block\Checkout\Onepage\Success. If not I don't think I can help any further sorry. – Ben Crook Jul 18 '18 at 13:45
0

Try this.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <block class="Vendor\Module\Block\Checkout\Onepage\Success"
               name="checkout.success"
               template="Vendor_Module::success.phtml"
               cacheable="false"/>
    </body>
</page>

Please put this code in your module file at Vendor/Module/view/frontend/layout/checkout_onepage_success.xml

| improve this answer | |
  • That's exactly what I did. See the third code paste. – steros Jul 19 '18 at 6:10
  • yes, but I think you added this <referenceBlock name="checkout.success" remove="true" /> and that's why Magento maybe not showing any content on success page. – kunj Jul 19 '18 at 7:05
  • As written in the other answer, I have tried both. With and without the removing. – steros Jul 19 '18 at 7:06
  • this code should work if not working then maybe other module calling same file or our code is not proper. – kunj Jul 19 '18 at 8:10
  • I have used your code and it is working for me – kunj Jul 19 '18 at 8:16

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.