Magento 1 數據庫遷移到Magento 2(只遷移客戶信息)

本篇文章轉載:https://www.sky8g.com/technology/1988/

如果瀏覽不順暢請到原文章出處:https://www.sky8g.com/technology/1988/

請注意可能會提示風險,這是csdn官網如果不是他們的網址,其他的網址都會提示有風險,這是CSDN網站設置的問題,本網站全部文章爲免費技術分享,請放心訪問,無需擔心。

原文章出處:https://www.sky8g.com/technology/1988/

此篇文章是由SKY8G網作者原創,禁止抄襲。

好多天沒有寫文章了,最近有點事情。今天我在這篇文章裏面我將講述關於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/

留言給我,希望對你有幫助。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章