- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 Override Magento Checkout Model Cart updateItems function
I want to pass my module helper. It is causing issue.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 Override Magento Checkout Model Cart updateItems function
Try below shared code:
use \Module_Namespace\Module_Name\Helper\Data;
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 Override Magento Checkout Model Cart updateItems function
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 } }
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 Override Magento Checkout Model Cart updateItems function
This?
Try this,
/**
* @param \Magento\Core\Helper\Data $helper
*/
public function __construct(\Magento\Core\Helper\Data $helper)
{
$this->helper = $helper;
}
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Magento 2 Blogs/Tutorial