1

By default magento 2 review structure is like this -

/templates/review.phtml and inside it you have - /product/view/list.phtml and Magento_Review/templates/form.phtml files.

form.phtml file is called using <?= $block->getChildHtml() ?> in review.phtml file

What I want to do - remove that getChildHtml part from review.phtml file to make form disappear. So far I get it to work. I have successfully removed my review form. Now I want to add my form back but inside list.phtml file and call it using getChildHtml. How can I do this? So far what I have tried ended with no luck.

Final result should be that in list.phtml file I call my form like this - <?= $block->getChildHtml('review_form') ?> and it should appear in place I put him

Thank you for any advice! :)

  • Can you add the full path of file where you want to add the review form? – Sukumar Gorai Dec 18 '18 at 10:19
  • @SukumarGorai Inside - /module-review/view/frontend/templates/product/view/list.phtml – help Dec 18 '18 at 10:20
  • And where are you calling the list.phtml? – Sukumar Gorai Dec 18 '18 at 10:25
  • @SukumarGorai In its default place - inside review.phtml file. Basically what I want to do - remove form from review.phtml file and call it inside list.phtml file – help Dec 18 '18 at 10:33
2

You can do this by following steps:

Step 1(Adding form in review list.phtml):

Create review_product_listajax.xml under below location:

app/design/frontend/[Vendor]/[theme]/Magento_Review/layout/review_product_listajax.xml

with below content:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="product.info.product_additional_data">
        <block class="Magento\Review\Block\Form" name="product.review.form" as="review_form">
            <block class="Magento\Framework\View\Element\FormKey" name="formkey"/>
            <container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
        </block>
    </referenceBlock>
</layout>

Step 2:

Copy the below file:

vendor/magento/module-review/view/frontend/templates/product/view/list.phtml

to your theme:

app/design/frontend/[Vendor]/[theme]/Magento_Review/templates/product/view/list.phtml

and add the <?= $block->getChildHtml() ?> code block to desired location of that file.

Step 3(remove form from default location):

Create catalog_product_view.xml to below location:

app/design/frontend/[Vendor]/[theme]/Magento_Review/layout/catalog_product_view.xml

and add the below content:

<?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>
        <referenceBlock name="reviews.tab">
            <referenceBlock name="product.review.form" remove="true"/>
        </referenceBlock>
    </body>
</page>

Above code will remove the default form place and add ti list block. Flush the cache and test.

  • Thank you a lot! It worked. – help Dec 18 '18 at 11:15
  • Welcome. Happy Coding! You can also upvote the answer. – Sukumar Gorai Dec 18 '18 at 11:18
  • Hey, just noticed that my reviews arent saved anymore. Submitted review but didnt appear in reviews (also no message was shown that review successfully saved). And I also noticed that in console there is error - "magento 2 syntaxError: Unexpected token u in JSON at position 0" Can you help please? – help Dec 18 '18 at 13:47
  • You need to check the submit url of your form if its not showing correctly then you need to add the url correctly . – Sukumar Gorai Dec 18 '18 at 13:49
  • 1
    I need to check it then. – Sukumar Gorai Dec 18 '18 at 14:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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