0

I am using a third-party module, I am added one extra feature to it, after upgrade the extension all my changes removed right, how to handle this situation, i mean how to override third-party module from my theme,

eg. I am updated one feature in model app\code\Magefan\Blog\Model\Post.php How to add this update from my custom theme.

2
  • if it is .php file you can't override using theme you have to create your custom module and then override that class Mar 23 at 4:33
  • Like model how to add app\code\Magefan\Blog\view\frontend\templates\widget\recent.phtm files in prefrence?
    – zus
    Mar 23 at 8:51
1

To achieve this you should override that class from your own module.

Create a Magento 2 module (lear here: https://devdocs.magento.com/videos/fundamentals/create-a-new-module/) and then add this in your 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="Magefan\Blog\Model\Post" type="NameSpace\YourModule\Model\Magefan\Post" />
</config>

Now your class NameSpace\YourModule\Model\Magefan\Post.php

<?php

namespace NameSpace\YourModule\Model\Magefan;

class Post extends \Magefan\Blog\Model\Post {

    public function yourNewFunction()
    {
        return true;
    }
}

Now, if you call this function from your theme, then it should work.

4
  • How to add my following 2 files : app\code\Magefan\Blog\view\frontend\templates\widget\recent.phtml & app\code\Magefan\Blog\view\frontend\templates\post\view\relatedposts.phtml,
    – zus
    Mar 23 at 7:57
  • To override you can create a new folder/file in your theme, app/design/frontend/Your_Theme/ThemeName/Magefan_Blog/templates/widget/recent.phtml. The idea is to place all the files inside Magefan_Blog with the same folder structure. And clear your cache. Mar 23 at 22:42
  • Model is rewrite using a custom module, for templates inside the theme folder, is it the correct way?
    – zus
    Mar 24 at 3:38
  • 1
    Yes, that way, when that module is disabled, the theme will know and will not care about it. Mar 24 at 4:18

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.