Magento 1 数据库迁移到Magento 2(只迁移客户信息)

本篇文章转载:https://www.sky8g.com/technology/1988/

好多天没有写文章了,最近有点事情。今天我在这篇文章里面我将讲述关于magento 1数据库的迁移到magento 2的操作步骤。 本篇文章更新于:2019年04月13日

大家都知道magento 1的代码结构和magento 2的代码结构完全不同的,直接升级是行不通的。magento官网有介绍的是利用迁移工具来进行迁移,在这里我将介绍使用代码进行迁移。

下面我将直接使用代码操作使用数据库的迁移。本例只迁移客户信息。

第一步:在目标的m1的代码结构里面创建个api接口,创建app/code/Sky8g/Hello/controllers/Api/CustomerController.php

<?php
class Sky8g_Hello_Api_TestController extends Mage_Core_Controller_Front_Action
{
	public function indexAction()
	{
		$customerCollection =	Mage::getModel('customer/customer')->getCollection();     
		$from = $this->getRequest()->getParam('from');
		$to = $this->getRequest()->getParam('to');
		if(empty($to) || empty($from) ){
			echo json_encode([]);
			return;
		}
		$result =  $customerCollection->addFieldToFilter('created_at',  array("from" => $from, "to" => $to ) );
		$customer = Mage::getModel('customer/customer');
		foreach ($result as  $customer) {
			# code...
			$customerId = $customer->getId();
			$data[] = $customer->load($customerId)->getData();
		}
		 echo json_encode($data);

    }
}

保存上传到m1的代码中。

第二步:在目标的m2的代码结构里面创建app/code/Sky8g/Hello/Controller/Index/Customer.php

<?php
namespace Sky8g\Hello\Controller\Index;
class Customer extends \Magento\Framework\App\Action\Action
{
	protected $_pageFactory;
  const PASSWORD_HASH = 0;
  const PASSWORD_SALT = 1;
  protected $_storeManager;
	public function __construct(
		\Magento\Framework\App\Action\Context $context,
		\Magento\Framework\View\Result\PageFactory $pageFactory,
     \Magento\Store\Model\StoreManagerInterface $storeManager)
	{
		$this->_pageFactory = $pageFactory;
     $this->_storeManager = $storeManager;
		return parent::__construct($context);
	}
    private function setcurlconfig()
    {
        $setheader =array(
          'timeout' =>40,
          'CURLOPT_USERAGENT'=>'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
          'CURLOPT_REFERER'=>'https://www.sky8g.com/',
          'header'=>''
           ); 
        return $setheader;
    }
    private function upgradeCustomerHash($hash)
    {
        if (isset($hash)) {
            $hashExploded = $this->explodePasswordHash($hash);
            if (strlen($hashExploded[self::PASSWORD_HASH]) == 32) {
                $hash = implode(':', [$hashExploded[self::PASSWORD_HASH], $hashExploded[self::PASSWORD_SALT], '0']);
            } elseif (strlen($hashExploded[self::PASSWORD_HASH]) == 64) {
                $hash = implode(':', [$hashExploded[self::PASSWORD_HASH], $hashExploded[self::PASSWORD_SALT], '1']);
            }
        }
        return $hash;
    }
    /**
     * @param string $passwordHash
     * @return array
     */
    private function explodePasswordHash($passwordHash)
    {
        $explodedPassword = explode(':', $passwordHash, 2);
        $explodedPassword[self::PASSWORD_SALT] = isset($explodedPassword[self::PASSWORD_SALT])
            ? $explodedPassword[self::PASSWORD_SALT]
            : ''
        ;
        return $explodedPassword;
    }
    public function doUpdata($from,$to){
     
      $url = 'https://www.sky8g.com/en/sky8g/api_customer/';
      $params = ['from'=>$from,'to'=>$to];
      $curlAdapter = $this->_objectManager->get(\Magento\Framework\HTTP\Adapter\CurlFactory::class)->create();
      $curlAdapter->setConfig($this->setcurlconfig());
      $curlAdapter->write('POST', $url, '1.1', $this->setcurlconfig(), http_build_query($params, null, '&') );
      $result = $curlAdapter->read();
      $data =json_decode($result,true);
      if(empty($data)){
        echo "没有设置日期";
        $_SESSION["flag"] = true;
        return; 
      } 
        $customer = $this->_objectManager ->get(\Magento\Customer\Model\Customer::class);
        $i=0;
        $created_in = $this->_storeManager->getStore()->getName();
        $store_id   = $this->_storeManager->getStore()->getStoreId();
        foreach ($data as $customerData) {
                //print M2 password hash
                $customerData['password_hash'] =  $this->upgradeCustomerHash( $customerData['password_hash'] );
                $customerData['created_in']    =  $created_in;
                $customerData['store_id']      =  $store_id;
                $customer->setData($customerData);
                try {
                    $customer->save();
                    $i++;
                    echo $i.'<br/>';
                }catch (Exception $e) {
                    echo  $e->getMessage();
                }

        }
    }

	public function execute()
	{
      ignore_user_abort();//关闭浏览器仍然执行
      set_time_limit(0);//让程序一直执行下去
      if( !isset($_SESSION["flag"]) ){
          echo '<meta http-equiv="refresh" content="2;url=http://www.sky8g2.com/sky8g/index/customer" />'; 
      }
      ob_flush(); //将数据从php的buffer中释放出来
      flush(); //将释放出来的数据发送给浏览器
      if (!isset($_SESSION["num"])){
               $_SESSION["num"] = 1391212800; // strtotime('2014-02-01')
               $from= $_SESSION["num"];
      }else {
               $_SESSION["num"] = $_SESSION["num"]+2592000;  //30天
               $from = $_SESSION["num"];
               $to = $_SESSION["num"]+2592000;
      }
      if(!isset($to)){
        $to = 1393804800;
      }
      $from = date('Y-m-d H:i:s',$from);
      $to   = date('Y-m-d H:i:s',$to);
      $this->doUpdata($from,$to);
	}
}

如果有不懂得地方请去https://www.sky8g.com/

留言给我,希望对你有帮助。

猜你喜欢

转载自blog.csdn.net/jimbooks/article/details/90179901