Skip to content

Instantly share code, notes, and snippets.

@ashfame
Created June 13, 2013 14:40
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ashfame/5774215 to your computer and use it in GitHub Desktop.
Save ashfame/5774215 to your computer and use it in GitHub Desktop.
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
}
}
}
@AnkhEl
Copy link

AnkhEl commented Aug 6, 2021

The problem with this implementation is that it can get any address, be it shipping or billing so long as it is first on the list and assign it as the default shipping address. The correct way is to browse through all the addresses and select the first shipping address put the if within your foreach that will check if the address is a billing address then afterwards break out of the foreach🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment