Running a multi website and store setup.
Example
Website - Japan Store - JP Store View - JP
I'm trying to import newsletter subscribers from a CSV.
I'm using the following code in a file called import.php that I run from the root dir. eg: dev.xyz.com/import.php and the subscribers.csv is also in the root dir.
<?php
ob_start();
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$store_id = 1;
$csv_filepath = "subscribers.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 = $_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();
?>
CSV Headers/Column names as follows:
ID | Email | Type | Customer First Name | Customer Last Name | Status | Website Store | Store View
But I get the following error
IDGuest
Warning: Cannot modify header information - headers already sent by (output started at /home/xyz/dev.xyz.com/import.php:36) in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 148
Fatal error: Uncaught Magento\Framework\Stdlib\Cookie\FailureToSendException: The cookie with "mage-cache-sessid" cookieName couldn't be deleted. in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php:154 Stack trace: #0 /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(301): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setCookie('mage-cache-sess...', '', Array) #1 /home/xyz/dev.xyz.com/vendor/magento/module-customer/CustomerData/Plugin/SessionChecker.php(54): Magento\Framework\Stdlib\Cookie\PhpCookieManager->deleteCookie('mage-cache-sess...', Object(Magento\Framework\Stdlib\Cookie\CookieMetadata)) #2 /home/xyz/dev.xyz.com/vendor/magento/framework/Interception/Interceptor.php(121): Magento\Customer\CustomerData\Plugin\SessionChecker->beforeStart(Object(Magento\Framework\Session\Generic\Interceptor)) #3 /home/xyz/dev.xyz.com/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\Sessio in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 154
How can I import newsletter subscribers from a CSV?
Thanks in advance.
UPDATE
Changed code as suggested by Rajesh Hothi
Now I get the following error
IDGuest
Fatal error: Uncaught Exception: The input is not a valid email address. Use the basic format local-part@hostname in /home/xyz/dev.xyz.com/vendor/magento/module-newsletter/Model/Subscriber.php:501 Stack trace: #0 /home/xyz/dev.xyz.com/import.php(37): Magento\Newsletter\Model\Subscriber->subscribe('ID') #1 {main} thrown in /home/xyz/dev.xyz.com/vendor/magento/module-newsletter/Model/Subscriber.php on line 501
Format of CSV file shown below.
It uses comma ',' as seperator
- ID,Email,Type,Customer First Name,Customer Last Name,Status,Website,Store,Store View
- 1,[email protected],Customer,村瀬,康洋,Subscribed,Japan,JP,Japan
- 2,[email protected],Guest, ----, ----,Subscribed,Japan,JP,Japan
- 3,[email protected],Customer,TANAKA,MASAKI,Subscribed,Japan,JP,Japan
- 4,[email protected],Customer,古賀,裕章,Subscribed,Japan,JP,Japan
It imports one line in which the email ID doesn't show and its not in the order of the csv file, as below.
UPDATE 2
Changed code as suggested by Raul Sanchez, it gave the same error, I figured it was the header row (row 1) that was causing error and removed it.
now it gives the following error
[email protected]
[email protected]
Fatal error: Uncaught Error: Call to a member function create() on integer in /home/xyz/dev.xyz.com/import.php:37 Stack trace: #0 {main} thrown in /home/xyz/dev.xyz.com/import.php on line 37
its not importing the subscriber names, type, nor the correct website, store or store view.
$_subscriberFactory = $_subscriberFactory->create()->subscribe($emailId );
for just$_subscriberFactory->create()->subscribe($emailId);
$emailId = $data[1];