0

I am new to coding in Magento 2. I am trying to override a file using this guide https://webkul.com/blog/overriding-rewriting-classes-magento2/, and my file compiles without error, but changes don't take place on the frontend. I have put the file in a Hello World module called MyTest. My di.xml file located at app/code/[My_Vendor]/MyTest/etc/di.xml is

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="[My_Vendor]\MyTest\Rewrite\Pricing\Render\FinalPriceBox" /> </config> 

and my FinalPriceBox.php file which overwrites vendor/magento/module-catalog/Pricing/Render/FinalPriceBox.php and is located at app/code/[My_Vendor]/MyTest/Rewrite/Pricing/Render/FinalPriceBox.php is

<?php namespace [My_Vendor]\MyTest\Rewrite\Pricing\Render; class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox { public function _renderAmountMinimal() { $id = $this->getPriceId() ? $this->getPriceId() : ‘product-minimal-price-’ . $this->getSaleableItem()->getId(); $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem()); if ($amount === null) { return ‘’; } return $this->renderAmount( $amount, [ ‘display_label’ => __(‘As low as mytest’), ‘price_id’ => $id, ‘include_container’ => false, ‘skip_adjustments’ => true ] ); } } ?> 

I am not sure what is going wrong. I do not see any new error messages, and I know that changing the original file does cause the changes I want on the frontend. I also know that the module is enabled and correctly prints Hello World on a test page. The guide did not have a composer.json file so my module doesn't have one, and I am not sure if that could cause the problem. I was wondering if anyone had advice on what might be going wrong

Update:

I tried the two answers below, but for some reason it caused a page glitch as in the pictures below where the product names have been covered for anonymity.

Normal Page

Correct Page

Glitched Page

Glitched Page

When I check the exception.log file I see 3 errors

"URL key for specified store already exists"

"Unable to retrive deployment version of static file from the file system"

and

"Reflection Exception Class[My_Vendor]\MyTest\Rewrite\Pricing\Render\FinalPriceBox does not exist at [My_Website_Folder]/vendor/magento/framework/Code/Reader/ClassReader.php"

I have tried looking up each of these errors, but so far the solutions haven't worked. I also tried running php bin/magento dev:di:info "[My_Vendor]\MyTest\Rewrite\Pricing\Render\FinalPriceBox" which shows the class as being configured.

2
  • after implementation, you need to run the upgrade, deploy command. have you done that? Commented Dec 21, 2023 at 9:54
  • I did run php bin/magento setup:static-content:deploy Commented Dec 21, 2023 at 15:14

2 Answers 2

0
app/code/[My_Vendor]/MyTest/Rewrite/Pricing/Render/FinalPriceBox.php 

code like this.

 <?php namespace [My_Vendor]\MyTest\Rewrite\Pricing\Render; class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox { public function renderAmountMinimal() { $id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId(); $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem()); if ($amount === null) { return ''; } return $this->renderAmount( $amount, [ ‘display_label’ => __('As low as mytest'), ‘price_id’ => $id, ‘include_container’ => false, ‘skip_adjustments’ => true ] ); } } ?> 
1
  • Thank you for your help. I tried the code, but as mentioned in the comment for the other answer, for some reason I still have the glitch which is now shown in the updated question. Commented Dec 20, 2023 at 20:10
0

The reference you mentioned, in that they are overriding the _getProductCollection() which is available in the Magento\Catalog\Block\Product\ListProduct.php

And in your case the function you are overriding in your custom file using preference is not available in the vendor file which is _renderAmountMinimal

The function name is renderAmountMinimal not _renderAmountMinimal, so try to use without the underscore(_) sign

so,

<?php namespace [My_Vendor]\MyTest\Rewrite\Pricing\Render; class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox { public function renderAmountMinimal() { $id = $this->getPriceId() ? $this->getPriceId() : 'product-minimal-price-' . $this->getSaleableItem()->getId(); $amount = $this->minimalPriceCalculator->getAmount($this->getSaleableItem()); if ($amount === null) { return ''; } return $this->renderAmount( $amount, [ 'display_label' => __('As low as mytest'), 'price_id' => $id, 'include_container' => false, 'skip_adjustments' => true ] ); } } 

Hope this will work for you :)

4
  • Thank you for the response, but for some reason I still have a glitch. I updated the question with the picture of the glitch. Commented Dec 20, 2023 at 20:08
  • First of all you are running in default or production mode, if currently developing the site please enable the developer mode for your site, check deploy mode php bin/magento deploy:mode:show if is not set to developer then please use php bin/magento deploy:mode:set developer command. with this you can see error which you mentioned as page glitch with my answer. Commented Dec 21, 2023 at 5:54
  • I tried developer. What should I do for debugging after that? Commented Dec 22, 2023 at 14:47
  • Instead of the glitch you will see the actual error in your page, or you can check the log files for the errors Commented Dec 23, 2023 at 7:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.