0

I have overriden the class Magento\Catalog\Block\Product\ProductList\Upsell with my own Company\Base\Block\Product\ProductList\Upsell.

The class Magento\Catalog\Block\Product\ProductList\Upsell extends \Magento\Catalog\Block\Product\AbstractProduct.

Now I have overridden \Magento\Catalog\Block\Product\AbstractProduct with \Company\Base\Block\Product\AbstractProduct and added a new method:

public function testAbstractMethod()
{
    echo "test abstract works";
}

I added

<?php exit($this->testAbstractMethod()) ?>

to the template of the class Magento\Catalog\Block\Product\ProductList\Upsell:

app\design\frontend\MyTheme\Base\Magento_Catalog\templates\product\list\items.phtml:

But I get:

Invalid method Company\Base\Block\Product\ProductList\Related\Interceptor::testAbstractMethod

But if I add the method testAbstractMethod() to the original core class \Magento\Catalog\Block\Product\AbstractProduct then it works.

So it looks like only the first class is getting overriden and if this class extends another class which is overriden by dependency injection, then it wont work and load the original class?

2 Answers 2

0

I would suggest you to simplify the solution. You should do the changes in one class and not override the abstract unless really necessary. You can still override the method from the abstract class in your new class.

5
  • I need to override the AbstractProduct class, since I need the method in every class which extends it.
    – Black
    Feb 20, 2020 at 13:53
  • Please let me know exactly which method you are trying to override or maybe post the function to understand the issue?
    – Arif Ahmad
    Feb 20, 2020 at 13:59
  • I don't try to override a method, I try to extend the class
    – Black
    Feb 20, 2020 at 14:20
  • I think your method contains private method calls but not sure as I don't have the actual method call. Good luck finding a fix for this. I can't keep assuming and replying
    – Arif Ahmad
    Feb 21, 2020 at 17:08
  • no it is public as you can see in my question.
    – Black
    Feb 24, 2020 at 8:02
0

You can't override abstract classes in Magento. You should try with a plugin: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

EDIT: I got it! you have to rename the method. It MUST start with one of: get, set, uns or has

So if you rename your method like this:

    public function getTestAbstractMethod()
    {
        echo "test abstract works";
    }

Will work.

Your exception is thrown in: \Magento\Framework\DataObject::__call

     * Set/Get attribute wrapper
     *
     * @param   string $method
     * @param   array $args
     * @return  mixed
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function __call($method, $args)
    {
        switch (substr($method, 0, 3)) {
            case 'get':
                $key = $this->_underscore(substr($method, 3));
                $index = isset($args[0]) ? $args[0] : null;
                return $this->getData($key, $index);
            case 'set':
                $key = $this->_underscore(substr($method, 3));
                $value = isset($args[0]) ? $args[0] : null;
                return $this->setData($key, $value);
            case 'uns':
                $key = $this->_underscore(substr($method, 3));
                return $this->unsetData($key);
            case 'has':
                $key = $this->_underscore(substr($method, 3));
                return isset($this->_data[$key]);
        }
        throw new \Magento\Framework\Exception\LocalizedException(
            new \Magento\Framework\Phrase('Invalid method %1::%2', [get_class($this), $method])
        );
    }

Magento's magic :D

8
  • Thats not an abstract class
    – Black
    Feb 20, 2020 at 13:52
  • I don't understand. How is renaming the method name helping?
    – Black
    Feb 20, 2020 at 14:56
  • the method _call check the first 3 characters of the methods, if it not contains that prefixes, throws an exception (take a look on that switch) Feb 20, 2020 at 14:57
  • But it has nothing do to with my question?
    – Black
    Feb 20, 2020 at 15:00
  • i shown you the reason of the exception. The override seems to be working as expected Feb 20, 2020 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.