1

The scenario : I am trying to register adress information such as phone, company name , etc via ajax using the youama ajax login/registration module. Right now only email, names, password seem to be posted. I am almost there but there must be doing something wrong in this the Model file: Ajaxregister.php

I took example from inchoo recent post: http://inchoo.net/magento/programming-magento/programmaticaly-adding-new-customers-to-the-magento-store/

It says that it should use Mage::getModel('customer/address') for adress data, and not Mage::getModel('customer/customer').

However I tryed and it does not post any phone/company name in the DB.

Here is the code below:

class Youama_Ajaxlogin_Model_Ajaxregister
    extends Youama_Ajaxlogin_Model_Validator
{
    /**
     * Init.
     */
    public function _construct() 
    {
        parent::_construct();

        // Result for Javascript
        $this->_result = '';
        $this->_userId = -1;

        // Terms and conditions has been accepted
        if ($_POST['licence'] == 'ok') {
            $this->setEmail($_POST['email']);

            // If this email is already exist
            if ($this->isEmailExist()) {
                $this->_result .=  'emailisexist,';
            // If this email is not exist yet.
            } else {
                $this->setPassword($_POST['password'], $_POST['passwordsecond']);
                $this->setName($_POST['firstname'], $_POST['lastname']);
                $this->setCompany($_POST['company']);
                $this->setTelephone($_POST['telephone']);
                $this->setNewsletter($_POST['newsletter']);

                // If there are no errors
                if ($this->_result == '') {
                    // Try register user

                    $this->_registerUser();

                    // Try subscribe user to newsletter
                    if ($this->_userNewsletter == true) {
                        $this->_subscribeUser();
                    }
                }
            }
        // Terms and conditions has not been accepted
        } else {
            $this->_result = 'nolicence,';
        }        
    }

    /**
     * Register user via Mage's API.
     */
    protected function _registerUser()
    {
        // Empty customer object
        $customer = Mage::getModel('customer/customer');
        $address = Mage::getModel('customer/address');

        $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
        $address->setWebsiteId(Mage::app()->getWebsite()->getId());

        // Set customer
        $customer->setEmail($this->_userEmail);
        $customer->setPassword($this->_userPassword);
        $customer->setFirstname($this->_userFirstName);
        $customer->setLastname($this->_userLastName);

        $address->setCompany($this->_userCompany);
        $address->setTelephone($this->_userTelephone);


        // Try create customer
        try {
            $customer->save();
            $address->save();
            $customer->setConfirmation(null);
            $customer->save();

            $storeId = $customer->getSendemailStoreId();
            $customer->sendNewAccountEmail('registered', '', $storeId);

            Mage::getSingleton('customer/session')->loginById($customer->getId());

            $this->_userId = $customer->getId();

            $this->_result = 'success';
        // Error by injected HTML/JS
        } catch (Exception $ex) {
            $this->_result .= 'frontendhackerror,';
        }
    }

2 Answers 2

1

Try

$address_data = array (
            'firstname' => 'fname here',
            'lastname' => 'lastname here',
            'street' => '123 test st',
            'city' => 'city here',
            ...
        );

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
// Set customer
$customer->setEmail($this->_userEmail);
$customer->setPassword($this->_userPassword);
...

$address = Mage::getModel('customer/address');
$address->setData($address_data)
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('0')
        ->setSaveInAddressBook('1');

$customer->addAddress($address);

try {
    $customer->save();
}
catch (Exception $ex) {
    $this->_error($ex->getMessage());
    return;
}

You need to assign the customer id to the address

protected function _registerUser()
{
    .....

    // Try create customer
    try {


        $customer->setConfirmation(null);
        $customer->save();
        if($customer->getId()){
           $address->setCustomerId($customer->getId()); 
           $address->save();
        }

        ...

See Programmatically adding new customers to the Magento store

3
  • Thanks but I still have same problem, no data is inserted to database.
    – user263286
    Sep 29, 2014 at 17:27
  • @user263286 ... take a look at my update Sep 29, 2014 at 18:41
  • Thanks R . S , adress-related data gets inserted now. You rock :)
    – user263286
    Sep 29, 2014 at 19:19
0

Issue1: You need set customer id at customer address object for set customer id to address

$customer->save();
/* add this code */
 $address->setCustomerId($customer->getId());
 $address->save();

Also for checking create address validate for save then try below code:

/* use to validate */
$valid = $address->validate();
if (!is_array($valid)) {
$address->save()
}
3
  • Thanks but I still have same problem, no data is inserted to database.
    – user263286
    Sep 29, 2014 at 17:27
  • check $this->_userCompany.. etc give proper value at _registerUser()
    – Amit Bera
    Sep 29, 2014 at 17:33
  • Thanks , I also tried with setData ( @R.S ) now it works
    – user263286
    Sep 29, 2014 at 19:20

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.