2

I'm trying to implement a basic m2 class override following the M2 Fundamentals Course and it is not working. Here is what I have in my Training/Test/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Catalog\Model\Product" type="Training\Test\Model\Testproduct" />

</config>

And inside the Training\Test\Model\Testproduct.php

<?php
namespace Training\Test\Model;

class Testproduct extends \Magento\Catalog\Model\Product
{

 public function getPrice() {
    return 3;
 }
} 

I understand it's better to do plugins / events, but my question is why is the above not working?

The module is installed and I also ran the following:

php bin/magento cache:flush
rm -Rf var/cache/*
rm -Rf var/di/
rm -Rf var/generation/*
rm -Rf var/page_cache/*
php bin/magento setup:di:compile

It also shows when runnning: php bin/magento module:status

List of enabled modules:
...
Training_Test
5
  • Looks like your module is not installed, isn't it?
    – KAndy
    Nov 30, 2015 at 8:06
  • It is installed. I also ran the following: php bin/magento cache:flush 993 rm -Rf var/cache/* 994 rm -Rf var/di/ 995 rm -Rf var/generation/* 996 rm -Rf var/page_cache/* 997 php bin/magento setup:di:compile
    – CD Brian
    Nov 30, 2015 at 14:36
  • Can you describe how are you setup magento and add module to it?
    – KAndy
    Nov 30, 2015 at 18:08
  • How I setup Magento? I followed the install guide and everything works fine on that front. I then followed the tutorial for building the module and ran: php -f bin/magento module:enable Training_Test I also, verified that if I change this module code to work as a plugin, it works properly. But overriding a class as above does not work.
    – CD Brian
    Nov 30, 2015 at 18:23
  • In my view you should not override Magento classes. You may want to use Magento Plugins and extend functionality with your custom logic.
    – Max Pronko
    Jan 21, 2016 at 0:36

1 Answer 1

10

You can try this simple module to override \Magento\Catalog\Model\Product class. It 100% works on my local Magento 2 test environment.

Base folder: app\code

Directory tree:

└── Example
    └── OverrideModel
        ├── composer.json
        ├── etc
        │   ├── frontend
        │   │   └── di.xml
        │   └── module.xml
        ├── Model
        │   └── Product.php
        └── registration.php

File: Example/OverrideModel/composer.json

{
    "name": "example/overridemodel",
    "description": "OverrideModel module for Magento 2",
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-catalog": "~100.0"
    },
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Example\\OverrideModel\\": ""
        }
    }
}

File: Example/OverrideModel/registration.php

<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Example_OverrideModel', __DIR__);

File: Example/OverrideModel/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Example_OverrideModel" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

File: Example/OverrideModel/etc/frontend/di.xml

<?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\Model\Product" type="Example\OverrideModel\Model\Product" />
</config>

File: Example/OverrideModel/Model/Product.php

<?php

namespace Example\OverrideModel\Model;

class Product extends \Magento\Catalog\Model\Product {

    public function getPrice()
    {
        return 3;
    }
}
1
  • Wish there was a 'highlight' function. Took me ages to see 'extends' rather than 'implements' and finally everything clicked. Mar 15, 2018 at 9:04

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.