Magento - 客户导入缓慢 - 寻找优化脚本

时间:2015-02-27 10:07:45

标签: magento optimization

我有一个cron,每天导入/更新3000个客户。然而,这需要大约5个小时......我需要每天都这样做,所以我试图优化代码以提高速度。

这是我目前的代码:

// ~3000 customers
foreach($results as $customerRecord) {
    // customer general data:
    $customer = $customerModel;
    $customer->setWebsiteId($websiteId);
    $email = $customerRecord['cc_email'];

    $password = $email; // set password same as email
    $customer->loadByEmail($email);

    $customer->setEmail($email);
    $nameAr = explode(" ", $customerRecord['cc_name']);
    $customer->setFirstname($nameAr[0]);
    $customer->setLastname($nameAr[1]);
    $customer->setPassword($password);
    // custom attributes
    $customer->setCustomerFromOw("Yes");
    $customer->setCdOwAccount($customerRecord['cd_ow_account']);
    $customer->setDiscountStructureName($customerRecord['discount_structure_name']);
    $customer->setPriceListName($customerRecord['price_list_name']);
    $customer->setCcdCreditLimit($customerRecord['ccd_credit_limit']);
    $customer->setCcdtBalance($customerRecord['ccdt_balance']);
    $customer->setAdditionalPriceLists($customerRecord['additional_price_lists']);
    $customer->setOwDeliveryRates($customerRecord['ow_delivery_rates']);
    // Netzarbeiter CustomerActivation - ensure set to true
    $customer->setCustomerActivated(true);
    try {
        $customer->setConfirmation(null);
        // Netzarbeiter CustomerActivation - ensure set to true
        $customer->setCustomerActivated(true);
        $customer->save();
    }
    catch (Exception $ex) {
        Zend_Debug::dump($ex->getMessage());
        Mage::log('FAIL: Customer with email '.$email.' - '.$ex->getMessage(), null, 'sulman.log');
    }

    // customer delivery address:
    $_delivery_address = array (
        'firstname' => $nameAr[0],
        'lastname' => $nameAr[1],
        'street' => array (
            '0' => $customerRecord['cd_invoice_address1'],
            '1' => $customerRecord['cd_invoice_address2']." ".$customerRecord['cd_invoice_address3'],
        ),
        'city' => $customerRecord['cd_invoice_town'],
        'region_id' => '',
        'region' => $customerRecord['cd_invoice_county'],
        'postcode' => $customerRecord['cd_invoice_postcode'],
        'country_id' => $customerRecord['cd_invoice_country_code'],
        'telephone' => $customerRecord['cd_invoice_telephone'],
        'company'=> $customerRecord['cd_invoice_name'],
    );

    $customAddress = Mage::getModel('customer/address');
    $customAddress->setData($_delivery_address)
        ->setCustomerId($customer->getId())
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
    try {
        $customAddress->save();
    }
    catch (Exception $ex) {
        Zend_Debug::dump($ex->getMessage());
    }
}

我真的无法看到其他任何方式 - 但如果有人能看到优化方法,我会很感激!     感谢

1 个答案:

答案 0 :(得分:2)

使用AvS_FastSimpleImport模块快速导入客户,就像模块一样。

https://github.com/avstudnitz/AvS_FastSimpleImport

您需要为导入客户传递数组格式的数据。

require_once 'src/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$data[] = array('firstname' => "test",
  .....
  );

try {
/** @var $import AvS_FastSimpleImport_Model_Import */
   $import = Mage::getModel('fastsimpleimport/import');
    $import->processCustomerImport($data);
} catch (Exception $e) {
    print_r($import->getErrorMessages());
}