0

I have the MageFan Awesome Blog plugin for my Magento 2 instance. I've managed to override the recent posts template by adding my own BlogOverride plugin to my vendor.

Now I am trying to override the relatedposts.phtml file with my own code and I thought I did it the right way but nothing happened.

I first copied the file

vendor/magefan/module-blog/view/frontend/templates/catalog/product/relatedposts.phtml

to

app/code/Company/BlogOverride/view/frontend/templates/catalog/product/relatedposts.phtml

and changed the code to how I'd like it.

I then did a grep for the relatedposts.phtml file to see which XML file calls it, and it led me here:

vendor/magefan/module-blog/view/frontend/layout/catalog_product_view.xml

I copied it to

app/code/Company/BlogOverride/view/frontend/layout/catalog_product_view.xml

where my code looks like this:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content.aside">
            <block class="Magefan\Blog\Block\Catalog\Product\RelatedPosts"
                   name="product.info.blog.relatedposts"
                   as="relatedposts"
                   before='-'
                   template="Company_BlogOverride::catalog/product/relatedposts.phtml" />
        </referenceContainer>
    </body>
</page>

I then ran:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush

and nothing changed, it's still using the default relatedposts.phtml file. How can I override files created by external plugins?

Edit: I've tried the below suggested answer to no avail:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance dc" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="content.aside">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Company_BlogOverride::catalog/product/relatedposts.phtml</argument>
        </action>
    </referenceBlock>
</page>

2 Answers 2

0

Try to follow below code

add below code in your catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance dc" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="content.aside">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Company_BlogOverride::catalog/product/relatedposts.phtml</argument>
        </action>
    </referenceBlock>
</page>

Run below commands

php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy -f

1
  • didn't seem to take affect :/
    – treyBake
    Commented Nov 22, 2018 at 9:46
0

The key is to add it via the theme - not via a plugin.

I made this directory in my theme:

app/design/frontend/Company/default/Magefan_Blog/templates/catalog/product

where my relatedposts.phtml file now lives.

Refresh the page and huzzah! It works. I double checked by rm -rf app/code/Company and refreshing the page - my custom template still takes affect.

I think how Magento2 works in terms of the order of what it looks at, goes

Core -> Plugins -> Themes

So the correct place to override Plugins is the next step up - themes.

Create a folder inside your theme entitled PluginVendor_PluginName and override like that.

Your Answer

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

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