Tuesday, 3 June 2014

show product review oin custom list page Magento

HI there are some times we need to make the own list pages of products . in that the review doesnot show on them . To show reviews on them use below code



1) if you intend to display the average rating of each product, add this helper method inside the page(like list.phtml):
 

<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)  ->load($_product->getId());
?>                                                                          
// Rating Percentage showing of a product
<div class="rating">(<?php echo $summaryData['rating_summary']; ?>%)</div>   
 
2) Get the product Review anywhere in magento


<?php // review of a product at any page
$_reviews = Mage::getModel('review/review')->getResourceCollection();
$_reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addEntityFilter('product', $product->getId())
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes();
$avg = 0;
$ratings = array();
if (count($_reviews) > 0){
foreach ($_reviews->getItems() as $_review): ?>
<?php foreach( $_review->getRatingVotes() as $_vote ): ?>
<?php $ratings[] = $_vote->getPercent(); ?>
<?php endforeach; ?>
<?php endforeach;
$avg = array_sum($ratings)/count($ratings); }
?>
<?php if($avg > 0):?>
<div class=”ratings”>
<div class=”rating-box”>
<div class=”rating” style=”width: <?php echo ceil($avg) ; ?>%;”></div>
</div>
</div>
<?php endif;?>
3) Get the Review Count of a product in any page like list.phtml

<?php                                                                         // review count of a product
echo $reviewCount = $_product->getRatingSummary()->getReviewsCount() ? $_product->getRatingSummary()->getReviewsCount(): 0;
?>

Tuesday, 27 May 2014

Get cart details in Magento

You can get the cart details with the following code

<?php
$cart = Mage::getModel('checkout/cart')->getQuote(); //to get cart detail
foreach ($cart->getAllItems() as $item) {
   echo  $productId = $item->getProduct()->getId(); // product id in cart
   echo $productPrice = $item->getProduct()->getPrice(); // product price in cart
$product = Mage::getModel('catalog/product')->load($productId); // load product to get all details of product
//load the categories of this product
$categoryCollection = $product->getCategoryCollection();
$catIds = $product->getCategoryIds(); // to get all category ids of products in cart
$catCollection = Mage::getResourceModel('catalog/category_collection')  // to get all all details of categories of products in cart
                     //->addAttributeToSelect('name')
                     //->addAttributeToSelect('url')
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $catIds)
                     ->addIsActiveFilter();

foreach($catCollection as $cat){
 // print_r($cat->getData());
 $catname[] =  $cat->getName();
  //echo $cat->getUrl();
}

}
print_r($catname); //category names array
?>