0

I'm trying to customize a Magento 2.1 cart page both by overriding the Block Cart and accessing some of the overridden elements in a new phtml template.

I hit a roadblock after overriding the Cart block however, as it appears the overridden block in never loading any cart form.phtml, custom or otherwise.

I have created the override by creating the app/code/Vendor/Module/Block/Cart.php file that extends \Magento\Checkout\Block\Cart

<?php
namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Cart extends \Magento\Checkout\Block\Cart {
    public function getContinueShoppingUrl() {
        return 'http://externalurl/from/config';
    }
}

I have also created app/code/Vendor/Module/etc/di.xml with the configuration

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

I know that the overridden block is at least loading, as I can get output from echoing items in the constructor and there appear to be no other errors in the log. However, when I go to the /checkout/cart/ page, where I expect the checkout details to load I get nothing - just the typical header and footer elements surrounding a blank page.

Any idea what I'm missing here?

8
  • please put the code of "Cart.php" file which you override. Commented Mar 14, 2017 at 16:13
  • Add this line after your namespace line in block file. use Magento\Framework\View\Element\Template; hope it works Commented Mar 14, 2017 at 16:27
  • No luck. Added the line, flushed my cache, but still blank.
    – KMan2012
    Commented Mar 14, 2017 at 16:33
  • Keep only Block file. I dont think you required di.xml file to override Block. remove "di.xml" file and try again. Commented Mar 14, 2017 at 16:35
  • I removed the di.xml file and the page definitely loads, but I do not believe that it is being overridden. Maybe this is too crude a check, but when I define a constructor and put a 'die' in it, nothing happens unless the di.xml file is configured.
    – KMan2012
    Commented Mar 14, 2017 at 16:43

1 Answer 1

0

In overriding the Cart, the template files are being searched for in the relative path of the new Module, instead of the parent module. The quick-and-dirty fix I came up with is to copy the contents of the module-checkout's /view/templates/ into /Vendor/Module/view/frontend/templates/

I suspect there are cleaner ways of doing this, but this method appears functional.

1
  • If you just want to change continue shopping url then just use plugin on that block and create afterGetContinueShoppingUrl() method in it.
    – Zefiryn
    Commented Mar 14, 2017 at 18:31

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.