我只花了几个小时的精心工作,将超过2万的客户转移到Magento 1.7的访问数据库。
现在我遇到的问题是该国家没有被所有地址的magento识别或接收。我不想经历另一个导入/导出过程。
我只需要一个简单的SQL脚本就可以将所有COUNTRY字段(在所有地址相关表中)设置为“US”或“United States”(Magento使用的格式)
非常感谢任何帮助。
答案 0 :(得分:1)
您可以使用以下脚本将US设置为所有客户的默认国家/地区
修改强>
ini_set('memory_limit','2048M');
error_reporting(E_ALL | E_STRICT);
require 'app/Mage.php';
$app = Mage::app('default');
$customers = Mage::getResourceModel('customer/customer_collection');
foreach ($customers as $customer) {
// customer object
$customer = Mage::getModel('customer/customer')->load($customer->getId());
$address = Mage::getModel('customer/address');
if ($default_shipping_id = $customer->getDefaultShipping()) {
$address->load($default_shipping_id);
} else {
$address
->setCustomerId($customer->getId())
->setIsDefaultShipping('1')
->setSaveInAddressBook('1')
;
$address_arr = $address->getData();
// country
if ( !isset($address_arr['country_id']) ) {
$address->setCountryId('US');
try {
$address->save();
} catch(Exception $e) {
error_log(json_encode($e->getMessage()));
}
}
}
}
希望这对您有所帮助
请告诉我是否可以为您提供更多帮助。
答案 1 :(得分:1)
我对OP有类似的问题。在我的客户CSV导入中,我有国家名称,而不是2个字母的ISO代码。进口没有抱怨。但是,当客户试图下订单时,可能无法计算运输方法 - 结果是因为国家/地区代码错误。
我用@liyakat写的脚本写了他的优秀答案,并且我做了一些我想分享的增强功能:
getCountryId()代码来自此question on Stackoverflow
ini_set('memory_limit','2048M'); set_time_limit(0); error_reporting(E_ALL | E_STRICT); require 'app/Mage.php'; $app = Mage::app('default'); // Method to Get countryId from CountryName function getCountryId($countryName) { //Most will be here, so save a lookup... if ($countryName == "United Kingdom") { $countryId = "GB"; } else { $countryId = ''; $countryCollection = Mage::getModel('directory/country')->getCollection(); foreach ($countryCollection as $country) { if ($countryName == $country->getName()) { $countryId = $country->getCountryId(); break; } } } $countryCollection = null; return $countryId; } function fixAddress($customer, $address, $isBilling) { if ($isBilling) { $address ->setCustomerId($customer->getId()) ->setIsDefaultBilling('1') ->setSaveInAddressBook('1') ; } else { $address ->setCustomerId($customer->getId()) ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); } $address_arr = $address->getData(); $save = false; if ( isset($address_arr['country_id']) ) { if (strlen($address_arr['country_id']) != 2) { $isoCountry = getCountryId($address_arr['country_id']); $address->setCountryId($isoCountry); $save = true; } } else { $address->setCountryId('GB'); $save = true; } if ($save) { try { $address->save(); } catch(Exception $e) { error_log(json_encode($e->getMessage())); } } } $customers = Mage::getResourceModel('customer/customer_collection'); foreach ($customers as $customer) { // customer object $customer = Mage::getModel('customer/customer')->load($customer->getId()); $address = Mage::getModel('customer/address'); $default_billing_id = $customer->getDefaultBilling(); $address->load($default_billing_id); echo $customer->getEmail() . ' ' . getCountryId($address->getCountryId()) . "
\n"; fixAddress($customer, $address, true); if ($default_shipping_id = $customer->getDefaultShipping()) { $address->load($default_shipping_id); fixAddress($customer, $address, true); } }