3

The may well be too broad of a question, but I'm having extreme difficulty finding any answers. I want to know if its possible to add a phtml block/view to a product page when you have a theme installed without overwriting what the theme is doing, just injecting alongside.

Everything I've found references creating your own templates/theme which defeats the purpose of what I'm trying to do, more or less just trying to create a helper module that can add some extra content while also getting the benefit of a paid theme.

(Just to note, have been scouring Adobe's documentation and other stack overflow articles, if there is already an answer to a similar question, please link.)

0

2 Answers 2

2

It sounds like you want to add some code to a product page without overriding files from your third party theme.

This can be achieved through a custom module. The following example would add a note from the custom modules template file below the add to cart button on simple product pages:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.form.content">
            <block class="Magento\Catalog\Block\Product\View" name="product.info.simple.productpagenote"
                   template="Max_ProductPageNotes::productpagenote.phtml">
            </block>
        </referenceBlock>
    </body>
</page>

The following line of code targets the add to cart form:

<referenceBlock name="product.info.form.content">

Within this tag we insert our custom template file:

<block class="Magento\Catalog\Block\Product\View" name="product.info.simple.productpagenote" template="Max_ProductPageNotes::productpagenote.phtml">

The content from productpagenote.phtml is then rendered below the add to cart button.

0

I am not getting your point about theme overriding, but you can inject the block class and phtml from your custom layout theme like this.

<block class="Companyname\Vendorname\Block\Product\View\Custom" name="customname" template="Companyname_Vendorname::product/view/custom.phtml">
2
  • Describing this is difficult. For example, given an installed 3rd party theme that already fully replaces the built-in Magento product page, inserting an extra 1st party widget on the product page alongside the theme's replacement. Would I have to copy the theme's product replacement code and then override it with my own? Or is there a way to do an injection without fully replacing the installed theme's phtml template? Sep 9, 2022 at 14:43
  • You can override the custom theme's phtml in your custom module. You can also take reference from @Max answer. Sep 12, 2022 at 7:11

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.