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?

  • please put the code of "Cart.php" file which you override. – Abhishek Panchal Mar 14 '17 at 16:13
  • Add this line after your namespace line in block file. use Magento\Framework\View\Element\Template; hope it works – Abhishek Panchal Mar 14 '17 at 16:27
  • No luck. Added the line, flushed my cache, but still blank. – KMan2012 Mar 14 '17 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. – Abhishek Panchal Mar 14 '17 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 Mar 14 '17 at 16:43
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.

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

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.