【问题标题】:Setting country for all customers in Magento在 Magento 中为所有客户设置国家
【发布时间】:2014-06-24 07:11:07
【问题描述】:

我刚刚经历了数小时的细致工作,将超过 20,000 个用于访问数据库的客户转移到 Magento 1.7 中。

现在我遇到的问题是,magento 没有为所有地址识别或接收该国家/地区。我不想经历另一个导入/导出过程。

我只需要一个简单的 SQL 脚本来将所有 COUNTRY 字段(在所有与地址相关的表中)设置为“US”或“United States”(无论 Magento 使用哪种格式)

任何帮助将不胜感激。

【问题讨论】:

  • 请添加这一行 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);在 $app = Mage::app('default'); 之前的@liyakat 的答案中代码,然后再次执行此脚本
  • 试过了,还是一样。国家/地区字段必须从“美国”更改为“美国”,但没有发生......
  • 好的,所以我找到了一个简单的方法来做到这一点。刚刚在名为“mage_customer_address_entity_varchar”的表中从 myPHPadmin 进行了查找/替换(我的安装有表的 mage_ 前缀。)无论如何,这很容易和快速地完成了这个技巧。谢谢大家的意见!

标签: sql magento


【解决方案1】:

您可以使用以下脚本将美国设置为所有客户的默认国家/地区

编辑

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

         }

    }

}

希望对你有帮助

如果我能帮助你更多,请告诉我。

【讨论】:

  • 感谢您的快速帮助。你在哪里运行这个脚本?我假设它必须在已经建立数据库连接的 php 文件中?我知道这可能是一个愚蠢的问题,但我的大脑现在已经炸了!
  • 您可以使用我的更新代码在您的客户地址中设置国家/地区,只需在 app 文件夹旁边运行此脚本或在任何位置加载 mage.php 以运行它。如果这可以解决您的问题,请不要忘记接受我的回答
  • 我在根文件夹的一个 php 文件中运行它,它执行时没有任何错误(几秒钟后返回一个空白页,但我仍然遇到同样的问题。有没有办法做这直接来自 SQL?这会改变所有客户通讯录中的所有地址吗?
  • 我还发现来自进口客户的国家/地区在表格字段中显示“美国”,所以我假设它必须将 country-id 字段(如果已经存在)替换为“美国”的正确标记。
  • 是的,你是对的,这个值存储在customer_address_entity_varchar你可以直接更新
【解决方案2】:

我遇到了与 OP 类似的问题。在我为客户导入的 CSV 文件中,我有国家名称,而不是 2 个字母的 ISO 代码。进口没有抱怨。但是,当客户尝试下订单时,可能会计算出不送货方式 - 结果证明是因为国家/地区代码错误。

我采用了@liyakat 在他的出色回答中所写的脚本,并做了一些改进,我想分享:

  • 它将修复运输和帐单地址
  • 如果 countryId 中有一个值,它将查找该国家/地区名称中的代码...但是...
  • 如果 countryId 已经包含两个字母的代码 别管它了
  • 默认为 GB“英国”,因为我是 英国人
  • 它将 time_limit 设置为零 - 没有限制,我不知道是否 这会影响命令行 PHP 与否,但我不会冒险 中途停下。
  • 打印客户的电子邮件 加工。它很慢,所以这让您了解 进展。

getCountryId() 代码来自这个question on Stackoverflow

ini_set('memory_limit','2048M'); 设置时间限制(0); 错误报告(E_ALL | E_STRICT); 需要'app/Mage.php'; $app = 法师::app('default'); // 从 CountryName 中获取 countryId 的方法 函数 getCountryId($countryName) { //大多数都会在这里,所以保存查找... if ($countryName == "英国") { $countryId = "GB"; } 别的 { $countryId = ''; $countryCollection = Mage::getModel('目录/国家')->getCollection(); foreach ($countryCollection 作为 $country) { if ($countryName == $country->getName()) { $countryId = $country->getCountryId(); 休息; } } } $countryCollection = null; 返回 $countryId; } 函数 fixAddress($customer, $address, $isBilling) { if ($isBilling) { $地址 ->setCustomerId($customer->getId()) ->setIsDefaultBilling('1') ->setSaveInAddressBook('1') ; } 别的 { $地址 ->setCustomerId($customer->getId()) ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); } $address_arr = $address->getData(); $保存=假; if ( isset($address_arr['country_id']) ) { if (strlen($address_arr['country_id']) != 2) { $isoCountry = getCountryId($address_arr['country_id']); $address->setCountryId($isoCountry); $保存=真; } } 别的 { $address->setCountryId('GB'); $保存=真; } 如果($保存){ 尝试 { $地址->保存(); } 捕捉(异常 $e){ error_log(json_encode($e->getMessage())); } } } $customers = Mage::getResourceModel('customer/customer_collection'); foreach ($customers 作为 $customer) { // 客户对象 $customer = Mage::getModel('customer/customer')->load($customer->getId()); $address = Mage::getModel('客户/地址'); $default_billing_id = $customer->getDefaultBilling(); $address->load($default_billing_id); 回声 $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); } }

【讨论】:

  • 非常适合我。我无法让它在浏览器中运行,但它在命令行中运行起来就像一台润滑良好的机器。谢谢!
猜你喜欢
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2014-05-08
  • 1970-01-01
  • 2018-09-15
  • 2016-02-14
  • 1970-01-01
相关资源
最近更新 更多