Magento Ajax Rating Star

  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();
	}
}
?>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值