Some useful Magento template code snippets

Ahmed July 17, 2010 11

Over the last 2 month, I’ve built a few Magento sites during which I’ve had certain requests to add some basic extras to the site template – basic it may sound however if you have even a little experience with Magento, you’ll be aware of its complexity.

Product Reviews and Review form in a tab

1. Add the following:

1
2
3
4
5
6
7
8
9
10
<catalog_product_view>
<reference name="product.info.tabs">
<block type="review/form" name="product.review.form" as="review_form"/>
<action method="addTab" translate="title" module="review">
<alias>avis</alias>
<title>Avis / commentaires</title>
<block>review/product_view_list</block>
<template>review/product/view/list.phtml</template>
</action>
</reference>

2. Add

1
2
3
4
5
<?php
layout = Mage::getSingleton('core/layout');
$block = $layout->getBlock('content')->getChild('product.info')->getChild('info_tabs')->getChild('review_form');
echo $block->toHtml();
?>

to app/design/frontend/[your_theme]/template/review/product/list.phtml

Note: For the above to work, your theme should already be using product tabs (Many tutorials are available online if you don’t have tabs)

Source: http://www.magentocommerce.com/boards/viewthread/31712/

Show Sub-Categories (as images with text) on Category or product Pages (and sort by name)

If a category contains both products and further sub-categories, then this code can be used so such a category shows both products and sub-categories.

Add the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
function sortCategories($a, $b) {
return strcmp($a['name'], $b['name']);
}
?>
<?php
$obj = new Mage_Catalog_Block_Navigation();
$cat = Mage::getModel('catalog/category')->load($obj->getCurrentCategory()->getId()); //get current cat
$subcats = $cat->getChildren(); // Get sub cats
// loop on it
if ($subcats) { ?>
<h2 class="catptitle">Sub-categories</h2>
<?php }
$categoryArray = array();
foreach(explode(',',$subcats) as $subCatid){ // split up the mage data for use
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()){
$caturl  = $_category->getURL(); // get link to image
$catname = $_category->getName(); // get the name
$catdesc = $_category->getDescription(); // get the name
if($_category->getImageUrl()){
$catimg = $_category->getImageUrl(); // hey, we got image
}else{
$catimg=null;
continue;
} // this is the basic testing data, format as desired & good luck!
$catdescTwo = substr(strip_tags($catdesc), 0, 70);
$categoryArray[] = array(
'name' => $catname,
'url' => $caturl,
'desc' => $catdescTwo,
'img' => $catimg
);
}
}
usort($categoryArray, "sortCategories");
foreach ($categoryArray as $categoryItem) {
echo '<a href="'.$categoryItem['url'].'">'.$categoryItem['name'].'</a>
<a href="'.$categoryItem['url'].'"><img alt="" src="'.$categoryItem['img'].'" /></a>
'.$categoryItem['desc'].'...';
}
?>

to app/design/frontend/[your_theme]/template/catalog/category/list.phtml

OR app/design/frontend/[your_theme]/template/catalog/product/list.phtml

Display RSS Feed items

Step 1

Create a file called latest_news.phtml in app/design/frontend/default/[your_theme]/template/callouts/latest_news.phtml with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php $channel = new Zend_Feed_Rss('INSERT FEED URL'); ?>
<div class="block block-latest-news">
<div class="sideBestS">
<h2><?php echo $this->__('Latest News') ?></h2>
</div>
<div class="block-content">
<ol id="graybox-latest-news">
<?php $i = 0; ?>
<?php foreach ($channel as $item): ?>
<?php if ($i != 3) { ?>
<li><a href="<?php echo $item->link; ?>"><?php echo $item->title; ?></a>
<p><?php echo substr($item->description,0,179) . "..."; ?></p>
</li>
<?php $i++; ?>
<?php } ?>
<?php endforeach; ?>
</ol>
</div>
</div>

Step 2

Add the following:

1
2
3
4
5
<reference name="right">
<block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
<block type="core/template" name="right.permanent.callout" template="callouts/right_col.phtml"/>
<block type="core/template" name="right.latest.news" template="callouts/latest_news.phtml"/>
</reference>

to app/design/frontend/[your_theme]/layout/page.xml

Note: The above will add the code to the right sidebar – you can reference it elsewhere on the site

Display a product’s “quantity in stock” on your Product Pages

1
2
<?php $stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($this->getProduct())->getQty(); ?>
<?php echo $stock_count ?>

Flush/force DNS (Windows only)

Many a time, when you change the DNS records and nameservers of a domain to point to the new site, it can take up to 72 hours to propagate over the internet. Unfortunately sometimes, the client/customer/general users may see the new site before the developer and if there are any issues then the developer won’t be able to fix them until they can see the new site. Previously i used to wait like anybody else however recently a colleague showed me a method by which I can force my browser to  view the new site!

Step 1:

Open command promt (search/run “CMD” in windows)

In Command promt, type the following:

“ipconfig /flushdns”

image

Step 2

Go to the folder:

%SystemRoot%\system32\drivers\etc\

For windows 7, %SystemRoot% is C:\Windows\

For windows XP, it may be a little different.

Inside the folder “etc” is a file named – “hosts”. Open this with Notepad and add the line

xx.xxx.xxx.xx www.YOUR-DOMAIN.co.uk

At the bottom of the file (where xx.xxx.xxx.xx is the ip address of the new host)

Then you will have to clear your browser cache.

Ecommerce tracking in Magento Using Google Analytics

This one is simple.

Go to System > Configuration > Google API and insert you analytics ID.

Then

  • login to Google analytics
  • Select the account for your site
  • Click edit
  • on “Main Website Profile Information”, click edit
  • tick the radio button “Yes, an E-Commerce site”

Site search tracking in Magento Using Google Analytics

  • login to Google analytics
  • Select the account for your site
  • Click edit
  • on “Main Website Profile Information”, click edit
  • tick the radio button “Do Track Site Search”
  • insert the letter “q” in the input box

Display PHTML files in Dreamweaver

http://www.dreamweaverclub.com/dreamweaver-phtml-inc.php

Conclusion

Is there anything else you would like to do with Magento? leave a note below and I’ll see what i can do!

11 Comments »

  1. Andrew McKean August 23, 2010 at 14:02 - Reply

    Magento 1.4.1.1
    Ubuntu 10.0.4
    PHP5
    Hosted on my own web server.

    Hi,
    How can I display on the CMS Home page a brief text message that says “Your Browser is xyz Version 1.234″ ?
    I have tried installing Browser.php with the following code in the Content area of the CMS Home page…

    getBrowser());
    $ver = $browser->getVersion();
    echo “$thebrowser $ver”;
    ?>

    However this did not work.

    I then tried it on a simple web page at var/www/index.html with a .htaccess file that allowed embedded PHP and it worked fine.

    I am not sure where to put the file Browser.php in Magento and how to call it in the CMS home page ?

    • Ahmed August 28, 2010 at 16:14 - Reply

      Hi Andrew, It seems like you have already achieved this on your site?

  2. Harita April 7, 2011 at 14:59 - Reply

    hello,

    I want to display product reviews on home page for new products. Using Magento ver. 1.4.2.0.
    I will be thankful if you can suggest me some solution.

    Thanks in advance.

  3. Maude Georgis May 17, 2011 at 04:02 - Reply

    Your blog for some reason is not showing most of your images in Opera.

    • Ahmed August 17, 2011 at 12:46 - Reply

      Thank you, I’ll check this out

  4. Moiz Ahmed August 9, 2011 at 00:28 - Reply

    I wanted to display review form. However you did not mention where to add the code in the 1st step.
    Please let me know
    I am on magento 1.4.1
    Thank you.

  5. mahesh November 5, 2011 at 06:27 - Reply

    Hi

    How can i list the products with sort by image order

    Ex: which product have the image that should come first and which product don’t have the image that should come last

  6. Free Magento Extensions November 6, 2011 at 01:16 - Reply

    These are great for Magento. Thank you!

  7. Nikie Deoampo November 17, 2011 at 06:54 - Reply

    do you have any solution or advise coz i need to create “Recent Search” in my home but still dont know how.. it will just show the recent searches that the user did.

    pls help. thank you!

  8. Mangalore webdesign December 5, 2011 at 14:36 - Reply

    Wow awesome, sub-categories code works fine,,,thank you for your nice blog.

Leave A Response »