Author Archives: admin

Session and cookie in Magento

Magento provides very easy way to handle Cookie and session.
Magento session:
Magento session is handle by core Module ‘core/session’. Here is the way to save Session in magento

$session = Mage::getSingleton('core/session');
$session->setData('my_magento_session', array('id' => "my_session_id_value", 'setAt' => time()));

This will store a array of session “my_magento_session”.
we can get store session like this

$mysession = Mage::getSingleton('core/session')->getData('my_magento_session');
$sessionId = $mysession["id"];

and session can be removed with setting session variable null

Mage::getSingleton('core/session')->setData('my_magento_session',null);

Cookie Handling in Magento:
Setting cookie

Mage::app()->getCookie()->set('cookie_name', "cookie_value", $lifetime,$domain,$httponly);

The parameter are same as normal PHP setcookie

Getting cookie in magento:

Mage::app()->getCookie()->get('cookie_name');

Display Best selling product in magento store

Display Best selling product in magento store

</pre>
<?php $_productCollection=$this--->getLoadedProductCollection() ?>

<?php if(!$_productCollection--->count()): ?>

<?php else: ?--></pre>

<div><?php $_collectionSize = $_productCollection--->count() ?>

&nbsp;

<script type="text/javascript">// <![CDATA[

mce:0

// ]]></script>

<table border="0" cellspacing="0">

<tbody><?php $i=0; foreach ($_productCollection as $_product): ?--> <?php if($i++%2==0): ?-->

<tr><?php endif; ?-->

<td><a href="<?php echo $_product->getProductUrl() ?>"> <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(95, 95); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="95" height="95" /> </a>

<div>

&nbsp;

<?php echo $this--->getPriceHtml($_product, true) ?>

<?php if($_product--->getRatingSummary()): ?>

<?php echo $this--->getReviewsSummaryHtml($_product, 'short') ?>

<?php endif; ?-->

<?php echo nl2br($_product--->getShortDescription()) ?></div></td>

<?php if($i%2==0 || $i==$_collectionSize): ?--></tr>

<?php endif; ?--> <?php endforeach ?--></tbody>

</table>

</div>

<pre>

<?php endif; //$_productCollection--->count() ?>
<pre>

Magento Template Structure (contd.)

Block its content for output to the browser. Let’s take the right column; our content blocks set for this column on a standard theme could be:

• mini cart

• recently viewed products

• newsletter subscription block

• poll

On receiving a request from a user connecting to our site to view the page:

1. Magento will load the structural areas

2. Each structural area will be processed through

3. Magento will gather the content blocks assigned to each structural area

4. It will then progress through the content block template for each structural area, to process the output

5. It sends all of this back as final output to the user, who then views the Magento page that was requested