Created
June 13, 2013 14:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} | |
} |
We should use break instead of continue in above foreach to set first one as default and definitely save it.
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
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/