How to Get Store ID in Magento

Thursday, December 6, 2012
Many times during coding we require the store ID of the magento store, We can get this by using the below code in magento :
 Mage::app()->getStore()->getStoreId();
 OR
 $this->helper(’core’)->getStoreId();

Install Magento in Ubuntu

To install magento in Ubuntu, first Open the terminal to run the commands.
Navigate to the directory (Inside www folder) where you need to install Magento.
$ cd /var/www 
Download magento 1.7.0.0 files by using the following command:
$ wget http://www.magentocommerce.com/downloads/assets/1.7.0.0/magento-1.7.0.0-alpha1.tar.gz
After the downloading process completes, just decompress the downloaded file:
$ tar -zxvf magento-1.7.0.0-alpha1.tar.gz
Now you can find a new folder “magento”. Just get into it.
$cd magento
Then set permissions for some folders like media, var and app/etc to 777:
$ chmod -R 777 media var
$ chmod 777 app/etc

Get All Items & Totals In Magento Shopping Cart

Wednesday, October 31, 2012
This blog will show you how to get all items in a shopping cart and totals in a magento store :
 $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
 foreach($items as $item) {
    echo 'Product ID: '.$item->getProductId();
    echo 'Product Name: '.$item->getName();
    echo 'Product Sku: '.$item->getSku();
    echo 'Product Quantity: '.$item->getQty();
    echo 'Product Price: '.$item->getPrice();

Price Currency Conversion in Magento

This article is to show you how to convert price amount from one currency of the shop to another currency.
// Currency conversion rates have to be available in the target currency
$fromCur = ‘USD’; // currency code to convert from – usually your base currency
$toCur = ‘EUR’; // currency to convert to
$price = Mage::helper(‘directory’)->
currencyConvert(1000, $fromCur, $toCur);
// if you want it rounded:
$converted_final_price = Mage::app()->getStore()->roundPrice($price);

How to show Category List in Magento

You can show all categories present in magento.
There are different methods to get the category list. Below are some possible methods:-
Get all categories
The following code will fetch all categories (both active and inactive) that are present in your Magento Shop.
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*');

Get all active categories:

Emulation process in Magento

Thursday, September 27, 2012
Recently,I had a task to send an custom transactional e-mail from admin panel on some event.And the email template was same as used in the frontend store. So, I was trying to make use of the same block and phtml of frontend theme to call in that email. But,i found that that,we cannot call frontend phtml files in the emails sending from admin.
There is a concept of emulation in magento which allow us to do the same.By using this method we can make use of frontend template files to call in mails sending from Admin.
This feature is available in magento 1.5 & above version.By using Emulation model a.k.a. Mage_Core_Model_App_Emulation class , we can emulate stores in magento.
Code to be used:
$appEmulation = Mage::getSingleton('core/app_emulation');
//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
/*
 * Any code thrown here will be executed as we are currently running that store
 * with applied locale, design and similar
 */
//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
You will need something similar also,when you want to send custom transactional e-mail for all stores at the same time as cronjob. However, the problem is that e-mail had {{block}} inside and block template needed to respect different theme set for each store.

Debugging Magento Code with Net Beans IDE

Sunday, September 23, 2012

Mostly people are aware of how the xdebug is used on debugging PHP applications. However there can be people out there, alone, killing their eyes trying to find out which class called which functions etc. Here is a quick tip for you! Especially if you are trying to find your way with Magento!

NetBeans is a great IDE with it’s support to PHP!By using NetBeans and xdebug, we can freely and easily debug our PHP applications. However as the web evolved, there are not anymore php files to debug but there are “user friendly” urls to debug. So you cannot give the filename to NetBeans and say debug! because most of the web applications out there are now MVC based as this is the new trend in the PHP world. MVC means, one file -index.php dispatches all the requests. So how to debug using friendly urls.

Well you can use your NetBeans IDE to listen requests coming from your browser. That’s what NetBeans does when you click on debug button, which opens the start page (index.php) with a get variable like: ?XDEBUG_SESSION_START=netbeans-xdebug. This value (netbeans-xdebug) can change from your NetBeans configuration (click on preferences and choose the PHP tab). When this parameter is given, you can send any request to the NetBeans and it will listen!

All you need to do:

How to use a external Script to run Magento Code.

To make any external script to perform any task we need to include ‘app/Mage.php‘ of our Magento installation. Then, we can use the code of magento in that external script.
For example, to fetch Product data from Magento shop in any external script.

/**
* Include Mage.php of your Magento installation
*/
require_once("../magento/app/Mage.php");

Mage::app('default');
/**
* To get Product Data
* For example productId is '11'
*/
$productId = 11;
$product = Mage::getModel('catalog/product')->load($productId);
/**
*To Print name,,sku, price and quantity of the product
*/
echo "Name: ".$product->getName();
echo "Sku: ".$product->geSku();
echo "Price: ".$product->getPrice();
echo "Quantity: ".$product->getStockItem()->getQty();

Insert ,Update ,Delete and Select Records from Database Tables in Magento

You can select,insert,update and delete data in the Magento wby using their models concept.
For example,a table named ‘testdata‘ with the following fields:-
id : int, auto increment, primary key
firstname: varchar
lastname: varchar

How to add Category Attributes in Magento

We can add attributes to categories by running sql query in our database ,but this is not a good way of doing. We can use better way that is, set up scripts feature of magento.
We can create a custom module and add a mysql set up file inside below location:
CustomNamespace/CustomModule/sql/custommodule_setup/
Add the following code inside it to add category attributes :
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_category', 'custom_category_attribute', array(
'group' => 'General',
'input' => 'text',
'type' => 'varchar',
'label' => 'Custom New Category Attribute',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();

How to add breadcrumb in magento

Breadcrumb is necessary for making website pages user navigation friendly. It can be printed out in any of the php or phtml pages by below code: We can also add our own breadcrumbs as per our requirements in our custom module.A method defined in mage "Mage_Page_Block_Html_Breadcrumbs" Class.We can use it to add crumb:
addCrumb ($crumbName, $crumbInfo, $after=false)
There are two ways to add crumb i.e. one by xml layout and another by using code:
By using code we can add breadcrumb like this:
$breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
$breadcrumbs->addCrumb('home',array('label'=>Mage::helper('cms')->__('Home'), 'title'=>Mage::helper('cms')->__('Home Page'),
'link'=>Mage::getBaseUrl()));

Elastic Load Balancing with Sticky Sessions

Thursday, August 23, 2012
Before starting the post,i would like to explain about sticky session,
What is Session Affinity (Sticky Sessions)?
When you only have one application server talking to your clients life is easy:
all the session contexts can be stored in that application server’s memory for fast retrieval. But in the world of highly available and scalable applications there’s likely to be more than one application server fulfilling requests, behind a load balancer. The load balancer routes the first request to an application server, who stores the session context in its own memory and gives the client back a cookie. The next request from the same client will contain the cookie – and, if the same application server gets the request again, the application will rediscover the session context. But what happens if that client’s next request instead gets routed to a different application server? That application server will not have the session context in its memory – even though the request contains the cookie, the application can’t discover the context.

If you’re willing to modify your application you can overcome this problem. You can store the session context in a shared location, visible to all application servers: the database or memcached, for example. All application servers will then be able to lookup the cookie in the central, shared location and discover the context. Until now, this was the approach you needed to take in order to retain the session context behind an Elastic Load Balancer.

But not all applications can be modified in this way. And not all developers want to modify existing applications. Instead of modifying the application, you need the load balancer to route the same client to the same application server. Once the client’s request has been routed to the correct application server, that application server can lookup the session cookie in its own memory and recover the conversational context.

That’s what sticky sessions are:
the load balancer routing the same client to the same application server. And that’s why they’re so important: If the load balancer supports sticky sessions then you don’t need to modify your application to remember client session context.

"How to Use ELB with Sticky Sessions with Existing Applications

The key to managing ELB sticky sessions is the duration of the stickiness: how long the client should consistently be routed to the same back-end instance. Too short, and the session context will be lost, forcing the client to login again. Too long, and the load balancer will not be able to distribute requests equally across the application servers.

How to get all associated products to a configurable product in Magento

Saturday, August 18, 2012
Configurable product is the product which is created by some Associated product, If you have an configurable product and you want to get list of all associated product and it's attribute then, follow the below code:You must have your configurable product Id, My configurable product Id is 10
 
$proId=10; //Use your Configurable product Id
$_product = new Mage_Catalog_Model_Product();

How to change Admin URL Path?

I will show you, how you can change the admin url. Let’s say from ‘admin‘ to ‘administrater‘. So, the new admin URL will be http://www.magento.com/administrater/
Here is how we do it:-

How to setup MySQL Master-Slave Replication on magento

Wednesday, April 11, 2012
This is an article to show you ,how you can set MySql Master-slave replication in magento for scalability ,handle failover and performance.
You will have to use Slave database for read (select queries) Operations and Master database for write( insert and update queries).
Make changes in the following config file of magento:
app/etc/local.xml

<default_setup>
<connection>
<host><![CDATA[Master-host]]></host>
<username><![CDATA[user]]></username>
<password><![CDATA[pass]]></password>
<dbname><![CDATA[magentodb]]></dbname>
<active>1</active>
</connection>
</default_setup>

Integrate Memcached in Magento

Saturday, March 17, 2012
Integrate memcache in your magento application which will increase the speed of Magento page loads as much as possible..In this this article i will tell you how you can do it.
Magento has built in support of memcached ,you only need to enabled it by making some changes in the configuration file.
Memcached is a high-performance memory object caching system that is designed to speed page loads on dynamic content websites. Magento supports Memcached for caching many objects, but it is not enabled by default and will require some simple changes to your configured local.xml file to use.

First, be sure that Memcached is installed and running on your server. It by default listens on port 11211. A ‘netstat’ will verify that you do indeed have a Memcached listening socket on port 11211 indicating that is it running. Configuring Memcached to listen on localhost is recommended as it would be a security risk allowing any external sources access Memcached directly if it were to be listening on an external ip that wasn’t firewalled.

You also need to be sure that Memcached support is loaded into PHP. A view on a phpinfo() page can verify this, just look for the ‘Memcache’ block in the phpinfo and be sure it is ‘enabled’.

To enable Memcached within Magento, you will need to add the following block of code to your app/etc/local.xml file: