May
17

Amazon S3 Cloudfront CDN also allow GZIP Compression

After integrating Amazon Cloudfront CDN with your current hosting, the next step you need to do probably to make use the benefit of compression as much as possible. Most of the modern browser is now supporting gzip-compression as standard feature. Amazon S3 Cloudfront CDN generally does not support on-the-fly gzip facility, but implementing gzip with Cloudfront could be easier than you think.

Store Compressed and uncompressed both files on the AMAZON s3 Cloud

  1. As we know Amazon doesnot support on-the-fly gzip-ing. What We can do is the gzip compression manually and upload it to cloudfront. Using 7zip(free) or many other compression utility, we can create the compressed version of any static file(css/js/etc..). So now we have to put 2 versions of static files on CDN  the normal version (in case a browser ll not support compression) and the compressed(gzip) version.
  2. For example: i have a CSS file from my website: stylesheet.css. Now i ll have have 2 copies : stylesheet.css (normal version) and style.css.gz (compressed version produced using gzip with any gzip software).
  3. Now the point is that we have to upload this gzip compressed file onj Amazon Cloudfront CDN with a renaming.For example,rename stylesheet.css.gz above to stylesheet.gz.css.The reason is some applications only check for the last extension instead of checking “Content-Type” on the header. So, in this case the program will still know that the file is actually a css file to describe the stylesheet.
    Note: “Content-Type” header is usually generated automatically by AmazonCloudfront based on the extension.
  4. Now finally we have both versions (stylesheet.css and stylesheet.gz.css) uploaded on on Amazon S3 Cloudfront.
    One extra Header only for compressed version (e.g:stylesheet.gz.css) with
    “Content-Encoding” and the value is “gzip”Now Confirm the content of the header:
    - Normal file(stylesheet.css):  “Content-Type: text/css”
    - Zipped file (stylesheet.gz.css) :  “Content-Type: text/css” and “Content-Encoding: gzip”.
    Note: Dont  forget to set the appropriate access privilege (ACL setting)

Little change in Calling Static files from Amazon CDN Cloudfront:

Modify Application index file for calling either zipped or normal version. The Logic is if the browser supports gzip, then we will point to compressed version. If it doesn’t support compression, ll call the normal version. A few additional code below to have comperssed or normal version depends on whether browser supports compressed version or not

1
<?php $middle_fix_gzstr = (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)) ? ”.gz” :";
2
 
3
//Calling for CSS changed a little depends on the condition
4
 ?>
5
<link rel=”stylesheet” type=text/css” href=”http://[AMAZZON CDN URL]/stylesheet<?php echo $middle_fix_gzstr;?>.css” media=”all” />

 So, by using this modification, we can do overcome the limitation of gzip on-the-fly compression from Amazon CDN to deliver even better performance for website.

Let’s Spread the Technique !

Sep
25

Magento display subcategories of active categories

Display Subcategories of Active Category

 PHP |  copy code |? 
01
&lt;div class="block block-category tpVerticalNavigation1">
02
<?php $_helper = Mage::helper('catalog/category') ?>
03
<?php $_categories = $_helper->getStoreCategories() ?>
04
<?php $currentCategory = Mage::registry('current_category') ?>
05
<?php if (count($_categories) > 0): ?>
06
    <ul id="tpVerticalNavigation1">
07
        <?php foreach($_categories as $_category): ?>
08
            <li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a>
09
                <?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
10
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
11
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
12
                    <?php if (count($_subcategories) > 0): ?>
13
                        <ul><?php foreach($_subcategories as $_subcategory): ?>
14
                                <li><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></li>
15
                            <?php endforeach; ?>
16
                        </ul>
17
                    <?php endif; ?>
18
                <?php endif; ?>
19
            </li>
20
        <?php endforeach; ?>
21
    </ul>
22
<?php endif; ?>

 

Sep
25

Magento Vertical Categories Navigation revised versions

Display Top Level Categories

01
<div class="block-title block-category-title"><h2><?php echo $this->__('Categories') ?></h2></div>
02
<?php $_helper = Mage::helper('catalog/category') ?>
03
<?php $_topCategories = $_helper->getStoreCategories() ?>
04
<?php if (count($_topCategories) > 0): ?>
05
    <ul>
06
        <?php foreach($_topCategories as $_category): ?>
07
            <li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a></li>
08
        <?php endforeach; ?>
09
    </ul>
10
<?php endif; ?>

Display Top Categories and Sub-Categories in Hierarchical Manner.  Vertical Navigation in Magento With CSS Style

Vertical Navigation CSS

 PHP |  copy code |? 
01
02
<style type="text/css">
03
 .tpVerticalNavigation1 ul{ margin: 0;padding: 0;list-style-type: none;font: bold 13px Verdana;width: 180px; /* Main Menu Item widths */border-bottom: 1px solid #ccc;}
04
  .tpVerticalNavigation1 ul li{position: relative;}
05
 
06
 /* Top level menu links style */
07
 .tpVerticalNavigation1 ul li a{ display: block; overflow: auto; /*force hasLayout in IE7 */color: white;text-decoration: none;padding: 6px;border-bottom: 1px solid #778;border-right: 1px solid #778;}
08
 .tpVerticalNavigation1 ul li a:link, .tpVerticalNavigation1 ul li a:visited, .tpVerticalNavigation1 ul li a:active{ background-color: #012D58; /*background of tabs (default state)*/}
09
 .tpVerticalNavigation1 ul li a:visited{color: white;}
10
 .tpVerticalNavigation1 ul li a:hover{background-color: black;}
11
 
12
 /*Sub level menu items */
13
 .tpVerticalNavigation1 ul li ul{ position: absolute; width: 170px; /*Sub Menu Items width */ top: 0; visibility: hidden;}
14
 .tpVerticalNavigation1 a.subfolderstyle{ background: url(../arrow.gif) no-repeat 97% 50%;}
15
</style>

Little JS Code to show/hide subcategories tab

01
02
<script type="text/javascript">
03
 var menuids=["tpVerticalNavigation1"] //Enter id(s) of each Side Bar Menu's main UL, separated by commas
04
 function initVerticalMenu(){
05
 for (var i=0; i<menuids.length; i++){
06
 var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
07
 for (var t=0; t<ultags.length; t++){
08
 ultags[t].parentNode.getElementsByTagName("a")[0].className+=" subfolderstyle"
09
 if (ultags[t].parentNode.parentNode.id==menuids[i]) //if this is a first level submenu
10
 ultags[t].style.left=ultags[t].parentNode.offsetWidth+"px" //dynamically position first level submenus to be width of main menu item
11
 else //else if this is a sub level submenu (ul)
12
 ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
13
 ultags[t].parentNode.onmouseover=function(){
14
 this.getElementsByTagName("ul")[0].style.display="block"
15
 }
16
 ultags[t].parentNode.onmouseout=function(){
17
 this.getElementsByTagName("ul")[0].style.display="none"
18
 }
19
 }
20
 for (var t=ultags.length-1; t>-1; t--){ //loop through all sub menus again, and use "display:none" to hide menus (to prevent possible page scrollbars
21
 ultags[t].style.visibility="visible"
22
 ultags[t].style.display="none"
23
 }
24
 }
25
 }
26
 if (window.addEventListener)
27
 window.addEventListener("load", initVerticalMenu, false)
28
 else if (window.attachEvent)
29
 window.attachEvent("onload", initVerticalMenu)
30
</script>
31
 
32

the PHP Code

01
02
<?php $_helper = Mage::helper('catalog/category') ?>
03
<?php $_topCategories = $_helper->getStoreCategories() ?>
04
<?php $currentCategory = Mage::registry('current_category') ?>
05
<div class="block block-category tpVerticalNavigation1">
06
<?php if (count($_topCategories) > 0): ?>
07
    <ul id="tpVerticalNavigation1">
08
        <?php foreach($_topCategories as $_category): ?>
09
            <li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a>
10
 <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
11
 <?php $_subcategories = $_category->getChildrenCategories() ?>
12
 <?php if (count($_subcategories) > 0): ?>
13
 <ul><?php foreach($_subcategories as $_subcategory): ?>
14
 <li><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></li>
15
 <?php endforeach; ?>
16
 </ul>
17
 <?php endif; ?>
18
            </li>
19
        <?php endforeach; ?>
20
    </ul>
21
<?php endif; ?>
22
</div>
23

Sep
24

free Magento installation

Magento Installation is one of the simpller task, I started to it FREE  ! Whats the aim? The aim is to increase the magento experience and technology update. Also you can write review of the work I do for you. I can put on my blog for others to see after your permission.  Simple enough !

Finally, I’ve decided to start offering Magento Installation as a service, there is high demand for a well installed Magento. As most of the people are using online selling and having a shop-cart online. But due to lack of knowledge an skills, they have to hire some professional to create their cart. Because of popularity of such services, i decided to start Free magento installation service. I had done more than 7 projects in magento and successfully running today. I came to know a lot of about magento in this period and i ‘ld like to share my experiece with this blog. So, I want to take a lot of experience of magento. So i am providing free magento installation as serive.

If you want to install magento on-site. let me know .. just leave a message here and we ll contact you soon. I started spreading my techniques among the people on the Magento forums who for a small fee could have saved themselves hours of headaches and had the work done quickly by a professional.

I cheked many magento sites  offering $149 to install magento – but as i checked there is nothing more complicated in installation in magento. and i ld like to increase my experience in magento, so i decided to work on this free at start. I will often be able to install same day, and usually install within 2 days.

Of course the price is 0.00 right now for a limited time I’ll install your Magento for FREE, so if you are contemplating it, get in touch with me.

Because I haven’t offered any professional service on this blog before I thought I’d offer to do Magento Installations free of charge to get some references from happy customers! So if you want your Magento installed for you by an expert , let s conenct and make a new relation !
 

May be you can be also interested in:
Magento theme installation, Magento extension installation, Magento theme integretion aer also offered as free of cost.

Sep
21

Some useful function retrieves values related to magento website and currency configuration

Website related functions

When we want to check Type of customer. Following function helps us to ge that

<?php      echo $customerEntityType = Mage::getModel(‘eav/entity’)->setType(‘customer’)->getTypeId(); ?>

Current Website id in magento can be retrieved with the following function. In case of multiple websites in magento we can get the current Website id with the below code.

1
<!--DVFMTSC--><?php  $currentWebSite = Mage::getModel('core/website')->load( 'base', 'code');
2
            echo $currentWebSiteID = $currentWebSite->getId();
3
?>

 

Getting Website Name from the configuration. Anywhere when you want to retrieve any configure related value, you can call Store configuration function as follows, we used here to get the website name.

1
<?php echo Mage::getStoreConfig('system/website/name'); ?>

 

Getting Store Name from the configuration is as easy as getting website name from the configuration. Its again a configuration parameter we can get with the help of getStoreConfig function. So to get the Store name from the configuration Call :

1
<?php echo Mage::getStoreConfig('system/store/name'); //Returns store name ?>

 

Currency related Functions

Currency related function are dependent to store. So with the help of getStore() function we have the store object than we can call the relative currency related function to get the details about the store currency.

 i.e. to Get the Curency code of the Store ew an call the function getCurrentCurrencyCode.

1
<?php echo $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();?>

 

1
<?php echo $currency_code = Mage::app()->getStore()->getBaseCurrency(); ?>

 

1
<?php echo $currency_code = Mage::app()->getStore()->getDefaultCurrency(); ?>

 Formatting a Currencyin magento can be done via following function named currency

1
<?php
2
                $_coreHelper = $this->helper('core');
3
                echo $_coreHelper->currency('150');
4
?>

 

the Above code will output $150, if default currency is Dollar.

Sep
21

Magento Application some useful functions we used in development

Sometimes we need to check the current host name in magento. Here is the function that returns the hostname of server where the application is executing.

The following code is same as normal PHP $_SERVER[‘HTTP_HOST’]

1
<?php      echo Mage::app()->getFrontController()->getRequest()->getHttpHost();?>

 

 It returns 127.0.0.1 if we runs on 127.0.0.1 or in other cases it returns the domain name of the site.

 When we want to retrieve the Path of Base Directory in Magento. We can use the following function

 PHP |  copy code |? 
1
 <?php echo Mage::getBaseDir();  //gives  physical path of the sites root directory ?>

 

(In my case it return e:\www\magento )

Its returns the Physical path of the Application directory where magento application installed.

When we want to retrieve the relative path of the sites default skin folder. IT return the virtual path of the default skin folder. Many time we used in theme development

 PHP |  copy code |? 
1
 <?php echo $this->getSkinURL(); ?>

 

 It returns the http://127.0.0.1/www/magento/skin/frontend/default/default/

 

 Virtual path of the magneto application can be retrieved with the following function. Relative path of the Magento site

1
<?php echo Mage::getBaseURL();   ?>

 

Or

1
<?php echo Mage::getURL();?>

 

Sep
14

magento category navigation or vertical category menu in sidebar

 Creating category navigation placed in left/ right hand in magento is so simple. In magento all of the template elements configured via layout settings.

MAgento Categories navigation in left hand side

MAgento Categories navigation in left hand side

Building a theme is a separate big topic in magento and we will try to cover this in our other article. We are defining here the subject of the article as how can we show category navigation  in left / right bar.

Everything related to magento theme is getting configured through the  theme layout and the theme template. Each Theme contain its configuration stored in related layout XML file. And the display/ lookup of configured block is stored in template view, stored in template file having extension as .phtml. (More information related to theme development can be found at http://www.magentocommerce.com/4_-_themes_and_template_customization) Hence each theme contain layout and theme folder. Layout Folder contains number of xmls files where each file stores xml configuration for a block and theme folder contain .phtml files  where each file have a block of php-code. So first of all we are creating a block of php  code that retrieves the top level category can show it in list format

 

 

 

 

Step1. PHP Code to retrieve top level categories for navigation and save it at the following location.

in short this file contain code related to catalog Navigation Model which retrieves top level categories and show it in proper manner

Magento\app\design\frontend\base\default\template\catalog\navigation\categorymenu.phtml

01
<?php
02
/**
03
 * Left Category menu for store
04
 * We are showing  jsut the top level categorioes
05
 * @see Mage_Catalog_Block_Navigation
06
 */
07
?>
08
<div class="block block-category">
09
 <div class="inside-box">
10
 <div class="block-title block-category-title"><h2><?php echo $this->__('Categories') ?></h2></div>
11
 <div class="block-category-navigation">
12
 <ul id="category-nav">
13
 <?php foreach ($this->getStoreCategories() as $_category): ?>
14
 <?php if($_category->name!=""):  ?>
15
 <li><?php echo $this->drawItem($_category) ?></li>
16
 <?php endif?>
17
 <?php endforeach ?>
18
 </ul>
19
 </div>
20
 </div>
21
</div>

Step2. Change the page.xml to include category navigation menu.

So let’s start with the home page, as in most of the cases. Magento home page is simple CMS page, therefore, we have to locate the page related layout XML file . that exists in our layout folder of selected theme. We are taking example as the default theme. So have to open the following file:

Mangeto Home page layout File page.xml

Mangeto Home page layout File page.xml

app\design\frontend\base\default\layout\page.xml. Line no. 83  looks like :

1
<block type="core/text_list" name="left" as="left" translate="label">
2
         <label>Left Column</label>
3
</block>

This section tells us the left hand side section inherit from its parent Catalog  So we have to modify the catalog.xml file located in the same folder  hence open

app\design\frontend\base\default\layout\catalog.xml. it looks like :

the original

1
<reference name="left">
2
          <block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
3
                <action method="setImgSrc"><src>images/media/col_left_callout.jpg</src></action>
4
                <action method="setImgAlt" translate="alt" module="catalog"><alt>Our customer service is available 24/7. Call us at (555) 555&minus;0123.</alt></action>
5
                <action method="setLinkUrl"><url>checkout/cart</url></action>
6
            </block>
7
</reference>

we have to update it as follows. hence the modified version is

1
<reference name="left">
2
 <block type="catalog/navigation" name="catalog.categorymenu" after="top.search" template="catalog/navigation/categorymenu.phtml"/>
3
            <block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
4
                <action method="setImgSrc"><src>images/media/col_left_callout.jpg</src></action>
5
                <action method="setImgAlt" translate="alt" module="catalog"><alt>Our customer service is available 24/7. Call us at (555) 555-0123.</alt></action>
6
                <action method="setLinkUrl"><url>checkout/cart</url></action>
7
            </block>
8
        </reference>

Now we have to change the CSS as we needed in our CSS. as i had not included that part in the current article. that can be made via small changes in Css file.

Sep
09

Insert Static Block content in template

As magento describes that when we want to add any static block in our magento template, we must add some lines of codes in the XML file. Then we are able to attach related static block in our template. Sometimes we are in hurry and don’t want to update the XML file to insert code in our template. So we can use the following code to insert any static block directly in our template. We just want to make a call to this function :

1
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('required_cms_block_identifier')->toHtml() ?>

So, By using this code in the template will automatically fetch the content from the static block and insert into our template               

Sep
08

Best Seller Products in Magento

First of All before writting about the best seller products in magento. Here is the file you can use to retrieve best seller products in magento. anywhere in your magento site. you jut need to place this file at correct location and use as your requirements. Either on home page or in any catalog page or at any other location as per your need. So here is the file contain code of showing best-seller products in magento.

Bestseller in magento

One of the most popular option, that each customer needs is the best selling/ best seller products. Each second e-commerce site that uses magento, needs a location where they can show products which are mostly purchased by customer and in the term e-commerce, we call that products as Best Seller Products. Now the question arise that how to show best seller products in magento. I also needed to implement this feature and as generally we do, I also searched best–seller module for magento, as I was also unkown from the little trick of showing products collection in magento.

And I found some technical write-ups related to magento. Like the following Model Named Mage Reports model  (Mage_Reports_Model_Mysql4_Product_Collection)

http://docs.magentocommerce.com/Mage_Reports/Mage_Reports_Model_Mysql4_Product_Collection.html#sec-methods

we have to just call

1
Mage::getResourceModel('reports/product_collection');  //in our php file

returns the Products Collection for reporting purpose. We can add a few functions and get some useful data that we need for many customer’s e-commerce site.

Like when we want to get Best Seller Products in magento we just need to add one more filter to this collection.

$products = Mage::getResourceModel(‘reports/product_collection’)->addOrderedQty(); // retrieve Ordered qty of Products with Products collection.

Now a few more filter like

1
addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description'))
2
 
3
// or
4
 
5
addAttributeToSelect('*')

Above filter used to select required attributes/fields, we need with product collection. We can select the required attributes/ fields with this function by passing fields in array or we can also pass * when we need to take all fields

1
setStoreId($storeId)
2
addStoreFilter($storeId) // add this to retrieve products collection related to specific store
3
 
4
setOrder('ordered_qty', 'desc');

Mostly Ordered products on top. Hence we with the help of all of above functions related to this model we can get the Best Seller Products in magento through the following PHP Code

1
$products = Mage::getResourceModel('reports/product_collection')
2
->addOrderedQty()
3
//->addAttributeToSelect('*')
4
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description')) //edit to suit tastes
5
->setStoreId($storeId)
6
->addStoreFilter($storeId)
7
->setOrder('ordered_qty', 'desc'); //best sellers on top

So with the help of above collection we can create best seller module for magento. So, finally i think this code helps us to create magento best seller products extension.

Sep
02

Change the domain of magento

The magento installation is easy step-by-step process. In general way, we first install a demo-copy of magento on local server/ development-machine. We process the whole customization on local-server. The main problem is to upload/configure the magento from development machine to live server. There are a lots of problems occurred when we move our local copy of magento to live server. We are not discussing all at once. I am trying to cover the most comman problem, while moving the magento from local server to live server.

What we do in general, is we just install magento on local server with localhost or an ip address 127.0.0.1 as Website Domain / BASE URL . Now when we go live with magento, we have to change the base url of the magento. So it seems a big problem, but the solution is as simple as we do step-by-step installation of magento on local server. To Go live we just upload the whole magento folder and upload the Database we configured on local server. Now to change the configuration of magento regarding the domain is simple editing of a few records of existing table.

We have to edit 2 records of the table called core_config_data.

moving magento site from localhost to live

moving magento site from localhost to live

We have to open this table in any of mysqleditor either via phpmyadmin. We have to open this table in browse mode. Find the records with following entries in path field :

  1. web/unsecure/base_url : (Contain the unsecure domain name of magento site. Change the value of this field to your magento live site domain. Like http://time2php.com/magentostore
  2. web/secure/base_url : Contain the secure domain name of magento site. Change this with the value of the magneto live domain’s secure URL like https://time2php.com/magentostore

We can also fire the following SQL statements to change the domain of magento configuration

update core_config_data set value=”http://time2php.com/magento” where path=’web/unsecure/host’;
update core_config_data set value=” https://time2php.com/magento ” where path=’web/secure/host’;

Editing the BASE URL of magento

Editing the BASE URL of magento to go on live domain

we have to clear the magento cache after doing these changes in database.

Enjoy playing with small codes !