13

I want to create some customers programatically and I have an issue when I want to save the website ID.

I have multiple website IDs:

0 => admin
1 => germany
2 => hungary
3 => romania

This is my code:

 $customer = Mage::getModel("customer/customer");
 $customer->setWebsiteId(3);
 $customer->setStoreId(1);
.....
 $customer->save();

When I saved a customer I get the Admin value selected from the website drop down. Whatever value I give to the website Id (like 12321) I got the Admin value. Why ?

Thanks.

0

3 Answers 3

0

Can you try with this code:

//If you know store id
$storeId = 'id';
$store = Mage::getModel('core/store')->load($storeId); // Mage::app()->getStore($storeId);
if($store && $store->getId()) {
    $customer = Mage::getModel("customer/customer");
    $customer->setStore($store);
}


//->setStore reference:app/code/core/Mage/Customer/Model/Customer.php
/**
 * Set store to customer
 *
 * @param Mage_Core_Model_Store $store
 * @return Mage_Customer_Model_Customer
 */
public function setStore(Mage_Core_Model_Store $store)
{
    $this->setStoreId($store->getId());
    $this->setWebsiteId($store->getWebsite()->getId());
    return $this;
}
2
  • I get this error message: Fatal error: Uncaught Mage_Core_Exception: Customer website ID must be specified when using the website scope Commented Dec 20, 2016 at 9:29
  • 1
    You're trying load customer by Email? This problem is referred in: app/code/core/Mage/Customer/Model/Resource/Customer.php:212 +0- Please read: inchoo.net/magento/programming-magento/…
    – osrecio
    Commented Dec 20, 2016 at 9:33
0

This is the sample of my code and you have to set the website ID 2 times. Don't ask me why. Maybe someone will give you a better solution but this one is works for me:

 $customer->setWebsiteId(1);
 $customer->setStoreId(5);
 $customer->setData(.....)

 $customer->save();

 $customer->setConfirmation(null);
 $customer->setWebsiteId(1); 
 $customer->save();
0

Try this code

$websitesArray = array(0 => "admin",
                1 => "germany",
                2 => "hungary",
                3 => "romania");
foreach($websitesArray as $websiteId => $websiteName) {
    $website = Mage::getModel('core/website')->load($websiteId);
    if($website->getId()) {
        $customer = Mage::getModel("customer/customer");
        $customer->setWebsiteId($website->getId())
                    ->setFirstname('John')
                    ->setLastname('Doe')
                    ->setEmail('[email protected]')
                    ->setPassword('somepassword');

        try{
            $customer->save();
        }
        catch (Exception $e) {
        }       
    }
}

Note :

Go to admin side System > configuration > Customers > Customer Configuration > Account Sharing Options and set Per Website in field Share Customer Accounts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.