Tuesday, February 14, 2012

Most Frequently Used Functions in Magento

1)Magento: How to call static block from template file?
We can Easily call a static block directly from template.
Let us suppose, you created a static block with the identifier aforankur_link.
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId(‘aforankur_link’)->toHTML();?>

2)Magento: How to call block directly from phtml file without defining in layout?
<?php echo $this->getLayout()->createBlock(‘newmodule/newblock’)->setTemplate(‘newmodule/newblock.phtml’)->toHtml(); ?>

3)Magento: Add Static Block to CMS Page
{{block type=”cms/block” block_id=”home-page-promo”}}

4)Magento :How to call a phtml file in magento cms page
{{block type=”core/template” name=”a-name” template=”cms/home.phtml”}}


5) Magento Store – Get Base URL in Static Block
Try adding this to your static block:
<a href=”{{store url=”"}}”>Link to Base URL</a>

6) Mageto  : Skin URL
<a href=”#”><img src=”{{skin url=’images/home/dog-playing.jpg’}}” alt=”"/></a>

7) Magento: get Url
<a href=”<?php echo $this->getUrl(‘about-us’)?>”>

Thursday, February 9, 2012

Magento - Display total weight of Products in Cart

Here is some code snippet need to put -

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$weight = 0;
foreach($items as $item) {
    $weight += ($item->getWeight() * $item->getQty()) ;
}
echo $weight;

Custom Code for Sending Email to Customer



$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

$order->loadByIncrementId($incrementId);

try
{
$order->sendNewOrderEmail();
} catch (Exception $ex) {  }

Wednesday, February 8, 2012

Magento | Use Mini-Login in Header or anywhere you want!

Add Mini-login at Header

Reach to following path and open the given xml file - 

app/design/frontend/default/default/layout/page.xml
  <block type="customer/form_login" name="mini_login" template="customer/form/mini.login.phtml" />
Then, you just have to call the module :
 <?php echo $this->getChildHtml('mini_login'?>

Thursday, February 2, 2012

Magento: Upgrading mysql setup of a module

MyModule. Its version is 0.1.0. Now, you want to do some database changes for the module. You have the mysql setup file (mysql install file) mysql4-install-0.1.0.php inMyModule/sql/mymodule_setup folder of your module.
You don’t need to make direct changes to database. You can upgrade your module to make your necessary database changes. To do so,
---------------------------------------------------------

1) You need to create a new mysql upgrade file inside MyModule/sql/mymodule_setupfolder.
Let us suppose that you are going to change the version of your module from 0.1.0 to 0.1.1.
The mysql upgrade file name should be mysql4-upgrade-0.1.0-0.1.1.php
The upgrade format is like mysql4-upgrade-CURRENT_VERSION-UPGRADED_VERSION.php
2) Write necessary sql statements in the newly added mysql upgrade file.
3) You have to change the version in MyModule/etc/config.xml as well.
Change the version like 0.1.1. 0.1.1 is the new version for our module.
4) Reload your site. And you are done!

Friday, January 20, 2012

Code to get multiple SKUs in Sales->Order Grid in Magento Admin



$collection = Mage::getResourceModel($this->_getCollectionClass())
   ->join(
    'sales/order_item',
    '`sales/order_item`.order_id=`main_table`.entity_id',
    array(
        'skus'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
        'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
        )
    );
$collection->getSelect()->group('entity_id');

Monday, January 9, 2012

Magento | Get Cart Summary


Magento Get Cart Items and Cart Total -



To display items in cart use following code -

$this->helper('checkout/cart')->getSummaryCount();
or 


Mage::helper('checkout/cart')->getSummaryCount();

And for the Cart Total: 

Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

This will display only the total amount for the products. 
If you want the grand total (including shipping & handling) do this: 



Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();

Google - Website Translator Gadget for Magento

you don’t need a plugin for that. Just add this code:


<div id="google_translate_element"></div><script>
function 
googleTranslateElementInit() {
  
new google.translate.TranslateElement({
    pageLanguage
'en'
  
}'google_translate_element');}
</script>
<script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>



anyware you want in any phtml pages. 
But be careful. The translations is far from perfect.

How To Install A Downloaded Magento Theme


Unlike single template themes (WordPress) Magento themes are quite different as its layout depends on four elements i.e.
  1. Layout – Present is (app/design/frontend/your_interface/new_theme/layout/)
  2. Templates – Present in (app/design/frontend/your_interface/new_theme/template/)
  3. Skins – Present in (skin/frontend/your_interface/new_theme/)
  4. Locale – Present in (app/design/frontend/your_interface/new_theme/locale/)
In order to install and integrate a new theme you need to follow few steps more than what you are accustomed to follow in case of wordpress themes.
If you have already downloaded your preferred Magento theme then here is how you can install and integrate this new theme into your existing Magento installation.

  1. Uncompress the zip file containing theme templates (you can use winrar or winzip to uncompress files on windows machine or use unzip zipfilename.zip to uncompress files on Linux server)
  2. Copy entire app folder of your new theme underYour_Magento_Installation/app/design/frontend/default/ so it becomes likeYour_Magento_Installation/app/design/frontend/default/new_theme
  3. Copy entire skin folder of your new theme underYour_Magento_Installation /skin/frontend/default/ so it becomes like Your_Magento_Installation/ skin/frontend/default/ new_theme
  4. Now, as you have added your newly downloaded theme in your Magento store you need to login to your Magento store admin to tell Magento which theme to use as layout.
  5. Sometimes Magento Cache doesn’t let you see your changes instantly. In order to test your new theme it would be wise to disable cache for the time being. Go to System > Cache Management and select “Disable” then click “Save Cache”. Once you are done with all the steps Enable the cache again.
  6. Now go to System -> Configuration and select the Design Tab
  7. Type the name of your new theme which you have copied in your Magento installation in front of skin (Images/CSS) input box and click “Save Config”.
  8. Optional – If you have downloaded a widget ready Magento template then you need to install the widget provided with the theme. In order to install Magento theme widget you have to go to CMS -> Static Blocks and click on “Add New Block”. Based on your widget installation instructions you have to add your new widget and activate that using this new widget block. Finally select “Enabled” and click “Save Block”. Go and select “Custom Design” Tab and in Layout select “Homepage” otherwise your widget won’t show on home page.
  9. Optional – Home page of default Magento store is a CMS block so you have to provide custom code there in order to display something on home page. To do so you have to go to CMS- > Manage Pages and select “Home page” and in the content area type the home page content code which comes up with your new theme. You can change this area anytime you want.
  10. All set. Open up a new page on your browser and type in your store name to see the new theme. Your new Magento theme is ready to be used.

How To Install a Magento Theme Via Magento Connect?


To install a theme from Magento Connect you need to follow certain steps which are explained below (Advance users may skip few steps):
1. Select the theme by browsing the huge Magento theme archive. OR, If you know the direct URL of your favourite theme just open it’s Magento connect URL
2. Now, you will see a button right under the theme image called “Get Extension Key”. You need to click “Get Extension Key” and agree to extension license agreement.
3. After you select agree checkbox and click “Get Extension Key” you will see the Magento extension key right there itself (displayed in the box). You need to paste this extension key in your store’s Magento connect tab. Keep it safe.
4. Copy this Magento extension key. E.g. It will look like “magento-community/Magik_Autocomplete”
5. In order to install this theme into your Magento store you need to login to your store admin panel and go to System->Magento Connect->Magento Connect Manager. Magento connect manager will ask you to login again. You need to use your store admin login credentials to get past the login screen.
6. Soon after you see “Theme successfully installed” message go toSystem->Configuration->Design->Themes and provide the theme name in Default field (which will change your default theme to this new theme) and click “Save Config” button at the top right corner of your screen.
7. A Magento theme revolves around different columns in your theme layout. In case your store home page doesn’t load properly and shows weird design then go to CMS->Manage Pages. You will notice that there are two different layouts for the home page (may be more depending upon how many themes you have tried in the past). Based on your new theme layout disable all the other home page layouts. You can easily disable/enable the layouts by clicking “Enabled” or “Disabled” links provided in the status field.
8. Check your home page if the store is looking just like your new theme, you are done. If you still see problems chances are that you haven’t disabled the correct home page layout. Repeat step 7 until you see a correct store home page (not required if you have only two home page layouts showing in your “Manage Pages” section.
This was the easiest and fastest way of installing a Magento theme using Magento Connect manager.
Many Magento theme sites don’t publish their theme template in Magento connect (sometimes their themes do not pass through Magento connect guidelines). These themes are provided directly by the theme owner sites. If you are interested in downloading a Magento theme from such sites then following are the steps required to install a Magento theme within your existing Magento installation.