0

I can get customer address collection for each customer in front-end checkout process. What I need is I want to add a default address for all the customers in the collection. So in the onepage checkout process when the user sees select billing address dropdown list this default address should be loaded for all the customers. How to achieve this?

Any help will be appreciated. Thank you.

4
  • what code have you got so far? Commented Sep 27, 2013 at 16:19
  • So just to be clear, all customers should see the same address by default. Even the guest customers? Commented Sep 27, 2013 at 16:20
  • @Sander Mangel : yes. All the customers (excluding guests) should see this default address as an option in the onepage checkoout select shipping address.
    – Sukeshini
    Commented Sep 27, 2013 at 16:22
  • Did any of the answers help you resolving your issue? Commented Oct 1, 2013 at 10:45

1 Answer 1

2

When we take a look at template/checkout/onepage/billing.phtml we can see that the dropdown actually comes from the function getAddressesHtmlSelect, this function is located in the block class extended by the billing block being Mage_Checkout_Block_Onepage_Abstract.

Here you can see that a couple of values are required for each address being the database ID coming from the table customer_address_entity and the formatted address value. So if we're gonna add an address to that list it is required to have it exist as an entity in the database. Although I've never used it before you should be able to load an address like so

$address = Mage::getModel('customer/address')->load(14); // 14 is the id of the address

In your case I would suggest extending the function from that class in your own custom extension and making it work something like this. (mind you, this is untested)

class Namespace_Block_Model_Onepage_Abstract extends Mage_Checkout_Block_Onepage_Abstract
{
    public function getAddressesHtmlSelect($type)
    {
        if ($this->isCustomerLoggedIn()) 
        {
            $options = array();

            // this is getting your fixed address
            $address = Mage::getModel('customer/address')->load(14);
            $options[] = array(
                'value' => $address->getId(),
                'label' => $address->format('oneline')
            );

            // resume default working or function
            foreach ($this->getCustomer()->getAddresses() as $address) {
                $options[] = array(
                    'value' => $address->getId(),
                    'label' => $address->format('oneline')
                );
            }

            $addressId = $this->getAddress()->getCustomerAddressId();
            if (empty($addressId)) {
                if ($type=='billing') {
                    $address = $this->getCustomer()->getPrimaryBillingAddress();
                } else {
                    $address = $this->getCustomer()->getPrimaryShippingAddress();
                }
                if ($address) {
                    $addressId = $address->getId();
                }
            }

            $select = $this->getLayout()->createBlock('core/html_select')
                ->setName($type.'_address_id')
                ->setId($type.'-address-select')
                ->setClass('address-select')
                ->setExtraParams('onchange="'.$type.'.newAddress(!this.value)"')
                ->setValue($addressId)
                ->setOptions($options);

            $select->addOption('', Mage::helper('checkout')->__('New Address'));

            return $select->getHtml();
        }
        return '';
    }

}

Now all you need to do is create an address in the database and use it's ID in the load function.

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.