更新默认客户地址的国家/地区

时间:2013-03-04 23:59:37

标签: magento

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

         }

    }

}

1 个答案:

答案 0 :(得分:2)

你的代码中的拼写错误

此行必须

       $address->setCountryId('US');

而不是

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

你应该使用国家ID而不是美国的国家ID和国家ID。所以使用它并设置正确。

相关问题