associate customer to available websites

2019-09-01 08:38发布

问题:

I'd created 3 websites in magento admin but they do not have different urls for now.

The thing I want to do is, on the registration page I'd added one more field for website where I'd programmatically created a select box as:

<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>

Now when the user submits this form, based on the chosen website he need to be associated with it.

For this I'd already overriden the Customer/AccountController's createPostAction() but I'm wondering how do I assign this website id as in parent::createPostAction() there's too much abstraction.

Is there any easy way to do this?

回答1:

Try naming your select website_id instead of website.
The website_id is has a backend model Mage_Customer_Model_Customer_Attribute_Backend_Website that is called when saving the customer entity.
So each time you call $customer->save this is called

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

This means that if the customer does not have a website_id, the current website id is assigned.



回答2:

finally found this answer helpful. But I'd made some changes in the observer where I do needed to get the website id selected by the customer.

Here's the code from my observer

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

The event need to be handled is: customer_register_success

Revision

The above code works fine but the problem with the above implementation was, if a user is already registered in the current website then from the current website he will not be able to register in the other website. To solve this problem I'd overridden the Customer/AccountController's createPostAction()

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();
}

If I'm not doing this Mage::register('current_customer', $customer); then the parent createPostAction() will again fetch customer object as $customer = $this->_getCustomer(); and loss website_id which I had set in there previously.



标签: php magento