`
kirenenko04
  • 浏览: 150986 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Magento Add Custom Field to Customer Address

阅读更多

Adding Field to Admin

First we need to create the address attribute. There are two ways to do this one is through your module sql file or you can do it through directly through sql query.

First we will see it through the module.

In your module’s config.xml file, add this in the resource tag.

<global>
    <helpers>
      <myaddress>
        <class>Bysoft_Myaddress_Helper</class>
      </myaddress>
    </helpers>
	<resources>
	  <myaddress_setup>
		<setup>
		  <module>Bysoft_Myaddress</module>
		  <class>Mage_Customer_Model_Entity_Setup</class> <!-- This is the important thing-->
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </myaddress_setup>
	  <myaddress_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </myaddress_write>
	  <myaddress_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </myaddress_read>
	</resources>

The important thing is to have your module’s setup class pointed to Mage_Customer_Model_Entity_Setup. Next in the module’s mysql-install file we will put in the code

<?php
$installer = $this;
$installer->startSetup();

$this->addAttribute('customer_address', 'buyer_mobile', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Buyer Mobile',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'buyer_mobile')
    ->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
    ->save();

$installer->endSetup();

 This will add the relevant database entries. If you want to do it directly i.e through database or phpmyadmin. Then you need to do the below.

eav_attribute table

insert a row in this table with values entity_type_id = 2, attribute_code = govt_id, backend_type = varchar, fontend_input = text, fontend_label = Govt ID No#, is_user_defined = 1, is_required = 1, default = NULL.

eav_entity_attribute table

insert a row in this table with values entity_type_id = 2,attribute_set_id =2, attribute_group_id = 2, attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table)

customer_eav_attribute table

insert a row in this table with values attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table), is_visible = 1 rest all column will take default values

customer_form_attribute table

1. insert row in this table with values form_code = adminhtml_customer_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to show up in the admin

2. insert row in this table with values form_code = customer_address_edit and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to get saved in edit address form and checkout page

3. insert row in this table with values form_code = customer_register_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This is required to save the attribute in the create account page

After doing this step, you should be able to do see your new address attribute in the admin.

Address Format Next we need to change the address display format, this format is used everywhere in magento where ever address is displayed. To change the format, go to System -> Configuration -> Customer Configuration -> Address Template There you will see 5 options, we need to change all. 

Text Add {{depend buyer_mobile}}Buyer Mobile# {{var buyer_mobile}}{{/depend}} where ever you want it. {{depend}} basically checks, if buyer_mobile is not empty.

Text One line Add {{depend buyer_mobile}}Buyer Mobile# {{var buyer_mobile}}{{/depend}} where ever you want it. This format shows up in the checkout page shipping,billing address dropdowns. 

HTML Add {{depend buyer_mobile}}<br/>Buyer Mobile# {{var buyer_mobile}}{{/depend}}. This format is used in many places like Order View, Address Display etc. 
PDF Add {{depend buyer_mobile}}<br/>Buyer Mobile# {{var buyer_mobile}}{{/depend}}|. This format is used in PDF invoices, shipments etc. 

Javascript Template Add <br/>Buyer Mobile#{buyer_mobile}. This is used in admin add/edit address area. After saving these in the configuration, the new address format should be visible. 

 Create Account Address First to show address in the magento create account form we need to make changes to customer\form\register.phtml (magento 1.6- version) or persistent\customer\form\register.phtml (magento 1.6+ version). If your using a module we will overwrite these files and make changes, if your not using a module you can make these changes directly. To overwrite, in your module’s layout xml file

<customer_account_create>
        <reference name='customer_form_register'>
            <action method='setTemplate'><template>address/persistent/customer/form/register.phtml</template></action>
        </reference>
</customer_account_create>

 Next in the phtml file regsiter.phtml created we will copy the default magento register.phtml contents and add our new field. First in the beginning of the file add this code, which will show all the address fields. 

<?php $this->setShowAddressFields(true);?>

 Next we need to add our new field, place this code where you want to show the new field

<li class="fields">
    <div class="field">
        <label for="buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label>
         <div class="input-box">
                 <input type="text" name="buyer_mobile" value="<?php echo $this->htmlEscape($this->getFormData()->getData('buyer_mobile')) ?>" title="<?php echo $this->__('Buyer Mobile') ?>" id="buyer_mobile" class="input-text required-entry" />
          </div>
      </div>
</li>

 After this the new field added should be automatically saved to the database. You can view the new field in the admin and my account area.

Edit Address Next, in the My Account -> Edit Address form we need to add the the new field as well. For this we need to edit the customer\address\edit.phtml file. In the module’s layout xml file put in this code

<customer_address_form>
       <reference name='customer_address_edit'>
           <action method='setTemplate'><template>address/customer/address/edit.phtml</template></action>
       </reference>
</customer_address_form>

 and in the new edit.phtml file put in the form field where every required

            <li class="fields">
                 <div class="field">
                        <label for="buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label>
                        <div class="input-box">
                            <input type="text" name="buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getData('buyer_mobile')) ?>" title="<?php echo $this->__('Buyer Mobile') ?>" id="buyer_mobile" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone'); ?> required-entry" />
                        </div>
                    </div>
           </li>

 Now editing/saving the Govt ID will work.

 

Checkout Billing/Shipping Step

Now, we will see what we need to do for adding the fields in shipping and billing steps of checkout page.

For this need to make changes to the checkout/onepage/billing.phtml and checkout/onepage/shipping.phtml files. To do this in the module’s layout xml file put in this code

<checkout_onepage_index>
        <reference name='checkout.onepage.billing'>
            <action method='setTemplate'><template>address/checkout/onepage/billing.phtml</template></action>
        </reference>
        <reference name='checkout.onepage.shipping'>
            <action method='setTemplate'><template>address/checkout/onepage/shipping.phtml</template></action>
        </reference>
</checkout_onepage_index>

 Next in the shipping.phtml and billing.phtml files need to add the field. Add this code in billing.phtml file

<li class="buyer_mobile">
    <div class="buyer_mobile_field">
         <div class="left_label"><label for="billing:buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label></div>
         <div class="buyer_mobile_content">
               <input type="text" title="<?php echo $this->__('Buyer Mobile') ?>" name="billing[buyer_mobile]" id="billing:buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getBuyerMobile()) ?>" class="input-text validate-one-required-by-group required-group-1 validate-digits  validate-length minimum-length-11 maximum-length-11" />
          </div>
     </div>
</li>

 and add this code in shipping.phtml file

<li class="buyer_mobile">
    <div class="buyer_mobile_field">
         <div class="left_label"><label for="billing:buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label></div>
         <div class="buyer_mobile_content">
               <input type="text" title="<?php echo $this->__('Buyer Mobile') ?>" name="billing[buyer_mobile]" id="billing:buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getBuyerMobile()) ?>" class="input-text validate-one-required-by-group required-group-1 validate-digits  validate-length minimum-length-11 maximum-length-11" />
          </div>
     </div>
</li>

 Next, we need to add this xml code to our module config.xml file 

    <fieldsets>
        <sales_convert_quote_address>
            <buyer_mobile>
                <to_order_address>*</to_order_address>
                <to_customer_address>*</to_customer_address>
            </buyer_mobile>
        </sales_convert_quote_address>
        <customer_address>
            <buyer_mobile>
                <to_quote_address>*</to_quote_address>
            </buyer_mobile>
        </customer_address>
    </fieldsets>
  </global>

 Next we need to add govt_id column in the tables sales_flat_order_address, sales_flat_quote_address. To do this in your module’s sql file

$tablequote = $this->getTable('sales/quote_address');
$installer->run("
ALTER TABLE  $tablequote ADD  `buyer_mobile` varchar(255) NOT NULL
");
 
$tablequote = $this->getTable('sales/order_address');
$installer->run("
ALTER TABLE  $tablequote ADD  `buyer_mobile` varchar(255) NOT NULL
");

 Now, if a new address is created from the checkout step the new field will be added to the database and show up the order view.

These are all the places where the address shows up. The new field we have added will automatically show up in emails, pdfs etc.

分享到:
评论

相关推荐

    wp_custom_menu-1.2.5.rar_magento_magento custom menu

    标题中的"wp_custom_menu-1.2.5.rar"指的是一个专门为Magento设计的定制菜单插件的版本号为1.2.5的压缩包文件。这个插件允许用户根据自己的需求自定义商店的导航菜单,以提供更好的用户体验和更高效的网站导航。 在...

    magento2custom

    Magento 2 Custom 是一个专为 Magento 2 平台定制的项目,可能包含了对 Magento 2 默认功能的扩展、自定义模块的开发或是特定业务需求的实现。在深入讨论这个项目之前,首先需要理解 Magento 2 是一个强大的开源电子...

    magento2-customer-address-autocomplete:Magento 2客户地址自动完成

    请通过运行命令行进行安装: composer需要php-cuong / magento2-module-core 安装PHPCuong_CustomerAddressAutocomplete模块: 作曲家需要php-cuong / magento2-customer-address-autocomplete php bin / ...

    magento-custom-address:一个简单的模块,可将“邻居”和“互补”字段添加到Magento商店中的客户地址

    这个名为“magento-custom-address”的模块,旨在扩展Magento的默认地址管理功能,为客户提供更详细的住址信息选项。 首先,我们来看看“邻居”和“互补”这两个字段。在商业环境中,尤其是零售业,精确的送货地址...

    The Definitive Guide to Magento.pdf

    ### Magento 全面指南知识点概览 #### 一、引言 《Magento全面指南》是电子商务平台Magento的权威参考书籍,由Adam McCombs与Robert Banh共同编写。本书不仅适用于初学者,对于有一定经验的开发者和技术人员也同样...

    The Definitive Guide to Magento (Apress出品 Magento权威指南)

    ### Magento权威指南 #### 书籍概述 《Magento权威指南》是由Adam McCombs与Robert Banh共同编著的一本深入探讨Magento电商平台的技术手册。该书由Apress出版社于2009年出版发行,旨在为读者提供一个全面、系统的...

    Ajax-Magento-ajax-add-to-cart.zip

    Ajax-Magento-ajax-add-to-cart.zip,[模块magento 1]magento ajax添加到购物车-ajoter vos produits au panier en ajax/感谢ajax将您的产品添加到购物车,ajax代表异步javascript和xml。它是多种web技术的集合,包括...

    magento-adminhtml-customer-address-allow-deselect:存储库包含模块的源文件,允许用户在新客户创建过程中编辑地址字段时取消选择单选按钮

    在这个特定的场景中,我们关注的是“magento-adminhtml-customer-address-allow-deselect”模块,它旨在改进新客户创建过程中地址编辑的用户体验。 这个模块的核心功能是允许用户在输入地址信息时取消选择单选按钮...

    Magento 2 Beginners Guide

    You’ll start by getting a general understanding of what Magento is, why and how you should use it, and whether it is possible and feasible to migrate from an old web store to Magento 2. As you work ...

    magentoCustomerAddressSql:查询 magento 的客户名称地址并输出为 CSV 的 sql

    magentoCustomerAddressSql 用于查询 magento 的... 仅使用地址相关表转储数据库: mysqldump --opt -h IP_ADDRESS -u DATABASE_NAME -pPASSWORD --lock-tables=false peaches_prod customer_address_entity_int custo

    Magento 添加后台管理 addColumn

    "Magento添加后台管理addColumn"这个主题主要涉及的是如何在Magento的后台管理面板自定义添加新的数据列,以展示更多店铺运营的相关信息。这通常涉及到对Magento的MVC(Model-View-Controller)架构的理解,以及对...

    Magento 2 Cookbook

    compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the command-line interface for an experienced webmaster. Chapter 2, Magento 2 System ...

    magento-custom-stock-status:Magento

    magento-custom-stock-status Magento:自定义库存状态兼容:1.5、1.6、1.6.1、1.6.2.0、1.7、1.8、1.8.1、1.9、1.13、1.14 注意:安装前检查社区版本视图文件

    magento快速复制网站_magento_magento快速复制站_

    这里,`/path/to/source/magento/`是源站点的Magento根目录,`/path/to/destination/`是目标服务器的目录。 在新服务器上安装MySQL,并导入先前备份的数据库: ```bash mysql -u [new_username] -p[new_password] ...

    magento2-turnto-socialcommerce:Magento 2的TurnTo Networks社交商务插件

    概述该存储库包含将Magento 2与TurnTo的Social Commerce服务连接的Magento 2扩展。 与Magento社区和企业版2.1.x-2.3.x兼容。安装说明使用Composer安装(推荐) 在您的Magento根安装目录中运行以下命令: composer ...

    magento商城数据库

    这会创建 Magento 需要的所有表,包括 `catalog_product_entity`(产品信息)、`sales_flat_order`(订单数据)、`customer_entity`(客户信息)等。 4. **加载样本数据**:除了基础架构,这个包可能还包含一些示例...

    magento结构和原理

    magento结构和原理是magento框架的核心组成部分,了解magento的结构和原理是开发magento模块和主题的基础。本文将详细介绍magento的文件目录结构、URL路由与分发器、模板调用对应的JS、CSS、图片、重写核心模块等...

    Magento 自定义后台menu Insert dynamical menu in Magento’s Admin

    最后,为了让Magento知道你的模块存在并启用这个功能,记得在`app/etc/modules/Custom_Module.xml`中声明模块,并设置`active`为`true`。 ```xml &lt;Custom_Module&gt; &lt;active&gt;true &lt;codePool&gt;local &lt;/Custom_...

    [Magento] Magento 扩展开发入门教程 (英文版)

    Get a hands-on introduction to custom shipping and payment methods ☆ 出版信息:☆ [作者信息] Branko Ajzele [出版机构] Packt Publishing [出版日期] 2013年09月25日 [图书页数] 128页 [图书语言] 英语...

Global site tag (gtag.js) - Google Analytics