Tuesday, February 7, 2012

Add product list on MouseOver on My cart in top link Magento

if you want to display a dropdown list of products in  my cart on MouseOver on 'My Cart' in top menu then follow these easy step:

1.copy the following line of code and save this in file cartlist.phtml  at path
app\design\frontend\base\default\template\catalog\product\

code:
////////////
<?php if(Mage::helper('checkout/cart')->getSummaryCount()>0) { ?>
<div style="display:none;position: relative;left: 650px;width: 200px;top: -70px;" id="vinod">
<div class="block block-cart">
 
    <div class="block-title">
        <strong><span><?php echo $this->__('My Cart') ?></span></strong>
    </div>
    <div class="block-content">
        <?php
        $cartid= Mage::getModel('checkout/cart')->getProductIds();
   
      
            foreach($cartid as $pid)
            {
               $pname= Mage::getModel('catalog/product')->load($pid)->getName();
              ?>
               <p><b><?=$pid?>=<?=$pname?></b></p>
            <?php
               }
 
        ?>
           </div>  
</div>
</div>
<? }?>


//////////////

2. add this xml code in catalog.xml in default scope
app\design\frontend\base\default\layout\
////////////
<reference name="content">
             
          <block type="catalog/product" name="catalog.product.cartlist" template="catalog/product/cartlist.phtml"/>
          </reference>


///////////////////

3. find the following line of code in
app\code\core\Mage\Checkout\Block\ Links.php

//////////////

 public function addCartLink()
    {
      
        $parentBlock = $this->getParentBlock();
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
            $count = $this->getSummaryQty() ? $this->getSummaryQty()
                : $this->helper('checkout/cart')->getSummaryCount();
            if ($count == 1) {
                $text = $this->__('My Cart (%s item)', $count);
            } elseif ($count > 0) {
                $text = $this->__('My Cart (%s items)', $count);
            } else {
                $text = $this->__('My Cart');
            }
           
          
            $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
            $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
        }
        return $this;
    }

/////////////



and replace with 
///////////
 public function addCartLink()
    {
     
        $parentBlock = $this->getParentBlock();
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
            $count = $this->getSummaryQty() ? $this->getSummaryQty()
                : $this->helper('checkout/cart')->getSummaryCount();
            if ($count == 1) {
                $text = $this->__('My Cart (%s item)', $count);
            } elseif ($count > 0) {
                $text = $this->__('My Cart (%s items)', $count);
            } else {
                $text = $this->__('My Cart');
            }
           
          
            $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
            $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart" onMouseOver="$(\'vinod\').toggle();"');
        }
        return $this;
    }
 
/////////


that's it.


Enjoy!!!.....




Monday, February 6, 2012

how to add custom field in customer registration form in front end and admin end magento 1.6.X

let you want to add occupation for customer registration
1. Add the following code in the form tag in register.phtml and edit.phtml
app/design/frontend/default/yourstore/template/customer/form/register.html 

////////////////
 <div class="input-box">

                    <label for="occupation"><?php echo $this->__('Occupation') ?></label><br/>

                    <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />

                </div> 

/////////////////

2. replace the following block of code in
app/code/core/Mage/Customer/Model/Resource/Setup.php

//////////
 'group_id'           => array(
                        'type'               => 'static',
                        'label'              => 'Group',
                        'input'              => 'select',
                        'source'             => 'customer/customer_attribute_source_group',
                        'sort_order'         => 25,
                        'position'           => 25,
                        'adminhtml_only'     => 1,
                        'admin_checkout'     => 1,
                    ),     
///////////

with this
////////////

 'group_id'           => array(
                        'type'               => 'static',
                        'label'              => 'Group',
                        'input'              => 'select',
                        'source'             => 'customer/customer_attribute_source_group',
                        'sort_order'         => 25,
                        'position'           => 25,
                        'adminhtml_only'     => 1,
                        'admin_checkout'     => 1,
                    ),                   
                    'occupation'          => array(
                        'type'               => 'varchar',
                        'label'              => 'Occupation',
                        'input'              => 'text',
                        'sort_order'         => 7,
                        'validate_rules'     => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
                        'position'           => 120,
                    ),
///////////

3. find the following line of code around line 280  in
app/code/core/Mage/Customer/controllers/AccountController.php

///////
 $customerData = $customerForm->extractData($this->getRequest());
//////

and replace with
////////////
 $customerData = $customerForm->extractData($this->getRequest());
           //new code added to save occupation in database start here
            if($this->getRequest()->getParam('occupation'))
             {
                 $customer->setOccupation($this->getRequest()->getParam('occupation'));
                
             } 
              //new code added to save occupation in database end here
           

///////////

4. add the following code in
app/code/core/Mage/Customer/etc/config.xml
your
under the customer_account

////////
<occupation>
                 <create>1</create>
                 <update>1</update>
              </occupation>
//////

5. put this script in your register.phtml 's header file and remove it after one run
 <?php
  
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
  
    $AttrCode = 'occupation';
  
      $settings = array (
  
          'position' => 1,
  
          'is_required'=> 0
  
      );
  
      $setup->addAttribute('1', $AttrCode, $settings);
  
      ?>



To show this field in admin end in customer tab field


1. find and replace this function of code in
app/code/core/Mage/AdminHtml/Block/Customers/Grid.php

/////////////////
 protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
             ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }
/////////////////// 

and replace with

//////////////////
 protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('occupation')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
             ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }
/////////////////// 



2. find and replace this function of code in
app/code/core/Mage/AdminHtml/Block/Customers/Grid.php


///////////
protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
        ));
        $this->addColumn('firstname', array(
            'header'    => Mage::helper('customer')->__('First Name'),
            'index'     => 'firstname'
        ));
        $this->addColumn('lastname', array(
            'header'    => Mage::helper('customer')->__('Last Name'),
            'index'     => 'lastname'
        ));
       /* $this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name'
        ));*/
        $this->addColumn('email', array(
            'header'    => Mage::helper('customer')->__('Email'),
            'width'     => '150',
            'index'     => 'email'
        ));
        $this->addColumn('occupation', array(
            'header'    => Mage::helper('customer')->__('Occupation'),
            'width'     => '150',
            'index'     => 'occupation'
        ));

        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();

        $this->addColumn('group', array(
            'header'    =>  Mage::helper('customer')->__('Group'),
            'width'     =>  '100',
            'index'     =>  'group_id',
            'type'      =>  'options',
            'options'   =>  $groups,
        ));

        $this->addColumn('Telephone', array(
            'header'    => Mage::helper('customer')->__('Telephone'),
            'width'     => '100',
            'index'     => 'billing_telephone'
        ));

        $this->addColumn('billing_postcode', array(
            'header'    => Mage::helper('customer')->__('ZIP'),
            'width'     => '90',
            'index'     => 'billing_postcode',
        ));

        $this->addColumn('billing_country_id', array(
            'header'    => Mage::helper('customer')->__('Country'),
            'width'     => '100',
            'type'      => 'country',
            'index'     => 'billing_country_id',
        ));

        $this->addColumn('billing_region', array(
            'header'    => Mage::helper('customer')->__('State/Province'),
            'width'     => '100',
            'index'     => 'billing_region',
        ));

        $this->addColumn('customer_since', array(
            'header'    => Mage::helper('customer')->__('Customer Since'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'created_at',
            'gmtoffset' => true
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('website_id', array(
                'header'    => Mage::helper('customer')->__('Website'),
                'align'     => 'center',
                'width'     => '80px',
                'type'      => 'options',
                'options'   => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
                'index'     => 'website_id',
            ));
        }

        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('customer')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('customer')->__('Edit'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));

        $this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
        $this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
        return parent::_prepareColumns();
    }
///////////////

3...find the following line of code in
app\code\core\Mage\Adminhtml\Block\Customer\Edit\Tab\Account.php

/////
$suffixElement = $form->getElement('suffix');
        if ($suffixElement) {
            $suffixOptions = $this->helper('customer')->getNameSuffixOptions($customerStoreId);
            if (!empty($suffixOptions)) {
                $fieldset->removeField($suffixElement->getId());
                $suffixField = $fieldset->addField($suffixElement->getId(),
                    'select',
                    $suffixElement->getData(),
                    $form->getElement('lastname')->getId()
                );
                $suffixField->setValues($suffixOptions);
                if ($customer->getId()) {
                    $suffixField->addElementValues($customer->getSuffix());
                }
            }
        }
      
//////

and replace with
//////////////////////////////
$suffixElement = $form->getElement('suffix');
        if ($suffixElement) {
            $suffixOptions = $this->helper('customer')->getNameSuffixOptions($customerStoreId);
            if (!empty($suffixOptions)) {
                $fieldset->removeField($suffixElement->getId());
                $suffixField = $fieldset->addField($suffixElement->getId(),
                    'select',
                    $suffixElement->getData(),
                    $form->getElement('lastname')->getId()
                );
                $suffixField->setValues($suffixOptions);
                if ($customer->getId()) {
                    $suffixField->addElementValues($customer->getSuffix());
                }
            }
        }
        ///new code start here
         $fieldset->addField('occupation', 'text', array(
            'name'      => 'occupation',
             'label'=>'Occupation'
            
            ));
        
         ///new field end here



/////////////////////////////

4.find the follwing line of code around line no. 280 in file
app\code\core\Mage\Adminhtml\controllers\CustomerController.php

///////////////

if (isset($data['subscription'])) {
                $customer->setIsSubscribed(true);
            } else {
                $customer->setIsSubscribed(false);
            }
 ////////////////

and replace with
///////////////

if (isset($data['subscription'])) {
                $customer->setIsSubscribed(true);
            } else {
                $customer->setIsSubscribed(false);
            }
           //new code added to save occupation in database start here
          
            if(isset($data['account']['occupation']))
             {
              
                 $customer->setData('occupation',$data['account']['occupation']);
                
             } 
             //new code added to save occupation in database end here
///////////////



Enjoy !!!!!!!!

Friday, January 20, 2012

How to remove blank new line space in dreamweaver using regular expression

  1. >> open file
2. press Ctl+F
3. Choose regular expression select box in footer of search & replace window.
4. put "[\r\n]{2,}" in Find field
5. put "\n" in replace field
6 . press replace or replace all button.


Enjoy!!!!!

Tuesday, January 10, 2012

How to show customer group selecter in registration form

Make a copy of config.xml from app > code > core > Mage > Customer > etc > config.xml and add the following line of code into the customer account fieldset and save it to app > code >local>config.xml and in this file add the following line of code into the customer account fieldset:



<fieldsets>
<customer_account>
............
<group_id><create>1</create><update>1</update></group_id>
...........
</customer_account>
</fieldsets>


add code to the frontend of the store, in the registration form. Open template/customer/form/register.phtml and add the following code somewhere in the form:


<div class="input-box">
<label for="group_id"><?php echo $this->__('Group') ?><span class="required">*</span></label><br/>
<select name="group_id" id="group_id" title="<?php echo $this->__('Group') ?>" class="validate-group required-entry input-text" />
<?php $groups = Mage::helper('customer')->getGroups()->toOptionArray(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php print $group['value'] ?>"><?php print $group['label'] ?></option>
<?php } ?>
</select>
</div>


also change some code in app\code\core\Mage\Customer\controllers\AccountController.php now
replace $customer->getGroupId();
with

 if($this->getRequest()->getPost('group_id'))
 { $customer->setGroupId($this->getRequest()->getPost('group_id'));
 } else {
$customer->getGroupId(); }


Enjoy!!!



Thursday, January 5, 2012

PHP: Random password with numbers and letters

function genRandomString() {
    $length = 10;
    $characters = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
    $string = ;    

    for ($p = 0$p < $length$p++) {
        $string .= $characters[mt_rand(0strlen($characters))];
    }
    return $string;
}