1

I'm using Magento 1.9.0.1!

I'm currently searching a way to programmatically delete a user/customer address where email is responding to the pointed one.

Take a look at my code:

<?PHP
ob_start();
session_start();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?PHP
$Timer = $_COOKIE["ConvertorTimer"];    
$Timer = $Timer + 1;    
setcookie("ConvertorTimer", $Timer, time()+3600);  /* expire in 1 hour */

if($Timer <= 550){ ?>
 <meta http-equiv="refresh" content="1">
<?PHP } ?>
</head>
<?PHP

    $dbhost = "127.0.0.1";
    $dbuser = "user_convert";
    $dbpass = "mypass";
    $database = "mydb";

        error_reporting(E_ALL | E_STRICT);
        $mageFilename = 'app/Mage.php';
        require_once $mageFilename;
        $app = Mage::app('default'); 
        Mage::app();


    function ConnectWithMySQLDatabase() {

        global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;

            $conres=mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_query('set names utf8', $conres);


        if (!$conres)
        {
          switch (mysql_errno())
          {
            case 1040:
            case 2002:
                if ($HTTP_SERVER_VARS[REQUEST_METHOD] == "GET")
                    die("<html><head><meta http-equiv=refresh content=\"20 $HTTP_SERVER_VARS[REQUEST_URI]\"></head><body><table border=0 width=100% height=100%><tr><td><h3 align=center>".ERR_SERVER_LOAD."</h3></td></tr></table></body></html>");
                else
                    die(ERR_CANT_CONNECT);
            default:
                die("[" . mysql_errno() . "] dbconn: mysql_connect: " . mysql_error());
          }
        }
        mysql_select_db($database)
            or die(ERR_CANT_OPEN_DB." $database - ".mysql_error());



    }

    ConnectWithMySQLDatabase();

                $r = mysql_query("SELECT * FROM `Customers` WHERE `CustomerPasswordHashMode`='1' AND `Converted`='0' ORDER BY `CustomerID` DESC LIMIT 1");


                while($rowi = mysql_fetch_array($r))
                {
                $CustomerID = addslashes($rowi['CustomerID']);
                $CustomerEmail = addslashes($rowi['CustomerEmail']);



                // So here i've got the email $CustomerEmail variable.
                // Now i want Magento to delete all addresses on customer which have the email $CustomerEmail
                // Simple SQL example which is not corresponding to the real Magento Structube but maybe will point
                // my idea better:
                // DELETE * FROM `Magento_Customer_Addresses` WHERE `CustomerEmail`='$CustomerEmail'
                // The example above is not answering to the real Magento Structure and the way Magento saves the addresses for it's customers.
                // I know that maybe i'll have to get the CustomerID's by the email and then delete the adressess by customer IDs but i don't know how;





                echo "$CustomerAddressFirstName $CustomerAddressLastName - Address has been deleted!!! Current Number of converting: $Timer<br>";
                }

?>

I've Used this code to convert accounts from Old system to the Magento Store:

<?PHP
ob_start();
session_start();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?PHP
$Timer = $_COOKIE["ConvertorTimer"];    
$Timer = $Timer + 1;    
setcookie("ConvertorTimer", $Timer, time()+3600);  /* expire in 1 hour */

if($Timer <= 550){ ?>
 <meta http-equiv="refresh" content="1">
<?PHP } ?>
</head>
<?PHP

    $dbhost = "127.0.0.1";
    $dbuser = "user_convert";
    $dbpass = "mypass";
    $database = "mydb";

        error_reporting(E_ALL | E_STRICT);
        $mageFilename = 'app/Mage.php';
        require_once $mageFilename;
        $app = Mage::app('default'); 
        Mage::app();


    function ConnectWithMySQLDatabase() {

        global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;

            $conres=mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_query('set names utf8', $conres);


        if (!$conres)
        {
          switch (mysql_errno())
          {
            case 1040:
            case 2002:
                if ($HTTP_SERVER_VARS[REQUEST_METHOD] == "GET")
                    die("<html><head><meta http-equiv=refresh content=\"20 $HTTP_SERVER_VARS[REQUEST_URI]\"></head><body><table border=0 width=100% height=100%><tr><td><h3 align=center>".ERR_SERVER_LOAD."</h3></td></tr></table></body></html>");
                else
                    die(ERR_CANT_CONNECT);
            default:
                die("[" . mysql_errno() . "] dbconn: mysql_connect: " . mysql_error());
          }
        }
        mysql_select_db($database)
            or die(ERR_CANT_OPEN_DB." $database - ".mysql_error());



    }

    ConnectWithMySQLDatabase();

                $r = mysql_query("SELECT * FROM `Customers` WHERE `CustomerPasswordHashMode`='1' AND `Converted`='0' ORDER BY `CustomerID` DESC LIMIT 1");


                while($rowi = mysql_fetch_array($r))
                {
                $CustomerID = addslashes($rowi['CustomerID']);
                $CustomerEmail = addslashes($rowi['CustomerEmail']);


                $v = mysql_query("SELECT * FROM `CustomerAddresses`  WHERE `CustomerAddressID`='$CustomerID' AND `CustomerAddressFirstName`!=''");
                $vrowi = mysql_fetch_array($v);



                $CustomerAddressFirstName = addslashes($vrowi['CustomerAddressFirstName']);
                $CustomerAddressLastName = addslashes($vrowi['CustomerAddressLastName']);
                $CustomerPassword = "$CustomerAddressFirstName-$CustomerAddressLastName-005";
                $CustomerAddressLine1 = addslashes($vrowi['CustomerAddressLine1']);
                $CustomerAddressCity = addslashes($vrowi['CustomerAddressCity']);
                $CustomerAddressPhone = addslashes($vrowi['CustomerAddressPhone']);
                $CustomerAddressZip = addslashes($vrowi['$CustomerAddressZip']);

                $customer = Mage::getModel("customer/customer");
                $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
                $customer->setStore(Mage::app()->getStore());

                $customer->setFirstname("$CustomerAddressFirstName");
                $customer->setLastname("$CustomerAddressLastName");
                $customer->setEmail("$CustomerEmail");
                $customer->setPasswordHash(md5("$CustomerPassword"));
                $customer->save();


                $address = Mage::getModel("customer/address");
                // you need a customer object here, or simply the ID as a string.
                $address->setCustomerId($customer->getId());

                $address->setCountryId("BG"); //Country code here
                $address->setStreet("$CustomerAddressLine1");
                $address->setPostcode("$CustomerAddressZip");
                $address->setCity("$CustomerAddressCity");
                $address->setTelephone("$CustomerAddressPhone");

                $address->save();

                mysql_query("UPDATE `Customers` SET `Converted`='1' WHERE `CustomerID`='$CustomerID'");



                echo "$CustomerAddressFirstName $CustomerAddressLastName ID: <b>$CustomerID</b> - has been succesfully converted!!! Current Number of converting: $Timer<br>";
                }

?>

The problem with this convertor was the fact that it setted the address to customer by taking the address from the previously converted account but not from the account that it is actually converting now.

In another example, when it convert accounts with ids 1, 2, 3, it places the Adress of account 2 from account 1, and it places the address for account 3 taking it from account 2 and so on.

Now i have to fix this mistake, if you have any other better idea i'll be glad to hear it!

Thanks in advance!

Venelin Vasilev
  • 1,062
  • 4
  • 33
  • 60

1 Answers1

2

Here is how your loop should look like:

while($rowi = mysql_fetch_array($r))
{
    $CustomerID = addslashes($rowi['CustomerID']);
    $CustomerEmail = addslashes($rowi['CustomerEmail']);


    $v = mysql_query("SELECT * FROM `CustomerAddresses`  WHERE `CustomerAddressID`='$CustomerID' AND `CustomerAddressFirstName`!=''");
    $vrowi = mysql_fetch_array($v);



    $CustomerAddressFirstName = addslashes($vrowi['CustomerAddressFirstName']);
    $CustomerAddressLastName = addslashes($vrowi['CustomerAddressLastName']);
    $CustomerPassword = "$CustomerAddressFirstName-$CustomerAddressLastName-005";
    $CustomerAddressLine1 = addslashes($vrowi['CustomerAddressLine1']);
    $CustomerAddressCity = addslashes($vrowi['CustomerAddressCity']);
    $CustomerAddressPhone = addslashes($vrowi['CustomerAddressPhone']);
    $CustomerAddressZip = addslashes($vrowi['$CustomerAddressZip']);


    $customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getWebsite()->get‌​‌​Id())->loadByEmail($CustomerEmail);


    $dataShipping = array(
            'firstname'  => $CustomerAddressFirstName,
            'lastname'   => $CustomerAddressLastName,
            'street'     => array($CustomerAddressLine1),
            'city'       => $CustomerAddressCity,
            'region'     => '',
            'region_id'  => '',
            'postcode'   => $CustomerAddressZip,
            'country_id' => 'BG',
            'telephone'  => $CustomerAddressPhone,
    );

                                        $customerAddress = Mage::getModel('customer/address');
    if ($defaultShippingId = $customer->getDefaultShipping()){
             $customerAddress->load($defaultShippingId);
    } else {  
             $customerAddress
                    ->setCustomerId($customer->getId())
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1')
             ;  

             $customer->addAddress($customerAddress);
    }            

    try {
            $customerAddress
                    ->addData($dataShipping)
                    ->save()
            ;          
    } catch(Exception $e){
            Mage::log('Address Save Error::' . $e->getMessage());
    }

    mysql_query("UPDATE `Customers` SET `Converted`='1' WHERE `CustomerID`='$CustomerID'");



    echo "$CustomerAddressFirstName $CustomerAddressLastName With Adress: $CustomerAddressCity - $CustomerAddressLine1 - $CustomerAddressPhone ID: <b>$CustomerID</b> - has been succesfully converted!!! Current Number of converting: $Timer<br>";
}
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352