There are several routes in the Mage_Reviews
module, and you likely don't want to disable them all. Here's a list gleaned from Mage/Reviews/controllers/:
review/customer/(index)
- customer account view of their reviews
review/customer/view/id/{review id}
- customer account view of a single review
review/product/list/id/{product id}/[(category)/{category id}]
- list of reviews for a product
review/product/post
- process action for posting a review
review/product/view/id/{review id}
- individual view for a single product review - keep?
Bold = paths which you do/might want to map & redirect, italics = paths to keep.
Whereas there is content which can and should be used for the review requests, this seems best accomplished using an observer. To catch the review/product/list
route the observer should observe the controller_action_predispatch_review_product_list
event in the frontend event area and set an HTTP 301 redirect to the product page. The 301 will of course be used by search engines to update links and transfer some of the link juice.
<frontend>
<events>
<controller_action_predispatch_review_product_list>
<observers>
<your_module>
<class>your_module/observer</class>
<method>redirectReviewListToProductView</method>
</your_module>
</observers>
</controller_action_predispatch_review_product_list>
</events>
</frontend>
And in your module's observer, the redirect method:
/**
* Redirect requests for review page to product page,
* which now contains review contents. Relies on the
* accommodation of request object redirect in the
* elseif condition which is present in
* Mage_Review_ProductController::listAction().
*
* Observes
* controller_action_predispatch_review_product_list
*
* @see Mage_Review_ProductController::listAction()
* @see Mage_Core_Controller_Varien_Action::preDispatch()
*
* @param Varien_Event_Observer $observer
*/
public function redirectReviewListToProductView(Varien_Event_Observer $observer)
{
$request = $observer->getControllerAction()->getRequest();
/* @var $request Mage_Core_Controller_Request_Http */
//log to custom logfile
//Mage::log($request->getServer('HTTP_REFERER').': '.$request->getPathInfo(), Zend_Log::INFO, 'review_redirects.log',true);
//$toUrl will be empty if missing id param or invalid product ID.
$toUrl = Mage::getModel('catalog/product')->load($request->getParam('id'))->getProductUrl();
//$request->_forward() is protected (no idea why), so implement noroute (404) manually.
if (!$toUrl) {
$request->initForward();
$request->setActionName('noroute')->setDispatched(false);
}
else {
//review/product/list accommodates redirects, so this should work
$observer->getControllerAction()->getResponse()->setRedirect($toUrl);
$request->setParam('id',false);
}
}
// Invoke the Magento environment
require_once 'app/Mage.php';
Mage::app();
// Set up your own loop to to go through the reviews from the source cart.
// Logic to look up customer that has already been migrated into magento.
// So you have $_customer holding a Mage_Customer_Model_Customer
// IMPORTANT: Set up customer session.
// the rating/option model resource checks the customer session to get the customer ID.
$_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);
// Add the review
$_review = Mage::getModel('review/review');
->setEntityPkValue($_product->getId())
->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
->setTitle($row_source_review['Title'])
->setDetail($row_source_review['Review'])
->setEntityId(1)
->setStoreId($store)
->setStores(array($store))
->setCustomerId($_customer->getId())
->setNickname($_customer->getFirstname())
->save();
// Map your rating_id to your option_id with an array or something
$rating_options = array(
1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15)
);
// Now save the ratings
foreach($rating_options as $rating_id => $option_ids):
try {
$_rating = Mage::getModel('rating/rating')
->setRatingId($rating_id)
->setReviewId($_review->getId())
->addOptionVote($option_ids[$rating_value-1],$_product->getId());
} catch (Exception $e) {
die($e->getMessage());
}
endforeach;
Best Answer
There's nothing wrong with the posted code. I've checked it against 1.9.1.0 with sample data and using productId 337. I get a count of 6 each time and have been trying to break it, unsuccessfully (test script is below).
That means the error is in the code that you're not showing: the actual count() in the template file.
Test code used: