1.in product view phtml, use fancy box to add a click for rating star (fancy ajax)
<?php echo $this->getReviewsSummaryHtml($_product, 'star_ajax', true)?>
2. rewrite getReviewsSummaryHtml for choose ajax star phtml in config.xml:
<global> <blocks> <myreview> <class>Bysoft_Myreview_Block</class> </myreview> <review> <rewrite> <helper>Bysoft_Myreview_Block_Helper</helper> </rewrite> </review> </blocks>
3.write the block file:
<?php
class Bysoft_Myreview_Block_Helper extends Mage_Review_Block_Helper
{
protected $_availableTemplates = array(
'default' => 'review/helper/summary.phtml',
'short' => 'review/helper/summary_short.phtml',
'star' => 'review/helper/summary_star.phtml',
'star_ajax' => 'review/helper/summary_star_ajax.phtml',
);
}
4. write the summary_star_ajax.phtml file:
<?php if ($this->getReviewsCount()): ?>
<div class="ratings">
<?php if ($this->getRatingSummary()):?>
<div class="rating-box">
<div id="rating-<?php echo $this->getProduct()->getId();?>" class="rating" style="width:<?php echo $this->getRatingSummary() ?>%"
href="<?php echo $this->getUrl('review/product/list/id/'. $this->getProduct()->getId());?>"
><?php echo $this->getRatingSummary() ?></div>
</div>
<?php endif;?>
</div>
<?php elseif ($this->getDisplayIfEmpty()): ?>
<div class="rating-box">
<div class="rating" style="width:0%"><?php echo '0'?></div>
</div>
<?php endif; ?>
5. add fancy jquery on view.phtml
//fancy box for rating ajax $("#rating-<?php echo $_product->getId();?>").fancybox({ ajax : { type : "POST", } });
6. action for ajax post rating form config.xml:
<frontend> <routers> <review> <args> <modules> <Bysoft_Myreview before="Mage_Review">Bysoft_Myreview</Bysoft_Myreview> </modules> </args> </review> </routers> </frontend> </config>
7. controller file for rewrite post review function
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Review').DS.'ProductController.php';
class Bysoft_Myreview_ProductController extends Mage_Review_ProductController {
public function postAction()
{
$data = $this->getRequest()->getPost();
//$rating = array();
$rating[$this->getRequest()->getParam('ratingId')]=$this->getRequest()->getParam('optionId');
if (($product = $this->_initProduct()) && !empty($data)) {
$session = Mage::getSingleton('core/session');
/* @var $session Mage_Core_Model_Session */
$review = Mage::getModel('review/review')->setData($data);
/* @var $review Mage_Review_Model_Review */
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId(
$review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE)
)
->setEntityPkValue($product->getId())
->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
//直接通过验证
//->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED)
->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
->setStoreId(Mage::app()->getStore()->getId())
->setStores(array(Mage::app()->getStore()->getId()))
->save();
foreach ($rating as $ratingId => $optionId) {
Mage::getModel('rating/rating')
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
echo json_encode(array('status'=>1,'msg'=>$this->__('Your rating has been save, wait for approvement.')));
return;
}
catch (Exception $e) {
$session->setFormData($data);
$session->addError($this->__('Unable to post the review.'));
echo json_encode(array('status'=>2,'msg'=>$e->getMessage()));
return;
}
}
else {
$session->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
echo json_encode(array('status'=>3,'msg'=>$errorMessage));
return;
}
}
else {
echo json_encode(array('status'=>4,'msg'=>$this->__('Unable to post the review.')));
}
}
}
if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
$this->_redirectUrl($redirectUrl);
return;
}
$this->_redirectReferer();
}
}
?>