[TIP] CSV export of order details on PrestaShop and Thirtybees

Photo by Cookie the Pom on Unsplash

Context

We want to export the entire order data from the store into a CSV file without using an expensive module.

Solution

To successfully do this, we will take advantage of a solution implemented in PrestaShop 1.6, 1.7 and Thirtybees, namely, the SQL query manager.

For those who have already lost their footing by reading the terms CSV and SQL I invite you to read some explanations available on the Internet:

Next, I'll give you a working solution, but understanding what is being copied seems essential to be able to adapt to your needs.

The query

SELECT
   d.id_order,
   o.date_add,
   CONCAT_WS(' ', g.firstname, g.lastname) AS customer,
   g.email,
   os.name AS state,
   d.product_name,
   d.product_reference,
   d.product_quantity,
   d.product_price,
   o.payment,
   c.name AS carrier_name,
   CONCAT_WS(' ', a.lastname, a.firstname, a.address1, a.address2, a.postcode, a.city) AS address_delivery,
   CONCAT_WS(' ', a.lastname, a.firstname, a.address1, a.address2, a.postcode, a.city) AS address_invoice,
   REPLACE(IFNULL(GROUP_CONCAT(cd.value), ''), '"', '\'') AS customized_data 
FROM
   ps_order_detail d 
   LEFT JOIN ps_orders o ON (d.id_order = o.id_order) 
   LEFT JOIN ps_customer g ON (o.id_customer = g.id_customer) 
   LEFT JOIN ps_carrier c ON (o.id_carrier = c.id_carrier) 
   LEFT JOIN ps_order_state_lang os ON (o.current_state = os.id_order_state) 
   LEFT JOIN ps_address a ON (a.id_address = o.id_address_delivery) 
   LEFT JOIN ps_address ab ON (ab.id_address = o.id_address_invoice) 
   LEFT JOIN ps_customization cu ON (cu.id_cart = o.id_cart) 
   LEFT JOIN ps_customized_data cd ON (cd.id_customization = cu.id_customization) 
GROUP BY
   d.id_order 
ORDER BY
   d.id_order DESC

Comments