Frage

I was trying to override \Magento\Checkout\Controller\Cart\Index controller.

I created a di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">   
    <preference for="Magento\Checkout\Controller\Cart\Index" type="Test\Checkout\Controller\Rewrite\Checkout\Cart\Index" />
</config>

and created controller Test\Checkout\Controller\Rewrite\Checkout\Cart\Index.php

 <?php

namespace Test\Checkout\Controller\Rewrite\Checkout\Cart;

use Magento\Framework\Data\Form\FormKey;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;

class Index extends \Magento\Checkout\Controller\Cart\Index
{
    protected $scopeConfig;
    protected $formKey;
    protected $cart;
    protected $product;

    const FREE_INCLUSIONS = 'additionalServices/general/free_inclusions';

    public function __construct(
        FormKey $formKey,
        Product $product,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart
        );
        $this->scopeConfig = $scopeConfig;
        $this->formKey = $formKey;
        $this->cart = $cart;
        $this->product = $product;
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $free_inclusions =  array_filter(explode(',', $this->scopeConfig->getValue(self::FREE_INCLUSIONS, $storeScope)));
      
        return parent::execute();
    }
}

but I'm still getting an error like Type Error occurred when creating object.

Can anyone help me out to override

Thanks in advance.

War es hilfreich?

Lösung

Try to replace below code in your file

<?php

namespace Test\Checkout\Controller\Rewrite\Checkout\Cart;

use Magento\Framework\Data\Form\FormKey;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;

class Index extends \Magento\Checkout\Controller\Cart\Index
{
    protected $scopeConfig;
    protected $formKey;
    protected $cart;
    protected $product;

    const FREE_INCLUSIONS = 'additionalServices/general/free_inclusions';

    public function __construct(
        FormKey $formKey,
        Product $product,
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart,
            $resultPageFactory
        );
        $this->scopeConfig = $scopeConfig;
        $this->formKey = $formKey;
        $this->cart = $cart;
        $this->product = $product;
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $free_inclusions =  array_filter(explode(',', $this->scopeConfig->getValue(self::FREE_INCLUSIONS, $storeScope)));
      
        return parent::execute();
    }
}

In parent __construct() you have to pass one more argument $resultPageFactory. Add that and try to run below commands once and try check.

php bin/magento setup:upgrade
php bin/magento setup:di:compile

Hope this will help!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top