Magento: Loading Review Form on an External Page
In Magento, the default behavior is to display the review form as a section within the product reviews page. The URL for this section follows the pattern www.domain.com/(producturl)-reviews#review-form. However, you may want to load the review form onto its own unique page with the URL www.domain.com/(producturl)-review-form, where it exists independently of the main reviews page.
This approach can be achieved by creating a new template file for the review form and modifying the controller action to redirect to this new page. Here’s a step-by-step guide:
Step 1: Create a New Review Form Template
Navigate to your Magento theme directory, typically located at app/design/frontend/{package}/{theme}/template. Under the ‘catalog’ folder, create a new file named review_form.phtml.
Paste the following code into this new file. This is the default Magento review form template with some minor modifications:
<?php
$productId = Mage::registry('current_product')->getId();
$reviewsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
$reviewsCollection->addFilterBy SKU, $productSKU;
$reviewsCollection->setPageSize(1);
Step 2: Modify the Review Controller
Next, you need to modify the Mage_Catalog_ReviewController to redirect to your new review form page after submitting a successful review.
Open the file located at app/code/core/Mage/Catalog/controllers/ReviewController.php.
Find the postAction() method and replace it with the following code:
public function postAction()
{
$productId = (int)$this->getRequest()->getParam('id');
$product = Mage::getModel('catalog/product')->load($productId);
if (!$product || $product->isLockedForChanges()) {
$this->_forward('noRoute');
}
$session = Mage::getSingleton('catalog/session');
if ($session->getFormSubmit() === 1) {
$session->unsFormSubmit();
return $this->_redirect('*/*/post', array('_secure' => true));
}
}
This code checks if a review has been successfully submitted, and if so, redirects to the new review-form page.
Step 3: Create the New Review Form URL
To create the new URL for your review form page, you need to add a rewrite configuration to your Magento system.xml file. Open the app/code/core/Mage/Catalog/etc/system.xml file and look for the <routes> section.
Add the following entry inside this section:
<catalog_review_form translate="label">
<label>Review Form</label>
<frontName>reviewform</frontName>
<args/>
</catalog_review_form>
This configuration creates a new route called reviewform for your review form page.
Step 4: Update the Catalog URL Rewrite
Finally, you need to update the catalog URL rewrite with the new URL pattern for the review form. Open the app/code/core/Mage/Catalog/etc/catalog.xml file and look for the <rewrite> section under the <catalog_product_view> node.
Add a new entry inside this section:
<rewrite>
<frontendRoute>reviewform</frontendRoute>
<id>catalog_product_reviewform</id>
<type>product</type>
<action>review/form/</action>
<frontName>catalog</frontName>
<params/>
</rewrite>
This configuration maps the reviewform route to the review form action.
Conclusion
In this blog post, we explained how to load a Magento product review form on an external page using custom template files and controller modifications. By following these steps, you can create a unique URL for each review form while keeping it independent of the main reviews page structure.
This approach provides more flexibility in your site’s layout and navigation. However, it’s important to test this setup carefully, especially if your store has a large number of products or complex configuration settings.
Remember that modifying Magento core files can be risky, so always backup your data before making changes to the codebase.
If you’re looking for more advanced customizations and support in managing your Magento site, consider reaching out to Laravel Company. Our team of experienced professionals provides top-notch Magento development services tailored to meet your business needs.