Synchronize your customers from Magento

Home Magento TopicsSynchronize your customers from Magento
mage+php+mysql

Synchronize your customers from Magento

1 Comment

Synchronize your customers from Magento.

One of the common problems that we often find ourselves when we have to manage an e-commerce, is the data synchronization between e-commerce and our management system.

In the following example, we will show how to synchronize the information from Magento’s customers table using a small module in PHP. In our example, our management application uses a MySQL database. We use Magento API (Version 1), to retrieve customer information stored in Magento.

 

Updating Customer Information from Magento:

<?php

//*** Constants

DEFINE(‘DB_HOST_MAGENTO’, ‘Your_Magento_Host’);

//*** Access to Magento Database. 

DEFINE(‘DB_NAME_MAGENTO’, ‘Your_Magento_DB’)
DEFINE(‘DB_USER_MAGENTO’, ‘Your_Magento_DB_User’);
DEFINE(‘DB_PASS_MAGENTO’, ‘Your_Magento_DB_Password’);

//>>> Access to Magento’s API.

DEFINE(‘API_MAGENTO’, ‘http://www.your_web.com/index.php/api/?wsdl’);
DEFINE(‘USER_API’, ‘Your_API_User’);
DEFINE(‘PASS_API’, ‘Your_API_Password’);

try {

      //*** Url of connection to API Version 1.

      $customer_soapclient = new SoapClient(API_MAGENTO, array(‘trace’ => 1, ‘connection_timeout’ => 120));  

      //*** Login. Retrieve session values. You must define previously in Magento, API user and password!
      $session = $customer_soapclient ->login(USER_API, PASS_API);

 

       //*** Access to Magento Customer’s records directly from MySQL.

       //*** Connection to Magento Database.

      $mage_conn = mysql_connect(DB_HOST_MAGENTO, DB_USER_MAG, DB_PASS_MAG) or  trigger_error(mysql_error(),E_USER_ERROR);

      //*** Access to Magento database.
      $db_selected = mysql_select_db(DB_NAME_MAGENTO);
      if (!$db_selected) {
      die (‘Can\’t use foo : ‘ . mysql_error());
      }

      //*** Selection of Magento Customer table.

      $WhereMag = ‘select * from customer_entity’;

      $Customer_Mag = mysql_query($WhereMag);
      if (!$Customer_Mag) {
      echo ‘Could not run query: ‘ . mysql_error();
      exit;
      }

      //*** We prepare a new instance of customer’s table of our MySQL database! (Class previously defined!)

      $MySQL_Customer = new MySQL_Customer();
      $Updated = False;

      //*** We read all Magento customer’s records and update or insert in our MySQL customer table.

      while ($arrobjCustomer_Mag = mysql_fetch_row($Customer_Mag))
      {

         //*** Retrieve Magento Customer record.
      $MagCustomer_Record = $customer_soapclient ->call($session, “customer.info”,$arrobjCustomer_Mag[0]);

         //*** Update record values in an array (example fields!).

      $NewArray = array(“MySQL_Mag_Customer_Id” => $MagCustomer_Record[“customer_id”],
      “MySQL_Created_At” => $MagCustomer_Record[“created_at”],
      “MySQL_Updated_At” => $MagCustomer_Record[“updated_at”],
      “MySQL_Store_Id” => $MagCustomer_Record[“store_id”],
      “MySQL_Website_Id” => $MagCustomer_Record[“website_id”],
      “MySQL_Email” => $MagCustomer_Record[“email”],
      “MySQL_Firstname” => $MagCustomer_Record[“firstname”],
      “MySQL_Gender” => $MagCustomer_Record[“gender”],
      “MySQL_Lastname” => $MagCustomer_Record[“lastname”],
      “MySQL_Middlename” => $MagCustomer_Record[“middlename”]);

         //*** Update or insert record in MySQL DB.

         //*** We use 2 methods previously defined: Update and insert. You could use a MySQL query!

      $WhereMySQLGet = ‘ WHERE MySQL_Customer_Id = ‘.$MagCustomer_Record[“customer_id”];
      $MySQL_Customer->Select($WhereMySQLGet);
      foreach ($MySQL_Customer->record as $key => $arroMySQL_Customer)
      {
      $MySQL_Customer->Update($NewArray);
      $Updated = True;
      }

      If ($Updated == False)
      {
      $MySQL_Customer->Insert($NewArray);
      }


      }//*** End While

      //*** Logout
      $customer_soapclient ->endSession($session);

      echo _(“Update of Customer Information is succesfull!”);

 

} catch (Exception $e) {
echo ‘<h1>Error</h1>’;
echo ‘<p>’ . $e->getMessage() . ‘</p>’;
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *