asociar cliente a sitios web disponibles

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 como en parent::createPostAction() there's too much abstraction.

¿Hay alguna manera fácil de hacer esto?

preguntado el 12 de febrero de 14 a las 07:02

2 Respuestas

Try naming your select website_id en lugar de website.
La website_id is has a backend model Mage_Customer_Model_Customer_Attribute_Backend_Website that is called when saving the customer entity.
Así que cada vez que llames $customer->save se llama

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.

Respondido 12 Feb 14, 07:02

sigue recibiendo Main Website as the associated website - Mohammad Faisal

I'd tried renaming my <select> a website_id instead it was being added to Main Website. its not added to object being received here. - Mohammad Faisal

you were right but there was missing something... I'd updated my answer - Mohammad Faisal

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

Revisión

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(); y perdida website_id which I had set in there previously.

contestado el 23 de mayo de 17 a las 13:05

This seems to work okay, but the problem is afterwards the customer is forwarded to the account welcome screen and they are not logged in. Name and email info is missing also. If you click on any account links you're booted to login. - Corgalore

@Corgalore: the above code is just supposed to associate a customer to magento's available websites. there is no code for redirecting customer to welcome screen or login. probably something getting wrong which is preventing user from login. check in admin whether customer is created or not? I recommend to use second approach by overriding the controller instead of observer. - Mohammad Faisal

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.