Do You Want to Learn Marketing or Coding? Do You Want to Learn Marketing or Coding?

Magento Delete Old Credit Card Numbers – Solved

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Here is a quick tutorial on how to clear credit card numbers. I will include a few scripts that you might helpful. BACKUP YOUR DATABASE. If you don’t back up your database before running these scripts you are playing with fire. So always, back-up before running the scripts.

Magento Clear Credit Cards of Complete and Canceled Orders

Here is a script to clear credit cards for complete or canceled orders. Place this script into a php file on the root level of your magento installation. Wherever you see files like app, skin, var.

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

require_once('app/Mage.php');
Mage::app();

$orders = Mage::getModel('sales/order')->getCollection()
   ->addFieldToFilter('status', array('in' => array('canceled','complete')))
   ->addAttributeToSelect('status')
   ->addAttributeToSelect('increment_id')
   ->addAttributeToSelect('entity_id')
   ->addAttributeToSelect('quote_id')
   ->addAttributeToSelect('created_at');

$prefix=(string)Mage::getConfig()->getTablePrefix(); // Get Prefix

// fetch write database connection that is used in Mage_Core module
$write = Mage::getSingleton('core/resource')->getConnection('core_write');

foreach ($orders as $order) {
    $IncrementId = $order->getIncrementId();
    $OrderStatus = $order->getStatus();
    $EntityId = $order->getEntityId();  // Very Important
    $QuoteId = $order->getQuoteId(); // Very Important
    $year = date('Y', strtotime($order->getCreatedAt()));

           $updatefirsttable = "update ".$prefix."sales_flat_quote_payment set cc_number_enc=null where quote_id=".$QuoteId."";
           $updatesecondtable = "update ".$prefix."sales_flat_order_payment set cc_number_enc=null where entity_id=".$EntityId."";

           $write->query($updatefirsttable);  // Update Quote
           $write->query($updatesecondtable); // Update Order
            echo "Increment ID";
            echo $IncrementId;
            echo ' Quote id: ';
            echo $QuoteId;
            echo ' Entity IdL ';
            echo $EntityId;
            echo ' Year: ';
            echo $year;
            echo '<br/>';

}
echo "complete";

This should go through and remove all of the credit cards for completed and canceled orders. It should also print out to the Increment ID, Quote, Entity ID and Year, this is so you can troubleshoot it if there is an error.

Clearing Pending Credit Cards from Magento by Year

I had a client that needed to clear credit cards for items that were considered PENDING because they never updated them to complete. So I wanted to clear everything before the year 2015. So I just made a few modifications to the script. This clears all pending orders credit cards before the year 2015. You can update it to your year by changing 2015 to whatever year you want.

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

require_once('app/Mage.php');
Mage::app();

$orders = Mage::getModel('sales/order')->getCollection()
   ->addFieldToFilter('status', array('in' => array('pending')))
   ->addAttributeToSelect('status')
   ->addAttributeToSelect('increment_id')
   ->addAttributeToSelect('entity_id')
   ->addAttributeToSelect('quote_id')
   ->addAttributeToSelect('created_at');

$prefix=(string)Mage::getConfig()->getTablePrefix(); // Get Prefix

// fetch write database connection that is used in Mage_Core module
$write = Mage::getSingleton('core/resource')->getConnection('core_write');

foreach ($orders as $order) {
    $IncrementId = $order->getIncrementId();
    $OrderStatus = $order->getStatus();
    $EntityId = $order->getEntityId();  // Very Important
    $QuoteId = $order->getQuoteId(); // Very Important
    $year = date('Y', strtotime($order->getCreatedAt()));

    if ($year < 2015) {

           $updatefirsttable = "update ".$prefix."sales_flat_quote_payment set cc_number_enc=null where quote_id=".$QuoteId."";
           $updatesecondtable = "update ".$prefix."sales_flat_order_payment set cc_number_enc=null where entity_id=".$EntityId."";

           $write->query($updatefirsttable);  // Update Quote
           $write->query($updatesecondtable); // Update Order
            echo "Increment ID";
            echo $IncrementId;
            echo ' Quote id: ';
            echo $QuoteId;
            echo ' Entity IdL ';
            echo $EntityId;
            echo ' Year: ';
            echo $year;
            echo '<br/>';
    }

}
echo "complete";

I Really Need

Reviewing nerdy stuff is expensive.

Consider clicking on this ad & buying that item you REALLY wanted from Amazon.

It helps a ton!

Leave Your Thoughts