Friday 12 October 2012

Magento: How to Login to magento admin externally or programmatically

Magento: How to Login to magento admin externally or programmatically.

There may be some case, Where we need to login to magento admin externally and make some updates without login to the magento admin panel.

save the below code in a file and make sure that the path for Mage.php is correct depending on the location of the file you created.

<?php
require_once 'app/Mage.php';
umask(0);
$app = Mage::app('default');

Mage::getSingleton('core/session', array('name' => 'adminhtml'));

// supply username
$user = Mage::getModel('admin/user')->loadByUsername('admin'); // user your admin username

if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
  Mage::getSingleton('adminhtml/url')->renewSecretUrls();
}

$session = Mage::getSingleton('admin/session');
$session->setIsFirstVisit(true);
$session->setUser($user);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));

if ($session->isLoggedIn()) {
  echo "Logged in";
}
else{
    echo 'Not Logged';
    }

Thursday 11 October 2012

Magento: How to share customer while using multistore

Magento: How to share customer while using multistore

By default magento is built under website->store->storeview concept. so magento is highly flexible to share customer details among different store.
 The customer by logging in a store, can access any store with same session. To do so.

Step 1
1) create stores as much you need.
2) Login to admin->system->configuration
3) In the left side menu, under Customers select Customer Configuration.
4) under Account Sharing Options set Share Customer Accounts to Global
5) Save Configuration.

Step 2. 
We need to enable cookies
1) system->configuration.
2) In the left side menu, under General select Web.
3) under Session Cookie Management  set Cookie Path to /
4) Save Configuration.

step 3.
clear your browser cookies and try to login in frontend as customer, and the open other store created, you can see the customer is logged in to all store.  

Magento: How to list categories in sidebar in Magento site

Magento: How to list categories in sidebar in Magento site.

Let me help you to create a module to list categories in sidebar or where ever wanted. This is a very simple Module.

Step 1:
Namespace: Dinesh
Module Name: Sidebarcategory

First we need to create a xml inside modules folder to get out module active inside magento

create a Dinesh_Sidebarcategory.xml in the following path "app\etc\modules\Dinesh_Sidebarcategory.xml" and add the conetent below.

---------------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!--
/**
 * @Sidebarcategory   Dinesh
 * @package    Dinesh_Sidebarcategory
 * @author     Dinesh
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 -->
<config>
    <modules>
        <Dinesh_Sidebarcategory>
            <active>true</active>
            <codePool>local</codePool>
        </Dinesh_Sidebarcategory>
    </modules>
</config>

---------------------------------------------------------------------------------------------------------------------------
Step 2:

Now lets create a config.xml file in the following path "app\code\local\Dinesh\Sidebarcategory\etc\config.xml" and add the code below

----------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!--
/**
 * @Sidebarcategory   Dinesh
 * @package    Dinesh_Sidebarcategory
 * @author     Dinesh
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 -->
<config>
    <modules>
        <Dinesh_Sidebarcategory>
            <version>0.1.0</version>
        </Dinesh_Sidebarcategory>
    </modules>

    <global>
        <models>
            <sidebarcategory>
                <class>Dinesh_Sidebarcategory_Model</class>
           </sidebarcategory>
          </models>
        <blocks>
            <sidebarcategory>
                <class>Dinesh_Sidebarcategory_Block</class>
            </sidebarcategory>
        </blocks>
      </global>
</config>

------------------------------------------------------------------------------------------------------------
Step 3:

We need to create a model file, where we can write some function. Create Sidebarcategory.php under path "app\code\local\Dinesh\Sidebarcategory\Model\Sidebarcategory.php" and add below code.
-----------------------------------------------------------------------------------------------------------

<?php
class Dinesh_Sidebarcategory_Model_Sidebarcategory extends Mage_Core_Model_Abstract
{
  
  
    public function getSubCategories($cat_id, $depth)
      {
        $menu=$this->getSubCategory($cat_id, $depth);
        $this->setData('menu',$menu);
        return $this;
    }
  
  
    public function getSubCategory($cat_id, $depth)
      {  
        $root_category = Mage::getModel('catalog/category')->load($cat_id);
        $subcategories = $root_category->getChildren();
        $current_cat_id = Mage::getModel('catalog/category')->load(Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId());
        $active=explode('/',$current_cat_id->getPath());
        if(strlen($subcategories)>0)
        {
            $menu.='<ul>';  
            foreach(explode(',',$subcategories) as $subcategory)
            {
                $category = Mage::getModel('catalog/category')->load($subcategory);
                if($category->getLevel() <= $depth || $depth==0)
                {
                    if(in_array($category->getId(), $active))
                    {$activeval='active';}else {$activeval='';}
                    $menu.= '<li class="level'.$category->getLevel().' '.$activeval.'" id="cat'.$category->getId().'">';
                    $menu.= '<a href="'.$category->getURL() .'" />'.$category->getName().'</a>';
                    $menu.=$this->getSubCategory($subcategory,$depth);
                    $menu.= '</li>';
                }
              
             }
             $menu.='</ul>';
         }
      
        return $menu;
  }
 
}

--------------------------------------------------------------------------------------------------------------

Step 4:

Create a block file  Sidebarcategory.php under the path "app\code\local\Dinesh\Sidebarcategory\Block\Sidebarcategory.php" and add the below code.

------------------------------------------------------------------------------------------------------------------------------------

 <?php
class Dinesh_Sidebarcategory_Block_Sidebarcategory extends Mage_Core_Block_Template
{
  
}

------------------------------------------------------------------------------------------------------------------------------------

Step 5:
Now we need to create a template file to display the category list. Create categorylist.phtml under path "app\design\frontend\default\default\template\sidebarcategory\categorylist.phtml " and add the following code below

NOTE: I have added the css part in phtml file itself, please donot do this, add to your css file and add design/css as per the design your using.
--------------------------------------------------------------------------------------------------------------

<?php
echo $this->getData('depth');
    if($this->getData('catid'))
    $root_category_id = $this->getData('catid');
    else
    echo $root_category_id= Mage::app()->getStore()->getRootCategoryId();
    if($this->getData('depth'))
    $depth=$this->getData('depth');
    else
    $depth=4;
    echo $depth;
    echo '<br/>'.$root_category_id;
    $cat=Mage::getModel('sidebarcategory/sidebarcategory')->getSubCategories($root_category_id,$depth);
    echo $cat->getData('menu');

?>
<style type="text/css">
ul,li{
margin-left:10px;
}
</style>

--------------------------------------------------------------------------------------------------------------
Step 6:

Now we have successfully create the module. In step 6 we need to call the module from a static block. and assign to a widget and display it where every needed(rightbar/leftbar/footer/anywhere..).

Login to admin 

cms->staticblock add new block.

Block Title: Left category listing

Identifier : left_category_listing

Status: enable

Content 

 {{block type="sidebarcategory/sidebarcategory" depth=3  template="sidebarcategory/categorylist.phtml"}}


Save block

Note:
 Here depth is the variable used to define the category levels.
depth=0 list all levels of category(cat->subcat->subsubcat->..............)
depth=3 list only(main category)
depth=4 lists(main category->sub category).
and so.. on...
Donot use depth=1 or 2 . they belong to the rootcategory.


Optional Param .
{{block type="sidebarcategory/sidebarcategory" depth=3  catid=22 template="sidebarcategory/categorylist.phtml"}}

catid=22 (provide the category id and it will display only the category and its subcategory for the given id , based on depth value)

if the param is not specified it will list category of store rootcategory.

--------------------------------------------------------------------------------------------------------------
Step 7:
Now we are going to create a widget and assign a static block to it.

In admin Area

Cms->Widgets

Add new widget instance

Type: Cms static block

Design: your current  theme package i have used the default package (default/default).

Assign to Store Views : All store views

 Layout Updates

Display On: All pages

Block Reference: Left column

Widget properties.

Block: select the static block we created.

Save Widget.

That's it we have complete our module call, now just go to your front and refresh to see the category listed in the leftcolum of your theme. note that your on the page that contain leftcolumn

Thanks for reading it.