Friday, 4 July 2014

How to Create new Customer in magento or update customer info in magento

$customer = Mage::getModel('customer/customer');
$password = '123456';
$email = 'sanjay@rao.com';
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);

if(!$customer->getId()) {
    $customer->setEmail($email);
    $customer->setFirstname('Sanjay');
    $customer->setLastname('Rao');
    $customer->setPassword($password);
}
try {
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();
    //Make a "login" of new customer
    Mage::getSingleton('customer/session')->loginById($customer->getId());
}
catch (Exception $ex) {
    Zend_Debug::dump($ex->getMessage());
}



How to Get All State List In Magento for specific country

//below code load all the State  of US
$countryCode= ‘US’;
$states = Mage::getModel('directory/country')->load($countryCode)->getRegions();
$slist= array();
foreach ($states as $state) {
$sid = $state->getId();
            $sname = $state->getName();
            $slist[$sid]=$sname;                                                              
}

asort($slist);

How to Get All Country List In Magento

$clist= array();
$collection = Mage::getModel('directory/country')->getCollection();
foreach ($collection as $country){
$cid = $country->getId();
            $cname = $country->getName();
            $clist[$cid]=$cname;
}

asort($clist); // sort country list 

How to Get Customer Default Shipping Detail Using Customer Id in Magento

If you know the customer id than direct use it other wise first you can get customer id than use that.

$customerId =2; // already known

Mage::getSingleton('customer/session')->loginById($customerId);
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
$address = Mage::getModel('customer/address')->load($customerAddressId);
$address_data = array (
            'prefix' => $address->getPrefix(),
            'firstname' => $address->getFirstname(),
            'lastname' => $address->getLastname(),
            'street' => $address->getStreet(),
'city' => $address->getCity(),
            'region_id' => $address->getRegionId(),
            'region' => $address->getRegion(),
            'postcode' => $address->getPostcode(),
            'country_id' => $address->getCountryId(),
            'telephone' => $address->getTelephone(),
            'email' => $address->getEmail()

);

How to Get Customer Detail Using Email Id in Magento

$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId()); 
$customer->loadByEmail($customer_email);
//get customer id
$customerId = $customer->getId();
//get customer Email
$customerEmail = $customer->getEmail ();
//get customer First Name
$fname = $customer->getFirstname();
//get customer Last Name
$lname = $customer->getLastname();
//get customer Full Name
$fullname = $customer->getName();

Wednesday, 16 April 2014

PHP: How to Copy All Files and folders Within A Directory

function recurse_copy($src,$dst) {
        $dir = opendir($src);
        @mkdir($dst, 0777);
        while(false !== ( $file = readdir($dir)) ) {
                if (( $file != '.' ) && ( $file != '..' )) {
                        if ( is_dir($src . '/' . $file) ) {
                                $this->recurse_copy($src . '/' . $file,$dst . '/' . $file);
                        }
                        else {
                                copy($src . '/' . $file,$dst . '/' . $file);
                        }
                }
        }
        closedir($dir);
    }

Example : How to use above Function

$src='www/abc'; // copy all the files and folder within abc directory
$dst = 'www/newdir' //
recurse_copy($src,$dst);

above function first create the destination directory and than copy all the source directory content.
Tips: to create directory and set its permission using following code in php
mkdir($path, 0777, true)

PHP: Unlink All Files Within A Directory, and then Deleting That Directory

function recursiveRemoveDirectory($directory)
    {
        foreach(glob("{$directory}/*") as $file)
        {
                if(is_dir($file)) {
                        $this->recursiveRemoveDirectory($file);
                } else {
                        unlink($file);
                }
        }
        rmdir($directory);
    }

Example : How to use above function.
$directory = 'www/abc';
recursiveRemoveDirectory($directory)

List OF BANK PAN Numbers

List OF BANK PAN Numbers Bank/Home Loan Providers PAN Number Allahabad Bank AACCA8464F Andhra Bank AABCA7375C Axis Bank...