Magento Useful snippets


Cart Counter:

<?php
$count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
$total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
if ($count == 0) {
   echo $this->__('(0 ITEMS)', $count);
}
if ($count == 1) {
   echo $this->__('(1 ITEM)', $count);
}
if ($count > 1) {
   echo $this->__('(%s ITMES)', $count);
}
?>

Create Block By XML:

<block type="core/template" template="page/html/feature-ads.phtml" before="-" />  

Create block by PHP: 

<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/html/feature-ads.phtml')->toHtml(); ?>  

Layout local.xml

<?xml version="1.0"?>
<layout>
    <default>
        <!--Root/Default Layouts-->
        <reference name="root">
            <!--Appending Block-->
            <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>
        </reference>
        <!--CSS and JS Files-->
        <reference name="head">
            <!--Add CSS and JS, skin Folder-->
            <action method="addItem"><type>skin_css</type><name>css/styles.css</name></action>
            <action method="addItem"><type>skin_js</type><name>js/functions.js</name></action>
            <!--Remove CSS and JS, skin Folder-->
            <action method="removeItem"><type>skin_js</type><name>scripts/functions.js</name></action>
            <action method="removeItem"><type>skin_css</type><name>css/styles.css</name></action>
            <!--Remove JS, js Folder-->
            <action method="removeItem"><type>js</type><name>scripts/functions.js</name></action>
        </reference>
        <!--Header-->
        <reference name="header">
            <!--Append CMS Block-->
            <block type="cms/block" name="header.cms.block" as="headerCmsBlock">
                <action method="setBlockId"><block_id>header_cms_block</block_id></action>
            </block>
        </reference>
        <!--Change Block's Template if some Configuration is Set-->
        <reference name="catalog.topnav">
            <action method="setTemplate" ifconfig="pronav/pronavconfig/pronav_status">
                <template>pronav/navigation_top.phtml</template>
            </action>
        </reference>
        <!--Magento's Default Sidebar Blocks-->
        <remove name="cart_sidebar"/>                   <!--Cart Sidebar-->
        <remove name="catalog.product.related"/>        <!--Related products sidebar-->
        <remove name="wishlist_sidebar"/>               <!--Wishlist Sidebar-->
        <remove name="catalog.compare.sidebar"/>        <!--Compare Items Sidebar-->
        <remove name="right.permanent.callout"/>        <!--Right Callout Sample Data-->
        <remove name="left.permanent.callout"/>         <!--Left Callout Sample Data-->
        <remove name="right.reports.product.viewed"/>   <!--Viewed Products-->
        <remove name="right.reports.product.compared"/> <!--Compared Products-->
        <remove name="catalog.leftnav"/>                <!--Layered Navigation-->
        <remove name="left.newsletter"/>                <!--Sidebar Newsletter-->
        <remove name="right.poll"/>                     <!--Poll-->
        <remove name="tags_popular"/>                   <!--Popular Tags-->
        <remove name="paypal.partner.right.logo"/>      <!--Paypal logo Sample Data-->
        <remove name="catalogsearch.leftnav"/>          <!--Layered navigation on search result page-->
        <remove name="sale.reorder.sidebar"/>           <!--Reorder Sidebar When User Logged, in Dashboard-->
        <remove name="customer_account_navigation"/>    <!--Customer Navigation-->
    </default>
    <!--Home Page-->
    <cms_index_index>
    </cms_index_index>
    <!--All Cms Pages-->
    <cms_page>
    </cms_page>
    <!--Category View-->
    <catalog_category_view>
        <!--Set Page Template-->
        <reference name="root">
            <action method="setTemplate">
                <template>page/3columns.phtml</template>
            </action>
        </reference>
    </catalog_category_view>
    <!--Category View With Layered Navigation-->
    <catalog_category_layered>
    </catalog_category_layered>
    <!--Onepage Checkout Index Page-->
    <checkout_onepage_index>
    </checkout_onepage_index>
    <!--Onepage Checkout Success Page-->
    <checkout_onepage_success>
    </checkout_onepage_success>
    <!--Customer Accound Pages-->
    <customer_account>
        <!--Adds Body Class For All Dashboard Pages - MUST when Dashboard is present-->
        <reference name="root">
            <action method="addBodyClass"><className>customer-account-page</className></action>
        </reference>
    </customer_account>
    <!--Customer Logged In-->
    <customer_logged_in>
    </customer_logged_in>
    <!--Customer Logged Out-->
    <customer_logged_out>
    </customer_logged_out>
    <!--Product View-->
    <catalog_product_view>
        <!--Product Information Block-->
        <reference name="product.info">
            <!--Recently Viewed Products-->
            <block type="reports/product_viewed" name="product.viewed" template="reports/recently-viewed.phtml" />
        </reference>
    </catalog_product_view>
    <!--Catalogsearch Result Page-->
    <catalogsearch_result_index>
    </catalogsearch_result_index>
    <!--Advanced Search Result Page-->
    <catalogsearch_advanced_result>
    </catalogsearch_advanced_result>
    <!--Advanced Search Page-->
    <catalogsearch_advanced_index>
    </catalogsearch_advanced_index>
    <!--Cart-->
    <checkout_cart_index>
    </checkout_cart_index>
    <!--Contacts Page-->
    <contacts_index_index>
    </contacts_index_index>
</layout>