Magento 1.8 – Programmatically Setting a Customer’s Website ID

createcustomermagento-1.8

Could someone please advise why when adding a customer via an external script the website and store values are ignored and the customer is always assigned to "admin" and not to "main website".

I only have 1 website and 1 store within my Magento instance and each has an id of 1

My code is below:

    $customer = Mage::getModel("customer/customer");
    $customer   ->setWebsiteId(1)
                ->setStore(1)
                ->setFirstname('John')
                ->setLastname('Smith')
                ->setEmail('john@smith.com')
                ->setPassword('password')
                ->setGroupId('1');
    try{
        $customer->save();
    }
    catch (Exception $e) {
        Zend_Debug::dump($e->getMessage());
    }

Best Answer

Instead of using setStore() you could have use setStoreId()

$customer = Mage::getModel('customer/customer');

$email = 'john@smith.com';
$storeId = 1;

$website_id = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
$customer->setWebsiteId($website_id);
$customer->setStoreId($storeId);
...
Related Topic