Skip to main content
edited
Source Link
user2240778
  • 641
  • 12
  • 29
<?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 ."Guest <br"<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();
?>
lht@gmail.comGuestcom
4337@icloud.comGuestcom

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

it seems to be adding a Guest to the end of the email ID, andits not importing the subscriber names, type, nor the correct website, store or store view.

<?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 ."Guest <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();
?>
lht@gmail.comGuest
4337@icloud.comGuest

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

it seems to be adding a Guest to the end of the email ID, and not importing the subscriber names, type, nor the correct website, store or store view.

<?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();
?>
lht@gmail.com
4337@icloud.com

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.

edited
Source Link
user2240778
  • 641
  • 12
  • 29
<?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[0];$data[1];
       /* customer */
        if($customerAccountManagement->isEmailAvailable($emailId , $websiteId)){
            echo $emailId ."Guest <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();
?>

enter image description here

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

it seems to be adding a Guest to the end of the email ID, and not importing the subscriber names, type, nor the correct website, store or store view.

enter image description here

<?php
  
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.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        // email Id
        $emailId = $data[0];
       /* customer */
        if($customerAccountManagement->isEmailAvailable($emailId , $websiteId)){
            echo $emailId ."Guest <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);
}

enter image description here

<?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 ."Guest <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();
?>

enter image description here

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

it seems to be adding a Guest to the end of the email ID, and not importing the subscriber names, type, nor the correct website, store or store view.

enter image description here

edited
Source Link
user2240778
  • 641
  • 12
  • 29

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.

It imports one line in which the email ID doesn't show and its not in the order of the csv file, as 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.

edited
Source Link
user2240778
  • 641
  • 12
  • 29
Loading
edited
Source Link
user2240778
  • 641
  • 12
  • 29
Loading
Source Link
user2240778
  • 641
  • 12
  • 29
Loading