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.
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
Hello @ankurkinex
For more details, please follow:
https://alanstorm.com/instantiating-and-injecting-helpers-in-magento-2/
Kudos and Accept Solution. Cheers coding
I want to pass my module helper. It is causing issue.
Try below shared code:
use \Module_Namespace\Module_Name\Helper\Data;
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 } }
Hello @ankurkinex
Still, you are using same "\Vendor\Module\Helper\Data $helper", Please remove this and use what I shared with you.
This?
Try this,
/**
* @param \Magento\Core\Helper\Data $helper
*/
public function __construct(\Magento\Core\Helper\Data $helper)
{
$this->helper = $helper;
}
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.
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.