Forked from collymore/magento-1-2-migration-update-urls
Created
June 20, 2023 21:28
-
-
Save artmouse/282ca1708e513fc0e1cfd75635fba992 to your computer and use it in GitHub Desktop.
Replace Magento 2 url_key with correct value from Magento 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('max_execution_time', 0); | |
ini_set('display_errors', 1); | |
require __DIR__ . '/../app/bootstrap.php'; | |
use \Magento\Framework\App\Bootstrap; | |
$bootstrap = Bootstrap::create(BP, $_SERVER); | |
$objectManager = $bootstrap->getObjectManager(); | |
$state = $objectManager->get('\Magento\Framework\App\State'); | |
$state->setAreaCode('adminhtml'); | |
$productRepo = $objectManager->create('\Magento\Catalog\Model\ProductRepository'); | |
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); | |
$connection = $resource->getConnection(); | |
/******************************************************************************************************/ | |
** Takes a urls.CSV file containing SKU,URL_Key from M1 db export | |
** See https://www.develodesign.co.uk/learn/magento-migration-url-issue-duplicate-url-keys | |
/******************************************************************************************************/ | |
$productCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory') | |
->create()->addAttributeToSelect("url_key"); | |
$skuMap = $this->createSkuMap($connection); | |
$urlMap = $this->createUrlMap($skuMap, $connection); | |
foreach ($productCollection as $product) { | |
$product = $productRepo->getById($product->getId(), false, 0); | |
if (isset($urlMap[$product->getId()])) { | |
if ($product->getUrlKey() != $urlMap[$product->getId()]) { | |
echo "{$product->getId()} : {$product->getUrlKey()} to {$urlMap[$product->getId()]}"; | |
$product->setUrlKey($urlMap[$product->getId()]); | |
$productRepo->save($product); | |
echo " : saved \n"; | |
} | |
} | |
} | |
echo "done"; | |
die; | |
/** | |
* Load CSV to array | |
* @param $filePath | |
* @return array | |
*/ | |
function readFromCsvFile($filePath): array | |
{ | |
$result = []; | |
if (($handle = fopen($filePath, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1000)) !== FALSE) { | |
$result[] = $data; | |
} | |
fclose($handle); | |
} | |
return $result; | |
} | |
/** | |
* Returns product SKU => URL | |
* @param $skuMap | |
* @param $connection | |
* @return array | |
*/ | |
function createUrlMap($skuMap, $connection): array | |
{ | |
$urls = readFromCsvFile('urls.csv'); //sku,url | |
$urlMap = []; | |
foreach ($urls as $url) { | |
if (isset($skuMap[trim(strtolower($url[0]))])) | |
$urlMap[$skuMap[trim(strtolower($url[0]))]] = trim($url[1]); | |
} | |
return $urlMap; | |
} | |
/** | |
* Returns product SKU => Entity_ID | |
* @param $connection | |
* @return array | |
*/ | |
function createSkuMap($connection): array | |
{ | |
$query = "select * from catalog_product_entity"; | |
$products = $connection->fetchAll($query); | |
$skuMap = []; | |
foreach ($products as $product) { | |
$skuMap[strtolower(trim($product['sku']))] = $product['entity_id']; | |
} | |
return $skuMap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment