Magento2 Checkout Cart Total Block Class Not Overriding – How to Fix

blocksmagento2overrides

Try to Override Checkout cart total block \Magento\Checkout\Block\Cart\Totals#L11 but is not override.Getting the error:

Recoverable Error: Argument 4 passed to Magento\Checkout\Block\Cart\Totals::__construct() must be an instance of Magento\Sales\Model\Config, array given, called in /var/www/html/indiakalav1/app/code/BlueHorse/OverRide/Block/Cart/Totals.php on line 16 and defined in /var/www/html/indiakalav1/vendor/magento/module-checkout/Block/Cart/Totals.php on line 51
#0 /var/www/html/indiakalav1/vendor/magento/module-checkout/Block/Cart/Totals.php(51): Magento\Framework\App\ErrorHandler->handler(4096, 'Argument 4 pass...', '/var/www/html/i...', 51, Array)
#1 /var/www/html/indiakalav1/app/code/BlueHorse/OverRide/Block/Cart/Totals.php(16): Magento\Checkout\Block\Cart\Totals->__construct(Object(Magento\Framework\View\Element\Template\Context), Object(Magento\Customer\Model\Session), Object(Magento\Checkout\Model\Session), Array)

BLuehorse\OverRide\Block\Cart\Totals.php:

<?php
namespace BlueHorse\OverRide\Block\Cart;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
class Totals extends \Magento\Checkout\Block\Cart\Totals
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Config $salesConfig,
        array $layoutProcessors = [],
        array $data = []
    ) {
        $this->_salesConfig = $salesConfig;
        parent::__construct($context, $customerSession, $checkoutSession, $data);
        $this->_isScopePrivate = true;
        $this->layoutProcessors = $layoutProcessors;
    }

}

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\Checkout\Block\Cart\Totals" type="BlueHorse\OverRide\Block\Cart\Totals" />
</config>

module.xml

<?xml version="1.0"?>
<!--
/**
 * @copyright Copyright (c) 2015 X.commerce, Inc. (http://www.magentocommerce.com)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="BlueHorse_OverRide" setup_version="2.0.0"/>
 </config>

And registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'BlueHorse_OverRide',
    __DIR__
);

composer.json

{
    "name": "bluehorse/module-override",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0"
    },
    "type": "magento2-module",
    "version": "100.0.2",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "BlueHorse\\OverRide\\": ""
        }
    }
}

and checkout_cart_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="checkout_cart_item_renderers"/>
    <update handle="checkout_shipping_price_renderer"/>
    <body>
        <referenceContainer name="page.messages">
            <block class="Magento\Checkout\Block\Cart\ValidationMessages" name="checkout.cart.validationmessages"/>
        </referenceContainer>
        <referenceContainer name="content">
            <block class="Magento\Checkout\Block\Cart" name="checkout.cart" template="cart.phtml" cacheable="false">
                <container name="checkout.cart.items" as="with-items">
                    <container name="checkout.cart.form.before" as="form_before" label="Shopping Cart Items Before" htmlTag="div" htmlClass="rewards" before="checkout.cart.form"/>
                    <block class="Magento\Checkout\Block\Cart" name="checkout.cart.form" as="cart-items" template="cart/form.phtml">
                        <block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers" as="renderer.list"/>
                        <block class="Magento\Framework\View\Element\Text\ListText" name="checkout.cart.order.actions"/>
                    </block>
                    <container name="checkout.cart.widget" as="checkout_cart_widget" label="Shopping Cart Items After"/>


                    <!--
                    <block class="Magento\Checkout\Block\Cart\Coupon" name="checkout.cart.coupon" as="coupon" template="cart/coupon.phtml"/>
                    -->
                    <container name="checkout.cart.totals.container" as="totals" label="Shopping Cart Totals" after="checkout.cart.form" >
                        <block class="Magento\Checkout\Block\Cart\Totals" name="checkout.cart.totals" template="cart/totals.phtml">
                            <arguments>
                                <argument name="jsLayout" xsi:type="array">
                                    <item name="components" xsi:type="array">
                                        <item name="block-totals" xsi:type="array">
                                            <item name="component" xsi:type="string">Magento_Checkout/js/view/cart/totals</item>
                                            <item name="displayArea" xsi:type="string">totals</item>
                                            <item name="config" xsi:type="array">
                                                <item name="template" xsi:type="string">Magento_Checkout/cart/totals</item>
                                            </item>
                                            <item name="children" xsi:type="array">
                                                <!-- sort order for this totals is configured on admin panel-->
                                                <!-- Stores->Configuration->SALES->Sales->General->Checkout Totals Sort Order -->
                                                <item name="subtotal" xsi:type="array">
                                                    <item name="component"  xsi:type="string">Magento_Checkout/js/view/summary/subtotal</item>
                                                    <item name="config" xsi:type="array">
                                                        <item name="title" xsi:type="string" translate="true">Subtotal</item>
                                                        <item name="template" xsi:type="string">Magento_Checkout/cart/totals/subtotal</item>
                                                    </item>
                                                </item>
                                                <item name="shipping" xsi:type="array">
                                                    <item name="component"  xsi:type="string">Magento_Checkout/js/view/cart/totals/shipping</item>
                                                    <item name="config" xsi:type="array">
                                                        <item name="title" xsi:type="string" translate="true">Shipping</item>
                                                        <item name="template" xsi:type="string">Magento_Checkout/cart/totals/shipping</item>
                                                    </item>
                                                </item>
                                                <item name="grand-total" xsi:type="array">
                                                    <item name="component"  xsi:type="string">Magento_Checkout/js/view/summary/grand-total</item>
                                                    <item name="config" xsi:type="array">
                                                        <item name="title" xsi:type="string" translate="true">Order Total</item>
                                                        <item name="template" xsi:type="string">Magento_Checkout/cart/totals/grand-total</item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </argument>
                            </arguments>
                             <block class="Magento\Checkout\Block\Onepage\Link" name="checkout.cart.methods.onepage.bottom" template="cart/onepage-link.phtml" />
                        </block>
                    </container>
                    <!--
                    <block class="Magento\Checkout\Block\Cart" name="checkout.cart.methods.bottom" template="cart/methods.phtml">
                        <container name="checkout.cart.methods" as="methods" label="Payment Methods After Checkout Button">
                            <block class="Magento\Checkout\Block\Onepage\Link" name="checkout.cart.methods.onepage.bottom" template="onepage/link.phtml" />
                            <block class="Magento\Checkout\Block\QuoteShortcutButtons" name="checkout.cart.shortcut.buttons" />
                        </container>
                    </block>

                    <block class="Magento\Checkout\Block\Cart\Crosssell" name="checkout.cart.crosssell" template="Magento_Catalog::product/list/items.phtml" after="-">
                        <arguments>
                            <argument name="type" xsi:type="string">crosssell</argument>
                        </arguments>
                    </block>
                    -->
                </container>
                <container name="checkout.cart.noitems" as="no-items">
                    <block class="Magento\Checkout\Block\Cart" name="checkout.cart.empty" before="-" template="cart/noItems.phtml"/>
                    <container name="checkout.cart.empty.widget" as="checkout_cart_empty_widget" label="Empty Shopping Cart Content Before"/>
                </container>
            </block>
        </referenceContainer>
        <block class="Magento\Checkout\Block\Cart\Additional\Info" name="additional.product.info" template="cart/additional/info.phtml"/>
    </body>
</page>

Best Answer

As your new block extends \Magento\Checkout\Block\Cart\Totals I reckon you need to call the parent::__construct method with the original block method parameters and update your constructor like this:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\Config $salesConfig,
    array $layoutProcessors = [],
    array $data = []
) {
    parent::__construct($context, $customerSession, $checkoutSession, $salesConfig, $layoutProcessors, $data);
}