How To Use Cache In Magento

In this section we will see how to use cache in our custom modules. Suppose you have a custom module and wanted to save some data in magento cache, lets see how to do it.

Saving Data To Cache

$data = array(1,2,3,4); //any data which you want to save to cache $id = 'my_mod_id'; //unique id for your cache data $tags = array('collection'); //cache tags will be explain later in detail $lifetime = false; //false means infinity, or you can specify number of seconds $priority = 8; // number between 0-9, used by few backend cache models Mage::app()->saveCache($data,$id,$tags,$lifetime,$priority); Retrieving Data Mage::app()->loadCache($id);

Deleting Cache Mage::app()->removeCache($id);

Checking If Cache Is Enabled Mage::app()->useCache('collection'); //check if cache group is enabled or not

above are some basic caching function we can use in magento.


Custom Sending emails in magento

you can put this script in controller or any event of user


$email_to = '';

$receiveName = 'xyz';

$vars = data of user

$template_id = "New email"; //Name of email template
$email_template = Mage::getModel('core/email_template')->loadByCode($template_id);  //load templa

$email_template->send($email_to,$receiveName, $vars);



Magento – Install and Upgrade Database script

I struggled a lot to find a method to install a simple database script in Magento for creating a table or upgrading field or column. In the end I thought why not write one for reference. This post is mainly for my reference 😉

Initially you need to create a small extension to install the db script. You can also use the existing one if you want. But i have written a small extension just for the purpose of adding and updating database tables.

Below is the step by step procedure for executing Database queries

1) I have created a small extension named database. Create a file in “app/etc/modules” named “Greeting_Database.xml”.
The content of the file will be.

<?xml version="1.0"?>

This is just a basic file to let Magento know you have a module named Database. Greeting is the Company name of the module.
Continue reading

Display special priced products on your Magento homepage

Step 1: Open your homepage CMS page and click on the content tab. Paste the following:

{{block type="catalog/product" name="" alias="product" template="catalog/product/special.phtml"}}


Step 2: First of all this is the code were that we’re going to be using to do this.

Mage::getSingleton('core/session', array('name' => 'frontend'));
$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')


$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

$_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
<div class="specials_block">
<div class="page-title"><h2><?php echo $this->__('Specials') ?></h2></div>
<?php  $_collectionSize = $_productCollection->count();  ?>

<ul class="products-grid-new row">

<?php  $i=0; foreach($_productCollection as $_product): ?>
<?php $i++; if($i!=5){?>
<li class="item span3<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><span></span><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image non-mobile-img"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(270, 217) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
<div class="block_holder">

<span class="price">
<?php echo $this->getPriceHtml($_product, true, '-new') ?></span>

<div class="descr"><?php echo $_product->getShortDescription() ?></div>
<div class="actions">
<a href="<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?> " class="icon-shopping-cart"></a>
<a href="<?php echo $_product->getProductUrl() ?>" class="icon-info-sign"></a>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<a href="<?php  echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="icon-star-empty"></a>
<?php endif; ?>
<?php $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product) ?>
<a href="<?php echo $_compareUrl ?>" class="icon-retweet"></a>

<?php } else {break;}?>
<?php endforeach ?>

You can then paste this into a file and name it specials.phtml and save it into your frontend template folder catalog/product.

Display magento products by category ID

$categoryIds = array(18);//category id

$collection = Mage::getModel('catalog/product')
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
$_collectionSize = $collection->count()

Get detail of products

<?php $i=0; foreach ($collection as $_product):  ?>
<?php $i++;?>
<?php if($i!=4){ // for maximum 3 product ?>
<div class="product-image">

<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(); ?>" />


<div class="product-name">

<a href="<?php echo $_product->getProductUrl();?>"><?php echo $_product->getName(); ?></a>


<div class="product-price">

<?php echo Mage::helper('core')->currency($_product->getPrice(),true,false);?>


<div class="product-Short Description">

<?php echo    Mage::helper('core/string')->truncate($product->getShortDescription(), $length = 50, $etc = '...', $remainder = '', $breakWords = true); // word limit for short description ?>


<?php } else{break;}?>
<?php endforeach ?>


how to display article intro image in joomla module?

Article display by yourtheme/html/com_content/article/tmpl/default.php file.

you can use module mod_articles_latest so edit mod_articles_latest/tmpl/default.php file.

* @package Joomla.Site
* @subpackage mod_articles_latest
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt

defined('_JEXEC') or die;
<ul class="latestnews<?php echo $moduleclass_sfx; ?>">
<?php foreach ($list as $item) : ?>
<a href="<?php echo $item->link; ?>">
<?php echo $item->title; ?></a>
<img src="<?php echo json_decode($item->images)->image_intro; ?>"/> //new line
<?php endforeach; ?>

Drag and drop using jquery


Create .html file

<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
 // Example 1.1: A single sortable list
 $('#example-1-1 .sortable-list').sortable();
 // Example 1.2: Sortable and connectable lists
 $('#example-1-2 .sortable-list').sortable({
 connectWith: '#example-1-2 .sortable-list'
 // Example 1.3: Sortable and connectable lists with visual helper
 $('#example-1-3 .sortable-list').sortable({
 connectWith: '#example-1-3 .sortable-list',
 placeholder: 'placeholder',
 // Example 1.4: Sortable and connectable lists (within containment)
 $('#example-1-4 .sortable-list').sortable({
 connectWith: '#example-1-4 .sortable-list',
 containment: '#containment'
 Continue reading