cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 Override Magento Checkout Model Cart updateItems function

Magento 2 Override Magento Checkout Model Cart updateItems function

I Need to Override updateItems function from \Magento\Checkout\Model\Cart Also need to pass my custom helper class in __construct arguments . This is my __construct function of override class

namespace Vendor\Module\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session;
Class Cart extends \Magento\Checkout\Model\Cart
{
public function __construct(\Magento\Framework\Event\ManagerInterface $eventManager,
                            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
                            \Magento\Store\Model\StoreManagerInterface $storeManager,
                            \Magento\Checkout\Model\ResourceModel\Cart $resourceCart, Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\CatalogInventory\Api\StockStateInterface $stockState, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, ProductRepositoryInterface $productRepository,
                            \Vendor\Module\Helper\Data $helper, array $data = []
                            )
{
    $this->helper = $helper;
    parent::__construct($eventManager, $scopeConfig, $storeManager, $resourceCart, $checkoutSession, $customerSession, $messageManager, $stockRegistry, $stockState, $quoteRepository, $productRepository, $data);

}
}

After this i run setup:upgrade,compile, static content deploy commands. Also remove all folders in var. But when i pass the argument in __construct function. It is not working. It displays blank page. When i remove my arguments from __construct function. then page is loading.

10 REPLIES 10

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

Hello @ankurkinex

 

Try this,

/**
* @param \Magento\Core\Helper\Data $helper
*/

public function __construct(\Magento\Core\Helper\Data $helper)
{
$this->helper = $helper;

}

 

Issue solved, Please kudos and Accept as Solution. Cheers coding Smiley Happy

Manish Mittal
https://www.manishmittal.com/

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

Hello @ankurkinex

 

For more details, please follow:

 

https://alanstorm.com/instantiating-and-injecting-helpers-in-magento-2/

 

Kudos and Accept Solution. Cheers coding Smiley Happy

Manish Mittal
https://www.manishmittal.com/

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

I want to pass my module helper. It is causing issue.

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

Try below shared code:

use \Module_Namespace\Module_Name\Helper\Data;

public function __construct(Data $helper{
               $this->helper = $helper;
       }
 
Kudos and Accept as a solution, if it work. Cheers coding
Manish Mittal
https://www.manishmittal.com/

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

@Manish Mittal

I tried this but still showing blank page. here is my code. please check.

namespace Vendor\Module\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session;

Class Cart extends \Magento\Checkout\Model\Cart
{

    /**
     * @var \Vendor\Module\Helper\Data
     */
   private $helper;

    public function __construct(\Vendor\Module\Helper\Data $helper)
    {
        $this->helper = $helper;
    }

    public function updateItems($data)
    {
        foreach($data as $itemId=>$cartdetails)
        {

            $item = $this->_checkoutSession->getQuote()->getItemById($itemId);
            $product = $item->getProduct();
            $productid = $product->getId();
           // $productdetails = $this->productRepository->getById($productid);
            if(array_key_exists('warranty',$cartdetails))
            {
             //  $this->helper->CartUpdateCustomOption($itemId,$productid,$cartdetails['warranty']);
            }
        }
        return parent::updateItems($data); // TODO: Change the autogenerated stub
    }
}

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

Hello @ankurkinex

 

Still, you are using same "\Vendor\Module\Helper\Data $helper", Please remove this and use what I shared with you. 

Manish Mittal
https://www.manishmittal.com/

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

@Manish Mittal

This?

Try this,

/**
* @param \Magento\Core\Helper\Data $helper
*/

public function __construct(\Magento\Core\Helper\Data $helper)
{
$this->helper = $helper;

}

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

Yes if you want to use core helper then use this or if you want your custom helper then use other custom helpers which I shared in conversation.

Manish Mittal
https://www.manishmittal.com/

Re: Magento 2 Override Magento Checkout Model Cart updateItems function

You need to add parent class __construct() classname in your PHP file to use your custom helper in your PHP file,

Use below code,

<?php
namespace Vendor\Module\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session;

Class Cart extends \Magento\Checkout\Model\Cart
{

    /**
     * @var \Vendor\Module\Helper\Data
     */
    private $helper;

    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Checkout\Model\ResourceModel\Cart $resourceCart,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        \Magento\CatalogInventory\Api\StockStateInterface $stockState,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        ProductRepositoryInterface $productRepository,
        \Vendor\Module\Helper\Data $helper,
        array $data = []
    ) {
        $this->helper = $helper;
        parent::__construct($eventManager,$scopeConfig,$storeManager,$resourceCart,$customerSession,$messageManager,$stockRegistry,$stockState,$quoteRepository,$productRepository,$data);
    }

    public function updateItems($data)
    {
        foreach($data as $itemId=>$cartdetails)
        {

            $item = $this->_checkoutSession->getQuote()->getItemById($itemId);
            $product = $item->getProduct();
            $productid = $product->getId();
           // $productdetails = $this->productRepository->getById($productid);
            if(array_key_exists('warranty',$cartdetails))
            {
             //  $this->helper->CartUpdateCustomOption($itemId,$productid,$cartdetails['warranty']);
            }
        }
        return parent::updateItems($data); // TODO: Change the autogenerated stub
    }
}

Run php bin/magento setup:di:compile command and check.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial