Question

I would like to add a review form in my custom module front end page. On this page, I have displayed several selected products.

I want to display a review form in front of each product each review form on my custom page.

Please see the below attached screenshot for better understand.

enter image description here

How can I achieve these things? Make sure each product have a separate review form and it should be default Magento review form.


Edit :

app/code/Namespace/ModuleName/view/frontend/templates/form.phtml

<div>
     <?php echo $block->getLayout()->createBlock("Magento\Review\Block\Form")->setTemplate('Namespace_ModuleName::form.phtml')->toHtml(); ?>
</div>

Using create a block and set template method I am able to display review form. but I am getting trouble in submitting the form. because In my custom review page form action parameter is not passing the product ID.

The current review form action is like

/review/product/post/

What actually review form action (It should be like below).

/review/product/post/id/14/

Why ID is not passing in the form action. Is it a wrong method to display review form? or is there any other way to display review form?


Update :

After creating a custom block and extend my block to \Magento\Review\Block\Form(default) then I am able to see product ID in form action URL.

app/code/Namespace/ModuleName/view/frontend/templates/form.phtml

<div>
     <?php echo $block->getLayout()->createBlock("Namespace\ModuleName\Block\ReviewForm")->setTemplate('Namespace_ModuleName::form.phtml')->toHtml(); ?>
</div>

Now I can see separate review form for a separate product. But in console, I am getting an error like below.

Uncaught SyntaxError: Unexpected token u in JSON at position 0

Because of the above error, I am not able to submit a review for all product. can anyone help me to solve out this problem?

Any help would be appreciated!

Was it helpful?

Solution

It seems that you have not added jsLayout in argument data. Since you have created block in php file, you may have following code in your module's di.xml file.

[Vendor][Module]\etc\di.xml

<type name="[Vendor]\[Module]\Block\ReviewForm">
    <arguments>
        <argument name="data" xsi:type="array">
            <item name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="review-form" xsi:type="array">
                        <item name="component" xsi:type="string">Magento_Review/js/view/review</item>
                    </item>
                </item>
            </item>
        </argument>
    </arguments>
</type>

Add this code and it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top