开发者

How do I create custom fields like mobile number in customer registration form without modifying magento core files?

How do I create custom fields like mobile number in customer registration form without modifying magento core files? I tried but the mobile number is not saved in the DB

I override the Mage_Customer_Model_Entity_Setup c开发者_开发技巧lass with Myown_Mage_Customer_Model_Entity_Setup by creating Model/Entity/Setup.php in my modules folder and added the following code to the array in getDefaultEntities

'mobilenumber' => array(
                        'label'        => 'Mobile Number',
                        'visible'    => true,
                        'required'    => true,
                    ),

also my config file contains following code

<models>
        <customer_entity>
            <rewrite>
                  <customer>Myown_Mage_Customer_Model_Entity_Setup</customer>
            </rewrite>
        </customer_entity>
    </models>

Also i have this field in template/customer/form/register.phtml

<input type="text" name="mobilenumber" id="mobilenumber" value="<?php echo $this->htmlEscape($this->getFormData()->getMobilenumber()) ?>" title="<?php echo $this->__('Mobile Number') ?>" class="required-entry input-text" />  

Is there any thing I miss in this configuration? I want to save the mobile number in the DB and retrieve it afterward.


I had to create a similar field (referred_by in this case) for an Enterprise customer, so here's how that went:

First, I created a module that would house all this fun. I added my own entity setup entry for customer, so that I wouldn't have to rely on the default customer one or rewrite any models unnecessarily (as time goes on, you start to conflict with yourself on rewrites). Your entity code seems to work, so it's up to you if you want to refactor that.

Then I added the field to the customer fieldsets, which seems to help Magento understand what data to save in the database:

<global>
    <fieldsets>
        <customer_account>
            <referred_by>
                <create>1</create>
                <update>0</update>
                <to_order>customer_referred_by</to_order>
            </referred_by>
        </customer_account>
    </fieldsets> 
</global>

I then added the field to the customer form, similarly to the way you did above. In checkout, I was forced to add an overriding model to the onepage checkout to save the data during checkout (in my case, only during billing):

class Company_Module_Model_Checkout_Type_Onepage extends Mage_Checkout_Model_Type_Onepage {
    public function saveBilling($data, $customerAddressId) {
        if (isset($data['referred_by'])) {
            // set referred for later use.
            $session    = Mage::getSingleton("customer/session");
            $session->setData("referred_by", $data['referred_by']);
        }

        return parent::saveBilling($data, $customerAddressId);
    }//end saveBilling
}

And:

<global>
    <models>
        <checkout>
            <rewrite>
                <type_onepage>Company_Module_Model_Checkout_Type_Onepage</type_onepage>
            </rewrite>
        </checkout>
    </models>   
</global>

After that, the data was saved correctly in the database. Hurrah!

Hope that helps! Thanks, Joe


Regarding your code:

  1. You don't have to override the Mage_Customer_Model_Entity_Setup because this class is only used to install the customer attributes. The solution is to have your own setup class that inherits from the Mage_Customer_Model_Entity_Setup.

  2. You also need a mysql4-install setup file

To see exactly how to add new attributes to customer see this magento module I've made: https://code.google.com/p/magento-code-snippets/source/browse/#svn/trunk/PWS_ExtCustomerFields

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜