2014-02-12 27 views
0

我創建了3個網站magento admin但他們現在沒有不同的網址。聯繫客戶到可用網站

我想要做的事情是,在註冊頁面上我已經增加了一個領域website,我想程序創建一個選擇框爲:

<select name="website"> 
    <option value="">Please select...</option> 
    <?php 
     $websites = Mage::app()->getWebsites(); 
     foreach ($websites as $web) { 
      echo "<option value='" . $web->getId() . "'>" . 
        $web->getName() . "</option>\n"; 
     } 
    ?> 
</select> 

現在,當用戶提交此表,根據所選的website他需要與它關聯。

對於這個我已經重寫了Customer/AccountControllercreatePostAction()但我想知道我怎麼分配這website idparent::createPostAction()有太多的抽象。

有沒有簡單的方法來做到這一點?

回答

0

finally found this answer helpful。但是我在觀察者身上做了一些改變,我需要讓客戶選擇website id

下面是從我觀察代碼

public function setWebsiteId($object) { 
    //getting website_id from user selection 
    $webid = Mage::app()->getFrontController()->getRequest() 
      ->getParam('website_id', Mage::app()->getStore()->getWebsiteId()); 
    $customer = $object->getCustomer(); 
    $customer->setWebsiteId($webid); 
    return $this; 
} 

事件需要處理的是:customer_register_success

修訂

上面的代碼工作正常,但上述問題實施是,如果用戶已經在當前網站註冊,那麼從當前網站他將無法註冊另一個網站。爲了解決這個問題,我想重寫了Customer/AccountControllercreatePostAction()

public function createPostAction() { 
    $post = $this->getRequest()->getPost(); 
    if (isset($post['website_id'])) { 
     $webid = $this->getRequest()->getParam('website_id'); 
     $customer = $this->_getCustomer(); 
     $customer->setWebsiteId($webid); 
     Mage::register('current_customer', $customer); //this is the important line here 
    } 
    parent::createPostAction(); 
} 

如果我不這樣做Mage::register('current_customer', $customer);然後父createPostAction()將會再次獲取客戶對象,我在那裏已經建立以前$customer = $this->_getCustomer();和損失website_id

+0

這似乎工作正常,但問題是之後客戶轉發到帳戶歡迎屏幕,他們沒有登錄。姓名和電子郵件信息也失蹤了。如果您點擊了您啓動登錄的任何帳戶鏈接。 – Corgalore

+0

@Corgalore:上面的代碼只是應該將客戶關聯到magento的可用網站。沒有用於重定向客戶來歡迎屏幕或登錄的代碼。可能是一些阻止用戶登錄的錯誤。檢查管理員是否創建客戶?我建議通過覆蓋控制器而不是觀察者來使用第二種方法。 –

1

嘗試命名您的選擇website_id而不是website
website_id具有保存客戶實體時調用的後端模型Mage_Customer_Model_Customer_Attribute_Backend_Website
所以每次調用$customer->save這段時間被稱爲

public function beforeSave($object) 
{ 
    if ($object->getId()) { 
     return $this; 
    } 
    if (!$object->hasData('website_id')) { 
     $object->setData('website_id', Mage::app()->getStore()->getWebsiteId()); 
    } 
    return $this; 
} 

這意味着,如果客戶沒有website_id,目前網站的ID分配。

+0

仍然獲得'主要網站'作爲關聯網站 –

+0

我試着將我的'