以编程方式创建客户


13

我想以编程方式创建一些客户,并且在保存网站ID时遇到问题。

我有多个网站ID:

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

这是我的代码:

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

当我保存一个客户时,我Admin网站下拉列表中选择了值。无论我给网站ID(如12321)赋予什么价值,我都得到了Admin价值。为什么呢

谢谢。

Answers:


0

您可以尝试以下代码:

//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;
}

我收到此错误消息:致命错误:未捕获的Mage_Core_Exception:使用网站范围时必须指定客户网站ID
Attila Naghi

1
您正在尝试通过电子邮件加载客户吗?在以下位置引用此问题:app / code / core / Mage / Customer / Model / Resource / Customer.php:212 + 0-请阅读:inchoo.net/magento/programming-magento/…–
osrecio

0

这是我的代码的示例,您必须将网站ID设置2次。不要问我为什么。也许有人会为您提供更好的解决方案,但这对我来说很有效:

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

 $customer->save();

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

0

试试这个代码

$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('jd1@ex.com')
                    ->setPassword('somepassword');

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

注意 :

转到管理员端System > configuration > Customers > Customer Configuration > Account Sharing OptionsPer Website在字段中设置Share Customer Accounts

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.