Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programmatically set default billing/shipping address of customers if they are not set in magento
<?php
require_once ("app/Mage.php");
umask(0);
Mage::app("default");
$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
foreach ($collection as $customer) {
$customerObj = Mage::getModel('customer/customer')->load( $customer->getId() );
if ( ! $customerObj->getDefaultBillingAddress() ) {
foreach ($customerObj->getAddresses() as $address) {
$address->setIsDefaultBilling(true);
continue; // we only want to set first address of the customer as default billing address
}
}
if ( ! $customerObj->getDefaultShippingAddress() ) {
foreach ($customerObj->getAddresses() as $address) {
$address->setIsDefaultShipping(true);
continue; // we only want to set first address of the customer as default shipping address
}
}
}
@jamgit

This comment has been minimized.

Copy link

commented Apr 9, 2014

Hello,
Thanks for sharing this code. Created this file and ran it at the root of the website and it did not change addresses to default. This was Magento 1.7.x. Do you know why it would not set the default address?

@will-b

This comment has been minimized.

Copy link

commented Jul 10, 2014

The address needs to be saved after isDefaultXXX has been set

$address->setIsDefaultBilling(true);
$address->save();
@rafaesteller

This comment has been minimized.

Copy link

commented Aug 25, 2016

In 1.9 $address->setIsDefaultShipping(true); didn't work.
Changing to $address->setIsDefaultShipping('1'); worked.
Thanks anyway!

@petwork84

This comment has been minimized.

Copy link

commented May 6, 2017

How dynamically to switch on/off default billing and shipping address on customer side based on dynamic_shipping/dynamic_billing parameter?

I am using Magento 1.9.2.4. What change do I need to make in magento/app/code/core/Mage/Customer/controllers/AddressController.php in formPostAction() in case when $address->getIsDefaultBilling() or $address->getIsDefaultShipping() are 0, to unset the defaultBilling Address or defaultShipping Address values to null, because I don't want to show their defaults representatives on frontend:
http://magento.com/index.php/customer/address/index/

@babar-ali

This comment has been minimized.

Copy link

commented Jul 4, 2017

We should use break instead of continue in above foreach to set first one as default and definitely save it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.