I'm trying to override one protected function in Magento 2, but I am not able to achieve it. I have edited following files.
di.xml
<preference for="Magento\Checkout\CustomerData\DefaultItem" type="XYZ\Checkout\CustomerData\DefaultItem" />
XYZ\Checkout\CustomerData\DefaultItem
<?php
namespace \XYZ\Checkout\CustomerData;
class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{
 protected function doGetItemData()
 {
 $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
 return [
 'options' => $this->getOptionList(),
 'qty' => $this->item->getQty() * 1,
 'item_id' => $this->item->getId(),
 'configure_url' => $this->getConfigureUrl(),
 'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
 'product_name' => $this->item->getProduct()->getName(),
 'product_sku' => $this->item->getProduct()->getSku(),
 'product_url' => $this->getProductUrl(),
 'product_has_url' => $this->hasProductUrl(),
 'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
 'product_price_value' => $this->item->getCalculationPrice(),
 'product_image' => [
 'src' => $imageHelper->getUrl(),
 'alt' => $imageHelper->getLabel(),
 'width' => $imageHelper->getWidth(),
 'height' => $imageHelper->getHeight(),
 ],
 'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
 && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
 ];
 }
}
After clearing caches, when I add product to cart, there is no increment in minicart notification and I'm not able to open minicart and checkout/cart page is coming blank. I have checked in quote_item table and product has been added to the cart. So I think my overriding is not working.
- 
 which error are display ?Rakesh Jesadiya– Rakesh Jesadiya2016年11月23日 12:45:41 +00:00Commented Nov 23, 2016 at 12:45
 
2 Answers 2
You have keep extra slash in namespace line.
Please remove slash before class namespace in your defaultItem.php file.
Replace
<?php
namespace \XYZ\Checkout\CustomerData;
class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{
With
<?php
namespace XYZ\Checkout\CustomerData;
class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{
 - 
 can i do this? after canApplyMsrp add this following 'quantity_enabled' => $this->configHelper->getConfigEnabled() then call it in HTML file or do i need to pass it anywhere elsejibin george– jibin george2019年06月12日 07:18:42 +00:00Commented Jun 12, 2019 at 7:18
 
I have been working/testing this for 6 hours before finding this post: Working Code
I don't know why Rakesh's answer has been accepted as a solution because I tried with it too many times without success.
In the given post, it's said that it's not possible to override a protected method so the developer tried with another solution that effectively works.