2013-03-05 82 views
0

我有一項任務,要求我爲沒有設置國家/地區的所有客戶(這是由以前的客戶導入導致的錯誤)設置國家/地區。該國現在可用於默認結算/送貨地址。在下拉菜單中設置用戶國家

但是,在右側,國家/地區下拉列表未被填充。我不能爲我的生活弄清楚如何更新這個。它是否與運輸/帳單地址無關,是不同地址的一部分?

截圖: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())); 
    } 

} 
+0

從你的截圖,我可以找到你的第一個地址(這是不是您的默認地址)你沒有設置國家,讓你找不到的國家在右側設置。接下來,您已將國家/地區設置爲默認地址,但您未點擊地址以檢查是否已設置該地址......只需點擊左側的地址,即可獲取相同的地址。 。這是一個幻覺隊友;-) – Haijerome 2013-03-06 06:37:33

+0

順便說一句,在你的代碼中的一些編輯讓我把它作爲答案發布 – Haijerome 2013-03-06 06:39:51

回答

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; 
相關問題