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.
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.
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