[Skip to main content](#vessel-content)

#   Hikashop Orders to Xero Invoices - My Working PHP Code

##

- **[GSW00d](/forum/user/13067-gsw00d "View GSW00d's Profile")**
- [![GSW00d's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/13067-gsw00d "View GSW00d's Profile") Topic Author
- [ Offline](/forum/user/13067-gsw00d)

Less More

- Posts: 42
- Thank you received: 2
-
-

11 years 6 months ago -  11 years 6 months ago [\#199472](#199472) by [GSW00d](/forum/user/13067-gsw00d "View GSW00d's Profile")  *[Hikashop Orders to Xero Invoices - My Working PHP Code](/forum/37-import-export-csv-xls/876532-hikashop-orders-to-xero-invoices-my-working-php-code#199472 "This is not strictly a bug, but rather intended for anyone wanting to use my code as a template from which to work. I've edited the backend order-&gt;export view.  I have some logic that is strictly for my own needs relating to courier waybills, currency, custom products, international shipping method names, admin fees etc. Please adapt to your own needs.  If any PHP programmers have any tips for better/efficient/elegant factoring, please let me know as I am still new to coding.  Regards, Bruce Atkinson")* was created by *[GSW00d](/forum/user/13067-gsw00d "View GSW00d's Profile")*

This is not strictly a bug, but rather intended for anyone wanting to use my code as a template from which to work. I've edited the backend order-&gt;export view.

I have some logic that is strictly for my own needs relating to courier waybills, currency, custom products, international shipping method names, admin fees etc. Please adapt to your own needs.

If any PHP programmers have any tips for better/efficient/elegant factoring, please let me know as I am still new to coding.

Regards,
Bruce Atkinson
Code:

&lt;?php /** * @package HikaShop for Joomla! * @version 2.3.5 * @author hikashop.com * @copyright (C) 2010-2015 HIKARI SOFTWARE. All rights reserved. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html */ defined('_JEXEC') or die('Restricted access'); ?&gt;&lt;?php while (ob_get_level() &gt; 1) ob_end_clean(); $config =&amp; hikashop_config(); $format = $config-&gt;get('export_format', 'csv'); $separator = $config-&gt;get('csv_separator', ';'); $force_quote = $config-&gt;get('csv_force_quote', 1); $export = hikashop_get('helper.spreadsheet'); date_default_timezone_set('Africa/Johannesburg'); $export-&gt;init($format, 'Hikashop Order Export ' . date('Y-m-d H:i'), $separator, $force_quote); if (!empty($this-&gt;orders)) { // // HEADER ROW // $headerRow = array(); $o = reset($this-&gt;orders); $o = null; // Add dynamic keys $headerRow[] = 'InventoryItemCode'; $headerRow[] = 'Description'; $headerRow[] = 'Quantity'; $headerRow[] = 'UnitAmount'; $headerRow[] = 'AccountCode'; $headerRow[] = 'TaxType'; // Add static keys $headerRow[] = 'ContactName'; $headerRow[] = 'EmailAddress'; $headerRow[] = 'POAddressLine1'; $headerRow[] = 'POAddressLine2'; $headerRow[] = 'POAddressLine3'; $headerRow[] = 'POAddressLine4'; $headerRow[] = 'POCity'; $headerRow[] = 'POPostalCode'; $headerRow[] = 'POCountry'; $headerRow[] = 'InvoiceNumber'; $headerRow[] = 'Reference'; $headerRow[] = 'InvoiceDate'; $headerRow[] = 'DueDate'; $headerRow[] = 'Currency'; // // WRITE ROW // $export-&gt;writeLine($headerRow); // // PROCESS ORDERS // function compileContentLine(&amp;$static, &amp;$variable) { // set dynamic key values $contentRow[InventoryItemCode] = $variable[InventoryItemCode]; $contentRow[Description] = $variable[Description]; $contentRow[Quantity] = $variable[Quantity]; $contentRow[UnitAmount] = $variable[UnitAmount]; $contentRow[AccountCode] = $variable[AccountCode]; $contentRow[TaxType] = $variable[TaxType]; // set static key values $contentRow[ContactName] = $static[ContactName]; $contentRow[EmailAddress] = $static[EmailAddress]; $contentRow[POAddressLine1] = $static[POAddressLine1]; $contentRow[POAddressLine2] = $static[POAddressLine2]; $contentRow[POAddressLine3] = $static[POAddressLine3]; $contentRow[POAddressLine4] = $static[POAddressLine4]; $contentRow[POCity] = $static[POCity]; $contentRow[POPostalCode] = $static[POPostalCode]; $contentRow[POCountry] = $static[POCountry]; $contentRow[InvoiceNumber] = $static[InvoiceNumber]; $contentRow[Reference] = $static[Reference]; $contentRow[InvoiceDate] = $static[InvoiceDate]; $contentRow[DueDate] = $static[DueDate]; $contentRow[Currency] = $static[Currency]; return $contentRow; } function checkIfInternational($ShippingName) { if (strpos($ShippingName, 'International Courier') !== false) { $variable[AccountCode] = 260; $variable[TaxType] = 'Tax Exempt'; } else { $variable[AccountCode] = 200; $variable[TaxType] = 'Tax on Sales'; } return $variable; } foreach ($this-&gt;orders as $orderRow) { // skip if order is "processed" if ($orderRow-&gt;order_status === 'processed') { continue; } else { // // STATIC VALUES // $static[ContactName] = $orderRow-&gt;billing_address_firstname . ' ' . $orderRow-&gt;billing_address_lastname; $static[EmailAddress] = $orderRow-&gt;user_email; $static[POAddressLine1] = $orderRow-&gt;billing_address_street; $static[POAddressLine2] = $orderRow-&gt;billing_address_state . ', ' . $orderRow-&gt;billing_address_post_code . ', ' . $orderRow-&gt;billing_address_country; $static[POAddressLine3] = ' ~ SHIP TO: ' . strtoupper($orderRow-&gt;shipping_address_firstname) . ' ' . strtoupper($orderRow-&gt;shipping_address_lastname) . ', ' . $orderRow-&gt;shipping_address_telephone . ($orderRow-&gt;shipping_address_telephone2 == NULL ? '' : ' OR ' . $orderRow-&gt;shipping_address_telephone2); $static[POAddressLine4] = strtoupper($orderRow-&gt;shipping_address_street); $static[POCity] = $orderRow-&gt;shipping_address_state; $static[POPostalCode] = $orderRow-&gt;shipping_address_post_code; $static[POCountry] = $orderRow-&gt;shipping_address_country; $static[InvoiceNumber] = 'OIK' . $orderRow-&gt;order_number; // prepare order weight for reference $totalProducts = count($orderRow-&gt;products); $totalWeight = 0; for ($i = 1; $i &lt;= $totalProducts; $i++) { $orderProduct = $orderRow-&gt;products[$i - 1]; $hikaProductClass = hikashop_get('class.product'); // get hikashop product class $hikaProduct = $hikaProductClass-&gt;get($orderProduct-&gt;product_id); $totalWeight += $hikaProduct-&gt;product_weight * $orderProduct-&gt;order_product_quantity; } // prepare shipping class object and courier service required for reference $hikaShippingClass = hikashop_get('class.shipping'); $orderShipping = $hikaShippingClass-&gt;get($orderRow-&gt;order_shipping_id); if (strpos($orderShipping-&gt;shipping_name, 'Courier (L') !== false) { if ($totalWeight &lt; 1) { $serviceRequired = '&#124; Service: LOF &#124; '; } else { $serviceRequired = '&#124; Service: LOX &#124; '; } } elseif (strpos($orderShipping-&gt;shipping_name, 'International Courier') !== false) { $serviceRequired = '&#124; Aramex waybill no. 00000000000 &#124; '; } else { if ($totalWeight &lt; 2) { $serviceRequired = '&#124; Service: OVN &#124; '; } elseif ($totalWeight &lt; 6) { $serviceRequired = '&#124; Service: AIR &#124; '; } elseif ($totalWeight &gt;= 6) { $serviceRequired = '&#124; Service: ECO &#124; '; } else { $serviceRequired = '&#124; '; } } $orderNotes = trim($orderRow-&gt;notes); $orderNotes = ($orderNotes !== '') ? ' ' . $orderNotes : $orderNotes; $static[Reference] = $serviceRequired . 'Mass: ' . round($totalWeight, 2) . 'KG &#124; Pieces: 1' . ' &#124; Reference: order:0' . $orderRow-&gt;order_id . ' - ' . $orderRow-&gt;user_email . ' &#124; Parcel Description: Educational Text books &#124; Special Instructions: Please phone receiver before delivering.' . $orderNotes . ' &#124;'; // prepare order date format if (!empty($orderRow-&gt;order_created)) { $order_created_date = hikashop_getDate($orderRow-&gt;order_created, '%Y-%m-%d'); // Assign Hikashop getDate function result to variable $order_created_date = new DateTime($order_created_date); // Change variable to DateTime object $order_created_date = $order_created_date-&gt;format('d M Y'); // Change output format of datetime object to 01 Mon YYYY $orderRow-&gt;order_created = $order_created_date; // Assign this date to $orderRow array } if (!empty($orderRow-&gt;order_modified)) { $order_modified_date = hikashop_getDate($orderRow-&gt;order_modified, '%Y-%m-%d'); // Assign Hikashop getDate function result to variable $order_modified_date = new DateTime($order_modified_date); // Change variable to DateTime object $order_modified_date = $order_modified_date-&gt;format('d M Y'); // Change output format of datetime object to 01 Mon YYYY $orderRow-&gt;order_modified = $order_modified_date; // Assign this date to $orderRow array } $static[InvoiceDate] = $orderRow-&gt;order_created; $static[DueDate] = $orderRow-&gt;order_modified; $static[Currency] = 'ZAR'; // // PRODUCT ROWS // $TaxType = checkIfInternational($orderShipping-&gt;shipping_name); for ($i = 1; $i &lt;= $totalProducts; $i++) { // prepare dynamic key values $orderProduct = $orderRow-&gt;products[$i - 1]; $variable[InventoryItemCode] = $orderProduct-&gt;order_product_code; $variable[Description] = $orderProduct-&gt;order_product_name; $variable[Quantity] = $orderProduct-&gt;order_product_quantity; $variable[UnitAmount] = $orderProduct-&gt;order_product_price + $orderProduct-&gt;order_product_tax; // Check if children at risk if ($orderProduct-&gt;order_product_code === 'R10_donation_to_children_at_risk') { $variable[AccountCode] = 250; $variable[TaxType] = 'Tax Exempt'; } else { $variable[AccountCode] = $TaxType[AccountCode]; $variable[TaxType] = $TaxType[TaxType]; } $export-&gt;writeLine(compileContentLine($static, $variable)); } // // SHIPPING ROW // // prepare dynamic key values $variable[InventoryItemCode] = 'Shipping - ' . $orderShipping-&gt;shipping_name; $variable[Description] = $orderShipping-&gt;shipping_name; $variable[Quantity] = 1; $variable[UnitAmount] = $orderRow-&gt;order_shipping_price; // Check if international $variable[AccountCode] = $TaxType[AccountCode]; $variable[TaxType] = $TaxType[TaxType]; $export-&gt;writeLine(compileContentLine($static, $variable)); // // ADMIN FEE ROW // // prepare dynamic key values $variable[InventoryItemCode] = 'ADMIN-FEE'; $variable[Description] = 'Admin fee'; $variable[Quantity] = 1; $variable[UnitAmount] = $orderRow-&gt;order_payment_price; // Check if international $variable[AccountCode] = $TaxType[AccountCode]; $variable[TaxType] = $TaxType[TaxType]; $export-&gt;writeLine(compileContentLine($static, $variable)); } } } $export-&gt;send(); exit;

Last edit: 11 years 6 months ago by [GSW00d](/forum/user/13067-gsw00d "View GSW00d's Profile").

The following user(s) said Thank You: [Jerome](/forum/user/80-jerome "View Jerome's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzM3LWltcG9ydC1leHBvcnQtY3N2LXhscy84NzY1MzItaGlrYXNob3Atb3JkZXJzLXRvLXhlcm8taW52b2ljZXMtbXktd29ya2luZy1waHAtY29kZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[twhcreations](/forum/user/3186-twhcreations "View twhcreations's Profile")**
- [![twhcreations's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar3186.png)](/forum/user/3186-twhcreations "View twhcreations's Profile")
- [ Offline](/forum/user/3186-twhcreations)

Less More

- Posts: 127
- Thank you received: 8
-
-
- [](http://www.webtechplus.com.au)
-

7 years 9 months ago -  7 years 9 months ago [\#303400](#303400) by [twhcreations](/forum/user/3186-twhcreations "View twhcreations's Profile")  Replied by *[twhcreations](/forum/user/3186-twhcreations "View twhcreations's Profile")* on topic *[Hikashop Orders to Xero Invoices - My Working PHP Code](/forum/37-import-export-csv-xls/876532-hikashop-orders-to-xero-invoices-my-working-php-code#303400 "This is not strictly a bug, but rather intended for anyone wanting to use my code as a template from which to work. I've edited the backend order-&gt;export view.  I have some logic that is strictly for my own needs relating to courier waybills, currency, custom products, international shipping method names, admin fees etc. Please adapt to your own needs.  If any PHP programmers have any tips for better/efficient/elegant factoring, please let me know as I am still new to coding.  Regards, Bruce Atkinson")*

Bruce,
Did you get this to work and export then import a bunch of invoices into Xero?

Last edit: 7 years 9 months ago by [twhcreations](/forum/user/3186-twhcreations "View twhcreations's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzM3LWltcG9ydC1leHBvcnQtY3N2LXhscy84NzY1MzItaGlrYXNob3Atb3JkZXJzLXRvLXhlcm8taW52b2ljZXMtbXktd29ya2luZy1waHAtY29kZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.041 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
