2013-03-04 59 views
1

ANyone有更新客戶默認運費和帳單地址的示例腳本?我需要循環所有客戶,並在沒有指定國家的情況下將該國設爲默認「美國」。更新默認客戶地址的國家

我試過以下,沒有運氣。有什麼想法嗎?

$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('United States'); 

      try { 
       $address->save(); 
       fwrite(STDOUT, '++ COUNTRY UPDATED FOR ' . $customer->getId() . "\n"); 
      } catch(Exception $e) { 
       error_log(json_encode($e->getMessage())); 
      } 

     } 

    } 

} 

回答

2

其代碼中的一個錯字

該行必須

 $address->setCountryId('US'); 

,而不是

 $address->setCountryId('United States'); 

,您應該使用國家ID,而不是名稱和國家ID爲美國是美國..所以使用它,並設置它的權利。

相關問題