3/28/2011

How To Create a custom url redirect in Magento using URL Rewrite Management

To make magento more SEO friendly, It needs to redirect to a custom url, By default magento have this feature but if you have created any module whose url like modulename/id/3, then you needs to redirect it to a SEO friendly url. To redirect the url to a "301 Permanent redirect" you just login to your admin panel, then go to -> Catalog -> URL Rewrite Management, from the drop down select Custom ,follow the below screenshot to make it more easy.

1) In the Id path give your current url (e.g: review/product/list/id/14/category/3)
2) In Request Path write same as Id path (e.g: review/product/list/id/14/category/3)
3) In Target Path give oyu custom Url with domain name (e.g:- http://www.mysite.com/mobile.html)

3/25/2011

How to create Special Product or Hot Product or Feature Product in magento

This is what I used many times while developed magento sites. I have made Feature products , Special Producst, Hot products etc by the custom attribute. To do so I am going to make some Special Product which will show on frontend, for this I will create a custom attribute Special Product whose code name will be special. First Log in to admin panel then Catalog->Attributes -> Manage Attributes . Now click on Add new Attribute, I n the Attribute Code field write special and from Catalog Input Type for Store Owner select Yes/No from the drop down, Choose NO from the Default Value .Now click on Manage Label / Options then in the Admin field write Special Product then click on Save Attribute button to save the attribute.

Now go to Catalog-> Attributes -> Manage Attribute Sets . Click on your Attribute set (e.g : - I choose Default attribute set) then add the Special attribute from Unassigned Attributes to Groups by drag and drop. Now go to any products which have same attribute set in which the Special Attribute is assign, There you can see that Special Product Option has came with yes/no drop down. Now make some product Yes as Special product.

Now go to frontend and write the below code where you want to fetch the Special Product.

$storeId = Mage::app()->getStore()->getId();
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$categoryProductTable = $resource->getTableName('catalog/category_product');
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
$eavAttributeTable = $resource->getTableName('eav/attribute');
// Query database for special product
$select = $read->select()
->distinct()
->from(array('cp'=>$categoryProductTable),'cp.product_id')
->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
->joinNatural(array('ea'=>$eavAttributeTable))
->where('pei.value=1')
->where('ea.attribute_code="special"');// Write your attribute code instead of Special
$rows = $read->fetchAll($select);
// Save all product Id which have the Special Product Yes
foreach($rows as $row)
{
$product[] =$row['product_id'];
}

Inside the $product variable you have all product Id which are Special Product . Now write the Below code to get Details of the Each product

<ul>
<?php foreach($product as $key=>$val)
{
$productobject = Mage::getModel('catalog/product');
$productmodel = $productobject->load($val);
echo "<li>";
echo "<h2>".$productmodel->getName()."</h2>";
echo "<a href=".$productmodel->getProductUrl()."><img src=".$productmodel->getImageUrl()."title=".$productmodel->getName()."/></a>";
echo "<p>".$productmodel->getDescription()."</p>";
echo "<p>".$productmodel->getFinalPrice()."</p>";
?>
<input type="button" value="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add', array('product'=>$val,'qty'=>1)) ?>')" />
<?php
echo "</li>";
?>
</ul>

You can see all the Special Product has come to your frontend. By this way you can make Feature Product or Hot Product . Enjoy..............

3/20/2011

How to add new tab under system configuration in magento

In the last post I have showned , how to add new Tab in magento admin panel. Now in this post I will add new Tab under System->Configuration.To add so, You need to create a new module. Click here to create an admin Module then make some changes which is written here.

create a system.xml file inside etc folder of your module, Then write the following code to add a tab


<?xml version="1.0"?>
<config>
    <tabs>
        <systab translate="label" module="systab">
            <label>System Configuration Tab</label>
            <sort_order>200</sort_order>
        </systab>
    </tabs>
    <sections>
        <systab translate="label" module="systab">
            <class>separator-top</class>
            <label>My system Configuration Tab</label>
            <tab>systab</tab>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <systab_option translate="label">
                    <label>sysem Tab Options</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
<registration_mode translate="label"> <label>On New User Signup</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </registration_mode>
<forget_mode translate="label">
                            <label>Send Email on Forget Password</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </forget_mode>
<purchase_mode translate="label">
                            <label>Send Email On Product Purchase</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </purchase_mode>
                    </fields>
</systab_option>
            </groups>
        </systab>
    </sections>
</config>

Now important things which needs to do. Go to your System->Cache Management then clear your all cache. Now go to System->Permissions->Roles. Clcik on Administrators, from Role Resources Tab select Resource Access All from the drop down and save. Now go to System ->Configuration .You can see a new tab has added in the left side.

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');