How to call core template file in magento

1) For calling a core template file in .phtml file

<?php echo $this->getLayout()->createBlock(‘core/template’)->setTemplate(‘templateFolder/yourtemplate.phtml’)->toHtml(); ?>

2) For calling a core template file in CMS page

{{block type=”core/template” template=”templateFolder/your_template.phtml”}}

Leave a comment

Posted by on Mar 27, 2012 in Magento, PHP


Tags: , , ,

How to get random product of a category in magento

1) To Show the list of all product of a category in random order just copy & paste the following code anywhere in your magento template files:

$catid = '12';
$category = new Mage_Catalog_Model_Category();
$category->load($catid); // this is your special offers category id!
$collection = $category->getProductCollection();
$numProducts = 5;
$collection->setPage(1, $numProducts)->load();
foreach($collection as $product) {
$product = Mage::getModel('catalog/product')->load($product->getId()); /* Load Products by ID*/
echo $product->getName();
echo $product->getShortDescription();

Posted by on Feb 27, 2012 in Magento


Tags: , , , ,

How to add “View All” button in Magento’s pagination

Hi, Here is the solution of How to add view all option in magento pagination…

First of all, we need to trace the “app/design/frontend/your_package/theme/template/page/html/pager.phtml” file in our working template directory. When you do, all that needs to be added is the following code somewhere to fit your needs:

<a href="<?php echo $this->getLimitUrl('all')?>" title="< ?php echo $this->__('View All Products') ?>">
< ?php echo $this->__('View All') ?>

Now we’ve added our link to pagination. And only thing important here is the following segment of code:


This small segment of code generates our link with “limit” parameter set to “all”. Now when we click on it, it will give us a full category listing. The problem with that is that our value of “limit” set to “all” becomes stored permanently (for as long the session lasts). So we need to write just a couple lines of code more to fix it. Now  in “app/core/code/Mage/Catalog/Block/Product/List/Toolbar.php”  file, find the _construct() method. By default last line of that method is:


which sets our (edited) template to this model. Just after that line, add the following code:

$defaultLimit = $this->getDefaultPerPageValue();

So, this is it. I hope it will be helpful for someone.


Posted by on Jan 9, 2012 in Magento


Tags: , , ,

How to add reviews section in view.phtml in magento

In the catalog.xml layout:

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

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

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


Leave a comment

Posted by on Dec 29, 2011 in Magento


Tags: , ,

Restrict Product Reviews to Registered Users

In the custom template:

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



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):

$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 )
$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>

<?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): ?>
<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>
< ?php endforeach; ?>

Change it by the following :

< ?php foreach ($this->getGalleryImages() as $_image): ?>
<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>
< ?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: , ,

%d bloggers like this: