3

I want to create a new field for every address (say 'new_address_code') in the backend which shouldn't be visible in the frontend. When ever a customer adds a new address through address book or in checkout, the field's value should be generated automatically based on the 'customername'._.'serialNumberOfHisAddresses'. Could any one guide me how to do this. Thanks in advance..

6
  • May I ask you why you want to do that?
    – Simon
    Commented Aug 7, 2014 at 6:54
  • @Simon I need the field as I want to give a manual id for every address created by a customer. Commented Aug 7, 2014 at 7:01
  • You could just use the entity_id of the Mage_Customer_Model_Address instances. This is a unique ID for each address created by the customer. No need for a new address field I think...
    – Simon
    Commented Aug 7, 2014 at 7:14
  • @Simon Its a requirement in the site I am working on. Could u please suggest a way. Commented Aug 7, 2014 at 7:23
  • Can admin view / edit this field? Can customer view/ edit the field in the account and checkout?
    – Amasty
    Commented Aug 7, 2014 at 9:25

1 Answer 1

2

user3066006,I have create an extension which do this

Create an extension: app\code\local\Tdg\Check\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
        <config>
        <modules>
            <Tdg_Check>
                <version>1.0.0</version>
            </Tdg_Check>
        </modules>
        <admin>
            <fieldsets>
                <customer_dataflow>
                    <new_address_code><billing>1</billing><shipping>1</shipping></new_address_code>
                </customer_dataflow>
            </fieldsets>
        </admin>
        <global>
            <models>
                <check>
                    <class>Tdg_Check_Model</class>
                </check>
            </models>
            <resources>
                <check_setup>
                    <setup>
                        <module>Tdg_Check</module>
                        <class>Mage_Customer_Model_Entity_Setup</class>

                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </check_setup>
                <check_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </check_write>
                <check_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </check_read>
            </resources>
            <fieldsets>
                <sales_copy_order_billing_address>
                    <new_address_code><to_order>*</to_order></new_address_code>
                </sales_copy_order_billing_address>
                <sales_copy_order_shipping_address>
                    <new_address_code><to_order>*</to_order></new_address_code>
                </sales_copy_order_shipping_address>
                <sales_convert_quote_address>
                    <new_address_code><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></new_address_code>
                </sales_convert_quote_address>
                <sales_convert_order_address>
                    <new_address_code><to_quote_address>*</to_quote_address></new_address_code>
                </sales_convert_order_address>
                <customer_address>
                     <new_address_code><to_quote_address>*</to_quote_address></new_address_code>
                </customer_address>
                <checkout_onepage_billing>
                    <new_address_code><to_customer>*</to_customer></new_address_code>
                </checkout_onepage_billing>
            </fieldsets>
            <!-- call of  event basic of customer_save_after -->
            <events>
                <customer_address_save_before>
                    <observers>
                        <myautoincrement>
                            <class>check/observer</class>
                            <method>mymethod</method>
                        </myautoincrement>
                    </observers>
                </customer_address_save_before>
                <customer_address_save_after>
                    <observers>
                        <myautoincrement>
                            <class>check/observer</class>
                            <method>aftermymethod</method>
                        </myautoincrement>
                    </observers>
                </customer_address_save_after>
                <!-- event on checkout -->
            <checkout_submit_all_after>
                <observers>
                    <auto_set_custom_field>
                        <class>check/observer</class>
                        <method>autosetBillingShipfield</method>
                    </auto_set_custom_field>
                </observers>
            </checkout_submit_all_after>


            </events>

        </global>
    </config>

Observer.php(app\code\local\Tdg\Check\Model) is

    <?php
    class Tdg_Check_Model_Observer{
    public  function mymethod($observer)
        {
            /** @var $customerAddress Mage_Customer_Model_Address */
            try{
            $customerAddress = $observer->getCustomerAddress();
            $customer = $customerAddress->getCustomer();
        if (Mage::registry('my_is_new_address')) {
            Mage::unregister('my_is_new_address');
        }


             if ($customerAddress->getId()) {

                 //old address
             }else{
                        Mage::register('my_is_new_address', 'new_address');

                 //new address 
             }
            }catch(Exception $e){
            //Mage::log($e);
            Mage::log(print_r($e->getMessage(), 1), null, 'logfile1.log');
            }
        }
    public  function aftermymethod($observer)
        {
            /** @var $customerAddress Mage_Customer_Model_Address */
            try{
            $customerAddress = $observer->getCustomerAddress();
            $id= (string)$customerAddress->getId();
            $customer = $customerAddress->getCustomer();

            $ObcustomerAddress = $observer->getCustomerAddress();
             if ($customerAddress->getId()) {
            if(Mage::registry('my_is_new_address')){
            if(Mage::registry('my_is_new_address')=='new_address'):

                $customerAddress->setData('new_address_code','aftertesting-'.$customerAddress->getData('firstname').'=myid'.$id);
                 $customerAddress->getResource()->saveAttribute($ObcustomerAddress, 'new_address_code');
endif;
}

                 //old address
             }else{

                $customerAddress->setData('new_address_code','after-testingnew-'.$customerAddress->getData('firstname'));
                 $customerAddress->getResource()->saveAttribute($ObcustomerAddress, 'new_address_code');


                 //new address 
             }
            }catch(Exception $e){
            //Mage::log($e);
            Mage::log(print_r($e->getMessage(), 1), null, 'logfile1.log');
            }
        }
        public function autosetBillingShipfield($Observer){
    $Order=$Observer->getEvent()->getOrder();
    $Quote=$Observer->getEvent()->getQuote();
    if ($Quote->getBillingAddress()->getSaveInAddressBook()&& !is_null($Order->getBillingAddress()->getCustomerAddressId())) {
                 $address = Mage::getModel('customer/address')->load((int)$Order->getBillingAddress()->getCustomerAddressId());
        $SaleaOrderAdd=Mage::getModel('sales/order_address')->load($Order->getBillingAddress()->getId());       
        $SaleaOrderAdd->setData('new_address_code','after-testingnew-'.$Order->getBillingAddress()->getData('firstname').'--'.$address->getId());
        $SaleaOrderAdd->getResource()->saveAttribute($SaleaOrderAdd, 'new_address_code');


    }
    if ($Quote->getShippingAddress()->getSaveInAddressBook()&& !is_null($Order->getShippingAddress()->getCustomerAddressId())) {
                 $address = Mage::getModel('customer/address')->load((int)$Order->getShippingAddress()->getCustomerAddressId());
        $SaleaShipOrderAdd=Mage::getModel('sales/order_address')->load($Order->getShippingAddress()->getId());      

        $SaleaShipOrderAdd->setData('new_address_code','after-testingnew-'.$Order->getShippingAddress()->getDataFirstname().'--'.$address->getId());
        $SaleaShipOrderAdd->getResource()->saveAttribute($SaleaShipOrderAdd, 'new_address_code');

    }

}

    }

And installer mysql4-install-1.0.0.php ( Tdg\Check\sql\check_setup)

<?php
    /* @var $installer Mage_Customer_Model_Entity_Setup */
    $installer = $this;
    $installer->startSetup();
    /* @var $addressHelper Mage_Customer_Helper_Address */
$this->addAttribute('customer_address', 'new_address_code', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'New address code ',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'new_address_code')
    ->setData('used_in_forms', array('adminhtml_customer_address'))
    ->save();
    $installer->run("
        ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `new_address_code` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
         ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `new_address_code` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
        ");
    $installer->endSetup();
2
  • Thanks for sparing your time and answering it.. But, I have a small problem. The value for 'new_address_code' is not storing in the sales_flat_order_addresses table. Commented Aug 7, 2014 at 12:37
  • hi,can tell you want save whenever customer will creating new address in checkout?
    – Amit Bera
    Commented Aug 9, 2014 at 16:09

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.