Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento table "sales_flat_order" field "protect_code" explanation

We are working on magento database and tables. Magento seems to write a code in table sales_flat_order field protect_code to define if there is a invoice or a shipment done already. It would look something like

01b335 or
a0a243

But there is no key to understand what this protection code means. Is there an explanation of the meaning of these codes and how they are generated?

like image 625
Frank Marent Avatar asked Sep 24 '13 15:09

Frank Marent


1 Answers

Where is it generated?

If you look in app/code/core/Mage/Sales/Model/Order.php on around line 2052, you will find the following:

$this->setData('protect_code', substr(md5(uniqid(mt_rand(), true) . ':' . microtime(true)), 5, 6));

This is where protect_code is generated for the order (using a combination of md5, uniqid, and random integer.

What is it used for?

If you look in app/code/core/Mage/Sales/Helper/Guest.php and find the loadValidOrder function. You will see protect_code used in some areas to ensure the order being loaded is the correct one for the guest's cookie value.

It's also used in other areas, such as tracking information comparisons. You can see several instances of the getProtectCode() method being called in the Shipment models to compare the order to the tracking information. An example of a function that uses it is:

public function getTrackingInfoByTrackId()
{
    $track = Mage::getModel('sales/order_shipment_track')->load($this->getTrackId());
    if ($track->getId() && $this->getProtectCode() == $track->getProtectCode()) {
        $this->_trackingInfo = array(array($track->getNumberDetail()));
    }
    return $this->_trackingInfo;
}

As you can see with $this->getProtectCode() == $track->getProtectCode(), the tracking protect_code must match the Shipment protect_code.

like image 161
Axel Avatar answered Nov 05 '22 09:11

Axel