0

Hi so i wrote this code to override a method in the woom cmstree module. But it seems like, something is wrong and i have used an entire day to figure it out, without any success.

My Vendor/MyModule/Block/Widget/Children.php

<?php

namespace Vendor\MyModule\Block\Widget;

class Children extends \Woom\CmsTree\Block\Widget\Children

{

protected $belongsToMenu = "";

public function prepareLinks($treeChildren, $first = false)
{
    $html = '';

    some random code

    return $html;
}
}

My etc/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="Woom\CmsTree\Block\Widget\Children" type="Vendor\MyModule\Block\Widget\Children" />
</config>

So this is how i did it. And i of course got the registration file and module.xml

But when i go to the page where this widget is used i get this error

[2021-03-04 20:56:22] main.CRITICAL: Error: Class 'Vendor\Mymodule\Block\Widget\Children' not found in /home/sonose2/public_html/generated/code/Vendor/Mymodule/Block/Widget/Children/Interceptor.php:7

I have been look at some examples but the are exact as mine and i have been trying alot of other stuff and now i ran out of ideas what to do.

3
  • are you sure there is no type? in the error you have Mymodule VS MyModule above
    – laurent
    Mar 4 at 22:11
  • Yes, like @laurent said. Check your registration.php and etc/module.xml files and make sure that you use MyModule instead of Mymodule (either way is OK, but it's really important it's the same everywhere throughout the module). Also, if your preference doesn't want to work, try moving the preference from etc/di.xml to etc/frontend/di.xml
    – Zankar
    Mar 5 at 6:45
  • laurent MyModule is just an example :) I have checked it. @Zankar But i'll try to move di.xml to frontend/di.xml Mar 5 at 8:10
0

It looks like your class hasn't been generated by the Magento core code. I think you need to run these three commands to generate it:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

Furthermore I would suggest you to look into plugins for modifying preexisting classes instead of using preferences in your di.xml file. The reason for this is that sometimes multiple preferences for the same class can cause conflicts and therefore issues.

4
  • Already tried that. Mar 4 at 21:58
  • Okay i'll do that, thanks for the advice with regard of using preferences Mar 4 at 21:59
  • It works on my local where the module is in app/code/vendor/module. But it seems to give me problem when i move the module to /vendor/module-name/. Can this give me problem? Mar 8 at 14:58
  • You should never ever "move" modules to the /vendor directory. This is where the Magento core modules live, and other modules installed with composer. Everything you do and write should happen in /app. The only reason why your module could be in /vendor is if it managed to become one of the essential Magento modules that are installed at the initial installation. So, keep your module in app/code/ and it should work fine. Mar 9 at 17:57
0

1)Create di.xml file and add following code:

<config>
    <type name="Magento\Catalog\Api\Data\ProductInterface">
        <plugin name="inchoo_catalog_product" type="Inchoo\Catalog\Plugin\Model\Product" />
    </type>
</config>

2)Create a class in plugin folder:

<?php
namespace vendor\module\Plugin\Model;
 
class Product
{
    public function beforeSetPrice(\Magento\Catalog\Model\Product $subject, $price)
    {
        $price += 10;
        return [$price];
    }
}
0

There wasn't anything wrong with this code. The only thing i did to make it work was to remove it entirely from the magento and added it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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