2

After migration of magento1 to 2 i am getting the issue.The Phone Number Attribute has the values.But I want that values in my new attribute Mobile Number. how to copy customer attribute values of phone number attribute to mobile number attribute using CLI Command in magento2

1 Answer 1

2

Assume you have a basic module. Can you please try with the following method.

Put this file on etc/di.xml

<!-- 
/**
 * @package Vendor_Modulename
 */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="migrate_attributedata" xsi:type="object"> Vendor\Modulename\Console\Command\AttributeMigrate </item>
</argument>
</arguments>
</type>
</config>

AttributeMigrate on Vendor\Modulename\Console\Command

<?php
/**
 * @package Vendor_Modulename
 */
declare(strict_types=1);

namespace Vendor\Modulename\Console\Command;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerFactory as CollectionFactory;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\CustomerFactory;

class AttributeMigrate extends Command
{
    /**
     * @var State
     */
    private $state;
    /**
     * @var DirectoryList
     */
    private $directoryList;
    /**
     * @var Customer
     */
    private $collectionFactory;
    /**
     * @var CollectionFactory
     */
    private $customerRepository;

    /**
     * Customer constructor.
     * @param string|null $name
     * @param State $state
     * @param DirectoryList $directoryList
     * @param Customer $CollectionFactory
     * @param Page $page
     */
    public function __construct(
        State $state,
        DirectoryList $directoryList,
        CollectionFactory $collectionFactory,
        CustomerRepositoryInterface $customerRepository,
        Customer $customer,
        CustomerFactory $customerFactory
    ) {
        $this->state = $state;
        $this->directoryList = $directoryList;
        $this->collectionFactory = $collectionFactory;
        $this->customerRepository = $customerRepository;
        $this->customer = $customer;
        $this->customerFactory = $customerFactory;

        parent::__construct();
    }

    /**
     * Configures the command.
     */
    protected function configure()
    {
        $this->setName('Vendor:migrate_attributedata');
        $this->setDescription('Migrate Attribute Data');
        parent::configure();
    }

    /**
     * Excute function to migrate
     */
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
        $output->writeln('<info>Migration Started...</info>');

        $customers = $this->collectionFactory->create()->getCollection();

        foreach ($customers as $customerArr) {
            $customerId = $customerArr->getId();
            $customeratt = $this->collectionFactory->create();
            $customeratt->load($customerId);
            $customerValues = $customeratt->getData();

            if (isset($customerValues['customer_phone_number'])) {
                $phoneNumber = $customerValues['customer_phone_number'];
                $customer = $this->customer->load($customerId);
                $customerData = $customer->getDataModel();
                $customerData->setCustomAttribute('mobilenumber', $phoneNumber);
                $customer->updateData($customerData);
                $customerResource = $this->customerFactory->create();
                $customerResource->saveAttribute($customer, 'mobilenumber');
                $output->writeln(
                    '<info>' .
                        $customerValues['email'] .
                        '->' .
                        $customerValues['customer_phone_number'] .
                        ' saved.</info>'
                );
            } else {
                $output->writeln('<info>not found</info>');
            }
        }
        $output->writeln('<info>Migration Completed...</info>');
    }
}

And Finally U run the following command php bin/magento Vendor:migrate_attributedata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.