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.

Advertisements

Custom Sending emails in magento

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

<?php

$email_to = 'xyz@gmail.com';

$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->setSenderName(Mage::getStoreConfig('trans_email/ident_sales/name'));
$email_template->setSenderEmail(Mage::getStoreConfig('trans_email/ident_support/email'));

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

 

?>

WordPress function and its use

<?php bloginfo(‘name’); ?> – Title of the Blog, or Blog Name

<?php wp_title(); ?> – Title of a Specific Page

<?php bloginfo(‘url’); ?> – Exact URL for the Site

<?php bloginfo(‘description’); ?> – Site’s Description

<?php bloginfo(‘template_url’); ?> – Location of Site’s Theme File

<?php bloginfo(‘stylesheet_url’); ?> – Link to the Style.css location

<?php bloginfo(‘rss2_url’); ?> – RSS Feed URL for the Site

<?php bloginfo(‘pingback_url’); ?> – Pingback URL for the site

<?php bloginfo(‘version’); ?> – WordPress Version Number

<?php get_header(); ?> – Displays Header.php file content

<?php get_footer(); ?> – Displays Footer.php file content

<?php get_sidebar(); ?> – Displays Sidebar.php file content

<?php comments_template(); ?> – Displays Comment.php file content

<?php the_content(); ?> – Displays the Content of the Post

<?php the_excerpt(); ?> – Displays the excerpt that is used in Posts

<?php the_title(); ?> – Title of the Specific Post

<?php the_permalink() ?> – Link of the Specific Post

<?php the_category(‘, ‘) ?> – Category of a Specific Post

<?php the_author(); ?> – Author of the Specific Post

<?php the_ID(); ?> – ID of a Specific Post

<?php edit_post_link(); ?> – Edit link for a Post

<?php next_post_link(‘ %link ‘) ?> – URL of the Next Page

<?php previous_post_link(‘%link’) ?> – URL of the Previous Page

<?php get_links_list(); ?> – Lists all links in Blogroll

<?php wp_list_pages(); ?> – Lists all Pages

<?php wp_get_archives() ?> – List Archive for the Site

<?php wp_list_cats(); ?> – Lists all Categories

<?php get_calendar(); ?> – Displays the Built in Calendar

<?php wp_register(); ?> – Displays Register Link

<?php wp_loginout(); ?> – Displays Login/Logout Link only to Registered Users

Get Product Image based on its Position – Magento

You might have seen website where the product images changes when you move mouse on it. In this tutorial i’ll show you how to do it with Magento.

You need to create a small module to achieve this. We will add this function in the helper file of the module so it can be called globally anywhere using helper function.

If you are not familiar with module creation i’ll list the steps below:

Step 1: Create a file in “app/etc/modules” named “Greeting_Imagealt.xml”. The content of the file will be.

<?xml version="1.0"?>
<config>
<modules>
<Greeting_Imagealt>
<active>true</active>
<codePool>local</codePool>
</Greeting_Imagealt>
</modules>
</config>

Continue reading

Passing values and parameters to template file using XML and ShortCode from Magento Admin

At times there is a need to pass some parameters to template file in Magento. Some of the conditions are as follow:

1) Category Id is needed to be passed to display all the products of that particular category
2) Static block id needs to be passed to call it in template file.

We can pass any parameters in the template file from Magento Admin using two methods

1) Using the shortcode: By using shortcode we can pass the parameter as follows

{{block type="greeting_products/productslider" category_id="11" product_count="10" title="Featured Products" template="greeting/products/featuredslider.phtml" }}

Here category_id and product_count are passed in the file featuredslider.phtml

{{block type="core/template" name="contactForm"  form_action="contacts/index/post"  template="contacts/form.phtml"}}

Here form_action URL is passed as a form action in contact form which will be passed to postAction method of IndexController.php file.
Continue reading

Display Most popular Posts In WordPress

Add code in your theme where ever you want the popular 5 posts to be displayed.

<ul>
    <?php $pc = new WP_Query('orderby=comment_count&posts_per_page=5'); 
		
      while ($pc->have_posts()) : $pc->the_post(); ?>
		<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
		<p>Posted by <strong><?php the_author() ?></strong> with <?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></p></li>
      <?php endwhile; ?>
</ul>

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="home.new" 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.

<?php
Mage::getSingleton('core/session', array('name' => 'frontend'));
$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);

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

</div>
</div>
</li>
<?php } else {break;}?>
<?php endforeach ?>
</ul>
</div>


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