在下拉列表中设置用户所在国家

时间:2013-03-05 23:36:20

标签: magento

我的任务要求我为所有未设置国家/地区的客户设置国家/地区(这是以前的客户导入导致的错误),用于发货/结算地址。该国家/地区现在可用于默认结算/送货地址。

然而,在右侧,国家下拉列表没有填充。我不能为我的生活弄清楚如何更新这个。它是与运送/帐单地址无关的其他地址的一部分吗?

屏幕截图:http://cl.ly/image/2G1s261v2a2V

这是我用来批量更新默认国家/地区的代码。

$customers = Mage::getResourceModel('customer/customer_collection');

foreach ($customers as $customer) {

    // get individual customer
    $customer = Mage::getModel('customer/customer')->load($customer->getId());

    // now get their address
    $address = Mage::getModel('customer/address');

    $address
        ->setCustomerId($customer->getId())
    ;

    $address_data = $address->getData();

    if ( !isset($address_data['country_id']) || $address_data['country_id'] == 'United States' ) {
        $address->setCountryId('US');
    }

    try {
        $address->save();
    } catch(Exception $e) {
        Mage::log(json_encode($e->getMessage()));
    }

}

1 个答案:

答案 0 :(得分:1)

    $default_country_id = "US";

   /*Loads Customer Collection*/ 
   $customers = Mage::getResourceModel('customer/customer_collection');
   foreach($customers as $customer):
      $customerDetails = Mage::getModel('customer/customer')->load($customer->getId());
      /*Loads customer addresses*/ 
      foreach ($customerDetails->getAddresses() as $address): 
        /*If country is not set then it will set the default country to the address*/
        if(!$address->getData('country_id')){
            $address->setCountryId($default_country_id)->save();
        }
      endforeach;
   endforeach;