Decrypting Magento 2 customer passwords using email for migration to Shopify

28 views Asked by At

I need to migrate approximately 200,000 customers to Shopify, including their passwords. However, it appears that Magento does not provide this option by default, likely due to security reasons. I have attempted to use the code below, but it is returning an empty string for the password.

use Magento\Framework\App\Bootstrap;

require DIR.'/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

// Customer Repository
//$singlecustomer = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->getById('206991');
$customerCollection = $objectManager -    >create('\Magento\Customer\Model\ResourceModel\Customer\Collection');
try {

    // Output CSV header
    $csvHeader = ['ID', 'First Name', 'Last Name', 'Email', 'Hash', 'Password'];
    $outputFile = fopen('customers.csv', 'w');
    fputcsv($outputFile, $csvHeader);

    // Loop through customers and write to CSV
    $i = 1;
    $encrypt = $objectManager->create('\Magento\Framework\Encryption\Encryptor');
    foreach ($customerCollection as $customer) {
        if ($i == 10) {
            break;
        }
        $customerId = $customer->getId();
        $customerFirstName = $customer->getFirstname();
        $customerLastName = $customer->getLastname();
        $customerEmail = $customer->getEmail();
        $pass = false;
        $hash = $customer->getPasswordHash() ?? '';
        $password = '';
        if (!empty($hash)) {
            $password = $encrypt->decrypt($hash);
        }
        
        fputcsv($outputFile, [$customerId, $customerFirstName, $customerLastName, $customerEmail, $hash, $password]);
        $i++;
    }

    fclose($outputFile);

    echo "Customer data exported successfully.\n";
} catch (Exception $e) {
    echo "Error: ".$e->getMessage()."\n";
}

The code above generates empty passwords because I need plaintext passwords to import them into Shopify. I understand that this may not be possible due to security reasons, but I'm hoping someone more knowledgeable can provide a solution.

0

There are 0 answers