4

How can I show the customer information from a Shop-User (logged in) at my frontend template. I have some Form-PHTML-files and a customer can fill out the form etc.

I want to show "his" user data (Name, Forname, Street, City, ..) at the top of the form-template.

Who can help me?

5 Answers 5

7

I found this.

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerData = Mage::getSingleton('customer/session')->getCustomer();
    $mydatas['email'] = $customerData->getEmail();
    $mydatas['vatid'] = $customerData->getTaxvat();

    $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); //oder getDefaultShipping
    if ($customerAddressId) {
        $address = Mage::getModel('customer/address')->load($customerAddressId);
        $mydatas['name'] = $address->getFirstname().' '.$address->getLastname();
        $mydatas['company'] = $address->getCompany();
        $mydatas['zip'] = $address->getPostcode();
        $mydatas['city'] = $address->getCity();
        $street = $address->getStreet();
        $mydatas['street'] = $street[0];
        $mydatas['telephone'] = $address->getTelephone();
        $mydatas['fax'] = $address->getFax();
        $mydatas['country'] = $address->getCountry();
    }
}

and it worked for me.

3

This code depends on the fact that the user has a default billing address filled in. Only the name is standard present in an account

<?php

if (Mage::getSingleton('customer/session')->isLoggedIn())
{
    $customer = Mage::getSingleton('customer/session')->getCustomer();

    $address_id = $customer->getDefaultBilling();
    if ((int)$address_id){
       $address = Mage::getModel('customer/address')->load($address_id);

       /*
       * Insert the output here 
       * for example:
       * echo $customer->getName();
       * echo $address->getCity();
       */
    }
}
0
1

For logged in customer address information, use the below code. It will work

<?php
if (Mage::getSingleton('customer/session')->isLoggedIn())
{
    $addressIs = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
    if ($addressIs) 
    {
        $address = Mage::getModel('customer/address')->load($addressIs);
        /* echo "<pre>";
        print_r($address);die(); */

        $mydatas['name'] = $address->getName();
        $mydatas['company'] = $address->getCompany();
        $mydatas['zip'] = $address->getPostcode();
        $mydatas['city'] = $address->getCity();
        $street = $address->getStreet();
        $mydatas['street'] = $street[0].', '.$street[1];
        $mydatas['telephone'] = $address->getTelephone();
        $mydatas['country'] = $address->getCountry();
    }
} ?>
0

SPLITTING CUSTOMER ADDRESS FILED: street 0 and street 1

$customerAddress = Mage::getModel('customer/address')->load($customerAddressId);

$street = $customerAddress->getStreet();
$street0 = strtolower($street[0]);
$street1 = strtolower($street[1]);

// set "street0" as customer address...
if($street0 == $street1){
    $customerAddress->setData('street',$street0);
}
0

Another way to HTML the address:

public function getCustomerAddressHtml()
{
    $address = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBillingAddress();
    return $address->format('html');
}

Reference the file config.xml in app/code/core/Mage/Customer/etc, you can format the address in 'text', 'oneline', 'pdf', 'js_tempalte'. You can also create a customized format in your own config:

<global>    
    <customer>
        <address>
            <formats>
                <exthtml translate="title" module="extendedcustomer">
                    <title>EXTHTML</title>
                    <htmlEscape>true</htmlEscape>
                </exthtml>
            </formats>
        </address>
    </customer>             
</global>

<default> 
    <customer>
        <address_templates>
            <exthtml><![CDATA[{{if street1}}{{var street1}}<br />{{/if}}
{{depend street2}}{{var street2}}<br />{{/depend}}
{{depend street3}}{{var street3}}<br />{{/depend}}
{{depend street4}}{{var street4}}<br />{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br/>
{{depend telephone}}T: {{var telephone}}{{/depend}}
{{depend fax}}<br/>F: {{var fax}}{{/depend}}]]></exthtml>
        </address_templates>
    </customer>
</default>

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.