1

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.

asked Nov 23, 2016 at 12:18
1
  • which error are display ? Commented Nov 23, 2016 at 12:45

2 Answers 2

1

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
{
answered Nov 23, 2016 at 12:47
1
  • 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 else Commented Jun 12, 2019 at 7:18
1

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.

answered Nov 20, 2017 at 5:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.