0

I run a multi website and store setup, on PHP 7.1

I'm trying to import newsletter subscribers from a CSV as follows, which is in a CSV file called subscribers-import.csv in the root dir.

enter image description here

I'm using the following code in a file called import.php in the root dir.

<?php
  ob_start();
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$store_id = 1;
$csv_filepath = "subscribers-import.csv";
$csv_delimiter = ',';
$csv_enclosure = '"';
$magento_path = __DIR__;

$params = $_SERVER;

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

$obj = $bootstrap->getObjectManager();

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

$websiteId =  $obj->get('Magento\Store\Model\StoreManagerInterface')
    ->getStore()
    ->getWebsiteId();

$customerAccountManagement =$obj
    ->create('Magento\Customer\Api\AccountManagementInterface');

$_subscriberFactory = $obj->create('Magento\Newsletter\Model\SubscriberFactory');


if (($handle = fopen("subscribers-import.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        // email Id
        $emailId =  $data[1];
       /* customer */
        if($customerAccountManagement->isEmailAvailable($emailId , $websiteId)){
            echo $emailId ."<br/>";
            $_subscriberFactory->create()->subscribe($emailId );
        }else{
            echo $emailId ."Register <br/>";
            $customerFactory = $obj->get('Magento\Customer\Model\CustomerFactory');
            $customer=$customerFactory->create();
            $customer->setWebsiteId($websiteId);
            $customer->loadByEmail($emailId );// load customer by email address
            if($customer->getId()){
                $_subscriberFactory->create()->subscribeCustomerById($customer->getId());
            }

        }
    }
    fclose($handle);
}
 ob_end_flush();
?>

Right now it only imports the email ID, as shown as follows.

enter image description here

How can I get it to import the rest of the data in the CSV like

Type | Customer First Name | Customer Last Name | Status | Website Store | Store View

1 Answer 1

0

Free Extension

You Got Value in $data Array Like This :-

$data[0] = id
$data[1] = email
$data[2] = type
$data[3] = customer first name
$data[4] = customer last name
$data[5] = status
$data[6] = website
$data[7] = store
$data[8] = store view
3

Your Answer

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

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