RSS

Monthly Archives: December 2011

How to add reviews section in view.phtml in magento

In the catalog.xml layout:
app/design/frontend/default/custom/layout/catalog.xml

I added the following block to show the review form in view.phtml :

<block type="review/product_view_list" name="product.info.product_additional_data" as="product_review_data" template="review/product/view/list.phtml">
<block type="review/form" name="product.review.form" as="review_form">
<block type="page/html_wrapper" name="product.review.form.fields.before" as="form_fields_before" translate="label">
<label>Review Form Fields Before</label>
<action method="setMayBeInvisible"><value>1</value></action>
</block>
</block>
</block>

From app/design/frontend/default/custom/layout/review.xml

 

Advertisements
 
Leave a comment

Posted by on Dec 29, 2011 in Magento

 

Tags: , ,

Restrict Product Reviews to Registered Users

In the custom template:
app/design/frontend/default/custom/template/review/form.phtml

I added the following check to see if the user is logged in:

<?php if ($this->helper('customer/data')->isLoggedIn()) : ?>

In place of

<?php if ($this->getAllowWriteReviewFlag()): ?>
 
Leave a comment

Posted by on Dec 29, 2011 in Magento

 

Tags:

how to get average product rating on a page in magento

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;
?>
 
1 Comment

Posted by on Dec 29, 2011 in Magento

 

Tags: , ,

Remove white image space upon resizing thumbnails in Magento

Magento has pretty neat image helper you can use to output your product images

Here is a better, more nicer solution to ditch the whit frame space around resized thumbnail images in Magento.

Here is a practical example of a code sample from default media.phtml file:
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href=”#” onclick=”popWin(‘<?php echo $this->getGalleryUrl($_image) ?>’, ‘gallery’, ‘width=300,height=300,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes’); return false;” title=”< ?php echo $this->htmlEscape($_image->getLabel()) ?>”><img src=”<?php echo $this-/>helper(‘catalog/image’)->init($this->getProduct(), ‘thumbnail’, $_image->getFile())->resize(56); ?>” width=”56″ height=”56″ alt=”< ?php echo $this->htmlEscape($_image->getLabel()) ?>” /></a>
</li>
< ?php endforeach; ?>

Change it by the following :


< ?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href=”#” onclick=”popWin(‘<?php echo $this->getGalleryUrl($_image) ?>’, ‘gallery’, ‘width=300,height=300,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes’); return false;” title=”< ?php echo $this->htmlEscape($_image->getLabel()) ?>”><img src=”<?php echo $this-/>helper(‘catalog/image’)->init($this->getProduct(), ‘thumbnail’, $_image->getFile())->keepFrame(false)->resize(56); ?>” alt=”< ?php echo $this->htmlEscape($_image->getLabel()) ?>” /></a>
</li>
< ?php endforeach; ?>

As you can see, all it took was to add the keepFrame(false) method call prior to calling the resize()method.

 

 

 

 
Leave a comment

Posted by on Dec 21, 2011 in Magento

 

Tags: , ,

Listing of all products in magento

Sometime we need to show all products in magento on anywhere. here is the code which get all the products in magento:

 

<?php /* listing of all product */
$allproduct = Mage::getModel(‘catalog/product’)->getCollection()->setOrder(‘entity_id’,’desc’)->getData();

foreach($allproduct as $product){
$obj = Mage::getModel(‘catalog/product’);
$productid = $product[‘entity_id’];
$_product = $obj->load($productid); // Enter your Product Id in $product_id
$productUrl = $_product->getProductUrl();
?>
<li><a href=”<?php echo $productUrl ?>”><?php echo $_product->getName(); ?></a></li>
<?php } ?>

This is well running in Magento 1.61.0.

 
Leave a comment

Posted by on Dec 12, 2011 in Magento

 

Tags: , , , , , , , , , ,

Show random product in magento

Sometime we need to display random products in magento. Here is the code to get the listing of random products in magento:

<?php // get Random prdocut list

$collection = Mage::getResourceModel(‘catalog/product_collection’);
Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection);
$collection->getSelect()->order(‘rand()’);
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 2;
$collection->setPage(1, $numProducts);

foreach($collection as $product){

echo $product->getName();

}

?>

 

 
Leave a comment

Posted by on Dec 12, 2011 in Magento

 

Tags: , , , , , ,

Get Product detail in Magento

In Magento eCommerce while working with catalog model, There arise the need to fetch product details from product id.

We can get all product details if we have product id.

But sometimes we only have product name, so we need to get product id for getting product details.

I am listing here both the method.

1) Product details from Product ID.

<?php
$model = Mage::getModel('catalog/product') //getting product model
$_product = $model->load($productid); //getting product object for particular product id
echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url  
?>

2) Product ID from Product Name

This is little bit complex. (If anybody has better way please post here)

<?php
$product_name = 'Test Product'; //product name
$model = Mage::getModel('catalog/product') //getting product model
$collection = $model->getCollection(); //products collection
foreach ($collection as $product) //loop for getting products
{                  
    $model->load($product->getId());
    $pname = $model->getName();
    if(strcmp($pname,$product_name)==0)
    {
    $id = $product->getId();
    }
}
echo 'Required ID->'.$id; //id of product
?>
 
Leave a comment

Posted by on Dec 12, 2011 in PHP

 

Tags: , , , , , , , ,

 
%d bloggers like this: