2

When a user registers with us then Magento automatically save customer first name and last name into shipping and billing address.

Address fields are not displaying at the registration page and I don't want to save this information into the shipping and billing address.

  • Please specifiy magenot version – Amit Bera Jan 25 '18 at 7:22
  • @amit it's 1.9.36 – Rakesh Kumar Jan 25 '18 at 7:24
0

What you're looking for is mainly taking place at app/design/frontend/base/default/template/customer/widget/name.phtml. This "widget" will output a whole block containing prefix, first name, middle name, last name and suffix. It does so by adding values to the input field's value attribute, i.e. value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" in case of the first name input field.

This template is called in various places where there is an address input needed. For example at:

  • My Account create/edit a new address
  • Checkout create billing/edit address
  • Checkout create shipping/edit address

(You can identify the areas where this template is in use for example by viewing the frontend with active template path hints.)

As you can see, the same template is used for new and already existing address records. Since this is the case, it is not as easy as just removing the value attribute from the <input> field as this would not only remove the name from generally empty forms (as this seems to be what you want to achieve) but also from already filled up and saved forms.


Knowing this should allow us to address the issue in multiple different ways.

One for sure would be to simply replace the names-block in all templates that inherit the same. As an example you could (not saying you should, though) modify app/design/frontend/{theme}/{theme}/template/customer/address/edit.phtml, find

<li class="fields">
    <?php echo $this->getNameBlockHtml() ?>
</li>

and replace it with something like this based on the initial name widget template:

<li class="fields">
    <div class="field name-firstname">
        <label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->__('Firstname') ?></label>
        <div class="input-box">
        <input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo Mage::helper('core')->quoteEscape($this->__('firstname'))?>" value="<?php echo $this->getAddress()->getId() ? $this->escapeHtml($this->getAddress()->getFirstname()) : '' ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('firstname')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" />
        </div>
    </div>
    <div class="field name-lastname">
        <label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->__('Lastname') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('lasstname')?>" name="<?php echo Mage::helper('core')->quoteEscape($this->__('lastname'))?>" value="<?php echo $this->getAddress()->getId() ? $this->escapeHtml($this->getAddress()->getLastname()) : '' ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('lastname')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" />
        </div>
    </div>
</li>

This might be the simplest approach, however, the concept would need to be applied on every single template where there are address fields in use (see list above).


Some better way of addressing your need can be an override of of the method that is populating the methods that are used to call the widget block. In case of the my account address create/edit form this would be Mage_Customer_Block_Address_Edit::getNameBlockHtml().

Originally this method here looks like this:

/**
 * Generate name block html
 *
 * @return string
 */
public function getNameBlockHtml()
{
    $nameBlock = $this->getLayout()
        ->createBlock('customer/widget_name');
        ->setObject($this->getAddress());

    return $nameBlock->toHtml();
}

So, what you can do would be something like this:

/**
 * Generate name block html
 *
 * @return string
 */
public function getNameBlockHtml()
{
    $nameBlock = $this->getLayout()
        ->createBlock('customer/widget_name');

    if($this->getAddress()->getId()) {
        $nameBlock->setObject($this->getAddress());
    }
    else {
        $addressObject = new Varien_Object();
        $addressObject->setFirstname(null);
        $addressObject->setLastname(null);

        $nameBlock->setObject($addressObject);
    }

    return $nameBlock->toHtml();
}

Basically what we're doing here is, we check whether there is an existing address record (Magento does the same when setting the forms title to either "Create a new address" or "Edit an address"). If there is none, we create a new, empty object and set empty values for the same.

In terms of what Magento does for example when you create a new address in checkout based on an already existing record this might one approach that fits in the best - however, again this would need to be done on all blocks that are used to call the name-block in return.


Third and last idea for the moment would be applied on the name widget template again (app/design/frontend/base/default/template/customer/widget/name.phtml):

$this->getObject()->getEntityId()

can be used to identify whether we are dealing with a new address form or an edit form (similar to what we just did above in Mage_Customer_Block_Address_Edit::getNameBlockHtml()).

So, basically you could put an if condition into the value attribute of the input field (like what was done in the very first approach):

So, rather than this

<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('firstname')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />

it could like that:

<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->getObject()->getEntityId() ? $this->escapeHtml($this->getObject()->getFirstname()) : ''?>" title="<?php echo Mage::helper('core')->quoteEscape($this->getStoreLabel('firstname')) ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />

Note that this would affect all name-widget calls throughout the shop! This can and can not be desired likewise.

Another note: obviously none of these changes should be made in app/code/core nor in app/design/frontend/base/default - in case there are question about creating custom modules you may want to search on this topic here on stackexchange or the internet in general as there are tons of posts related to this topic.

  • Thank you .I had done according your step within the files but not getting any good luck :( – Rakesh Kumar Feb 1 '18 at 9:44
  • @RakeshKumar Please share the code and filenames that you've modified as an edit in your question – Christoph Farnleitner Feb 1 '18 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.