cancel
Showing results for 
Search instead for 
Did you mean: 

php script to update customer groups wont exit at completion

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

php script to update customer groups wont exit at completion

Hi All,

I have a small php CLI script i use to update the customers group.

the script opens a csv, loops through and updates the customers group.

however once the script completes it wont exit.

I'm a bit of a php noob, so please be gentle Smiley Tongue

I'm not sure if i have a resource i need to release, of if i need to close the Mage models after i finish with them (and I'm sure i can optimise the code a heap better, for what should be a simple csv for each loop, but like i said, bit of a noob on a learning curve)

any help i can get would be appreciated.

 

i know the code gets to the end of the script as the customer records are all updated and the echo "end"; line prints to screen

 

cheers

 

<?php

 

include_once "../app/Mage.php";

Mage::app('admin')->setCurrentStore(0);

 

/**

 

Code to upload csv via sftp

removed for post

**/

 

//firstpass bool to ignor csv header row.    

$firstpass = true;

$file = 'updatedCustomerTier.csv';

$localDir = '/tmp/';

//load the customer tier file into $csv array

$csv= array_map('str_getcsv',file($localDir . $file));

              $customerCollection = Mage::getModel('customer/customer')->getCollection();

              /* @var $customerCollection Mage_Customer_Model_Entity_Customer_Collection */

                $customerCollection->addAttributeToSelect(array(

                    'dob', 'firstname', 'lastname', 'email','erp_customer_id','customer_group_code'

                                                                ));

     

              foreach ($customerCollection as $customer)

                    {    

                     foreach($csv as $line_of_text)

                     {

                             if(!$firstpass)

                                {

              //load by group code (name)     

                               $targetGroup = Mage::getModel('customer/group');

                                $targetGroup->load($line_of_text[2], 'customer_group_code');

                                $targetGroupId = $targetGroup->getCustomerGroupId();

                 /* @var $customer Mage_Customer_Model_Customer */

                              //load by customer group ID

                                        $currentGroup = Mage::getModel('customer/group');

                                        $currentGroup->load($customer->getGroupId(), 'customer_group_id');

                                        if((string)$line_of_text[0] == (string)$customer->getErpCustomerId())

                                        {

                             

                                         $customer->setData( 'group_id', $targetGroupId);

                                         $customer->save();

                                         break;

                                   

                                         

                                        }

                                

                                }

                            $firstpass=false;

                        }

                    $firstpass=true;

            }

echo "end";

unset($csv);

unset($customerCollection);

unlink('/tmp/updatedCustomerTier.csv');

 

?>

1 ACCEPTED SOLUTION

Accepted Solutions

Re: php script to update customer groups wont exit at completion

Hi Again, just an update i solved my problem Smiley Very Happy

 

because my code was so very badly written, it was taking too long to execute and causing the sftp connection (which i had failed to unset) to timeout and hang. bit of refactoring (and unsetting the sftp connection) and Boom! she works quite well.

 

refactored snippets below if anyone is interested:

 

unset($sftp);

unset($connection);

$firstpass = true;

$file = 'updatedCustomerTier.csv';

$localDir = '/tmp/';

 

$csv= array_map('str_getcsv',file($localDir . $file));

                     foreach($csv as $line_of_text)

                     {

                             if(!$firstpass)

                                {

                                $targetGroupId = groupid($line_of_text[2]);

                                $customer =  getCustomer($line_of_text[0]);

                                        if(!(is_null($customer)))

                                        {

                                         $customer->setData( 'group_id', $targetGroupId);

                                         $customer->save();

                                        }

                                }

                            $firstpass=false;

            }

function groupid($id)

{

    $targetGroup = Mage::getModel('customer/group');

    $targetGroup->load($id, 'customer_group_code');

    return  $targetGroup->getCustomerGroupId();

}

function getCustomer($id)

{

     $customerCollection = Mage::getModel('customer/customer')->getCollection();

              /* @var $customerCollection Mage_Customer_Model_Entity_Customer_Collection */

                $customerCollection->addAttributeToSelect(array(

                    'dob', 'firstname', 'lastname', 'email','erp_customer_id','customer_group_code'

                                                                ));

         foreach($customerCollection as $customer)

         {

             if($customer->getErpCustomerId() == $id)

             {

                return $customer;

             }

         }

         return NULL;

}

?>

View solution in original post

1 REPLY 1

Re: php script to update customer groups wont exit at completion

Hi Again, just an update i solved my problem Smiley Very Happy

 

because my code was so very badly written, it was taking too long to execute and causing the sftp connection (which i had failed to unset) to timeout and hang. bit of refactoring (and unsetting the sftp connection) and Boom! she works quite well.

 

refactored snippets below if anyone is interested:

 

unset($sftp);

unset($connection);

$firstpass = true;

$file = 'updatedCustomerTier.csv';

$localDir = '/tmp/';

 

$csv= array_map('str_getcsv',file($localDir . $file));

                     foreach($csv as $line_of_text)

                     {

                             if(!$firstpass)

                                {

                                $targetGroupId = groupid($line_of_text[2]);

                                $customer =  getCustomer($line_of_text[0]);

                                        if(!(is_null($customer)))

                                        {

                                         $customer->setData( 'group_id', $targetGroupId);

                                         $customer->save();

                                        }

                                }

                            $firstpass=false;

            }

function groupid($id)

{

    $targetGroup = Mage::getModel('customer/group');

    $targetGroup->load($id, 'customer_group_code');

    return  $targetGroup->getCustomerGroupId();

}

function getCustomer($id)

{

     $customerCollection = Mage::getModel('customer/customer')->getCollection();

              /* @var $customerCollection Mage_Customer_Model_Entity_Customer_Collection */

                $customerCollection->addAttributeToSelect(array(

                    'dob', 'firstname', 'lastname', 'email','erp_customer_id','customer_group_code'

                                                                ));

         foreach($customerCollection as $customer)

         {

             if($customer->getErpCustomerId() == $id)

             {

                return $customer;

             }

         }

         return NULL;

}

?>