3/19/2011

How to adding new menu in magento admin panel

Here I have Created a new Module to Add new tab in admin panel of magento.
My Namesapce Name is Anjan and My Module name is Systab
As you know to make a module first we need to make a .XML file with Namespace_Module name under app/etc/modules/ folder. So My File name will be Anjan_Systab.xml

Now I will write the following code inside this xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Anjan_Systab>
            <active>true</active>
            <codePool>local</codePool>
        </Anjan_Systab>
    </modules>
</config>

As My CodePool is local which is written in Anjan_Systab.xml I will create a Folder with name Anjan inside app/code/local/, then again will create another folder with name Systab inside Anjan Folder.Now I will create an etc folder inside the Systab Folder. Inside the etc folder I will create config.xml and will write the following code

<?xml version="1.0"?>
<config>
    <modules>
        <Anjan_Systab>
            <version>0.1.0</version>
        </Anjan_Systab>
    </modules>
    <frontend>
        <routers>
            <systab>
                <use>standard</use>
                <args>
                    <module>Anjan_Systab</module>
                    <frontName>systab</frontName>
                </args>
            </systab>                        
        </routers>
        <layout>
            <updates>
                <systab>
                    <file>systab.xml</file>
                </systab>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
            <systab>
                <use>admin</use>
                <args>
                    <module>Anjan_Systab</module>
                    <frontName>systab</frontName>
                </args>
            </systab>
        </routers>
    </admin>
    <adminhtml>
        <menu>
            <systab module="systab">
                <title>My Tab</title>
                <sort_order>71</sort_order>
                <children>
                    <items module="systab">
                        <title>Manage Profile</title>
                        <sort_order>0</sort_order>
                        <action>systab/adminhtml_systab</action>
                    </items>
                </children>
            </systab>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <systab>
                                            <title>System Configuration Tab</title>
                                        </systab>
                                    </children>
                                </config>
                            </children>
                        </system>
                        <Anjan_Systab>
                            <title>System Configuration Tab</title>
                            <sort_order>10</sort_order>
                        </Anjan_Systab>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <systab>
                    <file>systab.xml</file>
                </systab>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <models>
            <systab>
                <class>Anjan_Systab_Model</class>
                <resourceModel>systab_mysql4</resourceModel>
            </systab>
            <systab_mysql4>
                <class>Anjan_Systab_Model_Mysql4</class>
                <entities>
                    <systab>
                        <table>systab</table>
                    </systab>
                </entities>
            </systab_mysql4>
        </models>
        <resources>
            <systab_setup>
                <setup>
                    <module>Anjan_Systab</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </systab_setup>
            <systab_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </systab_write>
            <systab_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </systab_read>
        </resources>
        <blocks>
            <systab>
                <class>Anjan_Systab_Block</class>
            </systab>
        </blocks>
        <helpers>
            <systab>
                <class>Anjan_Systab_Helper</class>
            </systab>
        </helpers>
    </global>
</config>

Now Clear your magento cache and login to you admin panel You can see one Tab Named My Tab has been successfully created.

3/15/2011

How to get customer First Name, Last Name , Email Address or Details in magento

Before geting the details of a customer you must to have check whether Customer is Logged in or not, If a customer is logged in then only you can get details fo the customer. To check whether user logged in or not click here or you can go to http://xhtmlandcsshelp.blogspot.com/2010/11/magento-check-if-user-logged-in-or-log.html to get the code. Then write the code below to get customer details

$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();// To get Email Id of a customer
$firstname = $customer->getFirstname();// To get Firstname of a customer
$lastnam e= $customer->getLastname();// To get Last name of a customer

3/09/2011

How to Remove Product from cart on checkout page or by coding in magento

From magento cart you can delete product by clicking on delete button from cart page , but if you wish to delete any cart item in checkout page or by coding then you have write

$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();

$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach ($cartItems as $item)
{
    $quote->removeItem($item->getId())->save();
}

By writing the above code your all cart item will be delete.If you wish to delete a particular product from the cart session then instead of writing $item->getId() pass your Id of the product.

e.g: - foreach ($cartItems as $item)
{
    if($item->getId()== 2)
    {
        $quote->removeItem($item->getId())->save();
    }
}

How to get all cart items from cart session in magento

This is one of the most usefull features for all magento developers.As it's very necessary while anybody working on Magento cart or checkout page.To get all cart item from cart session write the below code

$session= Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllItems() as $item)
{
   $productid = $item->getProductId();
   $productsku = $item->getSku();
   $productname = $item->getName();
   $productqty = $item->getQty();
}

How to get shopping cart session in magento

While working on Cart page or checkout page, if you want to get the cart session value then You have to use this code in magento

$session= Mage::getSingleton('checkout/session');

2/28/2011

How to get Magento Format Price with default currency symbol

Whenever we fetch all data of a product by using either product sku or by using product id, Price also we need . But if we fetch product price then it display price as number format without any currency . So if you want to fetch price like magento price format then you have to pass your price variable in the following code then you can get the dynamic currency also.

Let you got your price by

$price = $_Pdetails->getPrice();
// To fetch price like magento format price write
$magento_style_price = Mage::helper('core')->currency($price);

How to get product details using product sku in magento

If you know your product SKU you can get details of that products, e.g- you can get Product Id, Product Name, Product Description, Product Price etc.
To get details fo the product by sku please write the below code

$products = Mage::getModel('catalog/product')
$_Pdetails = $product->loadByAttribute('sku', 'product-sku');

or

$_Pdetails =
Mage::getModel('catalog/product')->loadByAttribute('sku','product-sku');

echo $_Pdetails->getName();
echo $_Pdetails->getDescription();
echo $_Pdetails->getPrice();
echo $_Pdetails->getProductUrl();

2/21/2011

How to get absolute path of base directory in magento

While uploading a file in magento It's necessary to write the absolute path of your magento path, otherwise it will not upload. you can get your site absolute path from php.ini file, You can place this path by hardcording but later if you think to move your site to another host then the path will be change, So to make your absolute path dynamic or get the proper path in which your magento Installed, write the following code

<?php echo Mage::getBaseDir('media') . DS ;?>

It will definately work,

2/18/2011

How to get access of magento core functions from outside the magento directory

This is the very important thing in magento while accessing data from outside of the magento, You also can use it to set cron job of any magento function, as cron job doen't work properly in magento.


require_once 'app/Mage.php'; // if your are not root folder then write the proper path like publichtml/magento/app/Mage.php
Mage::app('default');

Now you can create object of any classes and can access methods of those classes also.
Like the Below example

$obj = new Mage_Checkout_Model_Observer();
echo $obj->salesQuoteSaveAfter();

2/17/2011

Fix for Magento Admin Login not working on Localhost

When you want to install magento in your local machine by either wamp server or by Xamp server then you will face a problem, while logging into magento admin panel, after successfull install.
Here are the fix to logging into magento.This fix work above Magento version 1.4.1
Go to the location app/code/core/Mage/Core/Model/Session/Abstract/ and open Varien.php then go to line number 86. There you will find the below code. if (!$cookieParams['httponly']) {
unset($cookieParams['httponly']);
if (!$cookieParams['secure']) {
unset($cookieParams['secure']);
if (!$cookieParams['domain']) {
unset($cookieParams['domain']);
}
}
}
if (isset($cookieParams['domain'])) {
$cookieParams['domain'] = $cookie->getDomain();
}

Comment this code, then your code will look like this


/* if (!$cookieParams['httponly']) {
unset($cookieParams['httponly']);
if (!$cookieParams['secure']) {
unset($cookieParams['secure']);
if (!$cookieParams['domain']) {
unset($cookieParams['domain']);
}
}
}

if (isset($cookieParams['domain'])) {
$cookieParams['domain'] = $cookie->getDomain();
}*/

Save the file and Refresh your admin panel page and try to login once again , Now you will be able to login to your Admin Dashboard