A lot of online retails often need to export orders to third-party systems and services. They do it for inventory accounting, store houses, sales reports etc. There are plenty of such systems, and each of them has its own data transfer format. Despite the fact, that there are a lot of export modules on Prestashop Addons Marketplace, sometimes they are not appropriate, because each service has its own file format, its limitations, etc. So, that’s why there is a need to write your own script exports. Let’s consider the most popular file format: XML.
As an order is a set of several entities (the order itself, the customer, address, products, payment information, delivery, etc. ) we should configure the list of properties, which we are going to export.
To do this, we will create a special array of properties for each entity:
1 2 3 4 5 6 |
$properties = array( ‘order‘ => array(‘id_order‘, ‘referense‘, ‘total_paid‘), ‘customer‘ => array(‘id_customer‘, ‘firstname‘, ‘lastname‘), ‘addresses‘ => array(‘country‘, ‘city‘, ‘address1‘, ‘phone‘), ‘product‘ => array(‘product_id‘, ‘product_name‘, ‘product_quantity ‘, ‘total_price‘), ); |
For every entity you should prescribe necessary properties, which can be seen in every class of their array. All array classes are located in the folder /clases/
Then we create a method, which generates an xml string for set of properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
protected function _renderProperties($object, $fields) { $string = ''; foreach ($fields as $field) { if (property_exists($object, $field)) { if (is_string($object->{$field}) || is_float($object->{$field})) { $value = strval($object->{$field}); $string .= ' ' . $field . '="' . htmlspecialchars($value) . '"'; } } } return $string; } |
To get an array of orders, you should register a simple query:
1 |
$_orders = Db::getInstance()->ExecuteS(‘SELECT * FROM ‘ . _DB_PREFIX_ . ‘orders;‘); |
However, it is sometimes necessary to export not all orders in the store, but only the latest ones. Therefore we will export the last 100 orders:
1 |
$_orders = Db::getInstance()->ExecuteS(‘SELECT * FROM ‘ . _DB_PREFIX_ . ‘orders ORDER BY id_order DESC LIMIT 100;‘); |
Now we proceed to the formation of XML-document:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
$xml = ' <?xml version="1.0" encoding="UTF-8"?>' . chr(10); $xml = ' <orders>' . chr(10); foreach ($_orders as $_order) { // start the node of the order: $xml = '<order' . $this->_renderProperties($_order, $properties['order']) . '>'; // the node of customer information: $_customer = new Customer($_order->id_customer); $xml = '<customer' . $this->_renderProperties($_customer, $properties['customer']) . '/>'; // the node of address information of the delivery: $address_delivery = new Address($_order->id_address_delivery); $xml = '<address_delivery' . $this->_renderProperties($address_delivery, $properties['addresses']) . '><![CDATA[' . AddressFormat::generateAddress($address_delivery) . ']]></address_delivery>'; // the node of address information of the invoice: $address_invoice = new Address($_order->id_address_invoice); $xml = '<address_invoice' . $this->_renderProperties($address_invoice, $properties['addresses']) . '><![CDATA[' . AddressFormat::generateAddress($address_invoice) . ']]></address_invoice>'; // the node of order products : $xml = '<products>'; $products = $_order->getProducts(); foreach ($products as $product) { //add in XML all order products: $_product = new Product($product['id_product'], FALSE, Context::getContext()->language->id); $xml = '<product name="' . $_product->name . '"' . $this->_renderProperties((object)$product, $properties['product']) . '>'; // the node of selected attributes of the ordered product: $attributes = Product::getAttributesParams($product['id_product'], $product['product_attribute_id']); foreach ($attributes as $attribute) { $xml = '<attribute name="' . $attribute['group'] . '" value="' . $attribute['name'] . '" />'; } $xml = '</product>; } $xml = '</products>; $xml = '</order>'; } $xml = ' </orders>' . chr(10); |
Then we should register titles to generate the file and send it to download:
1 2 3 4 5 6 7 8 9 |
header("Expires: Thu, 19 Feb 1998 13:24:18 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Cache-Control: post-check=0,pre-check=0"); header("Cache-Control: max-age=0"); header("Pragma: no-cache"); header("Content-Type: text/xml"); header("Content-disposition: attachment; filename=\"Orders.xml\""); die($xml); |
All set, the file has been generated and downloaded.
hi
how to use this in prestashop 1.6