Reading Time: 7 minutes

If you were used to needing a Prestashop export products module to export products from your shop, you can finally drop it now. Prestashop 1.5 allows us to export products, and all sort of data, in handy CSV files without any third party module. How? Read on.

 

Prestashop version: 1.5.1

SQL Manager – What is it?

Prestashop export products with SQL Manager
Those of you who upgraded from 1.4 and spent some time in Prestashop 1.5′s back office, will have certainly noticed this new feature called SQL Manager. If you didn’t, it can be found under the Advanced Parameters tab.

Even though masked under a complex name this is an extremely usefult tool that finally lets Prestashop export products and all sort of data you can imagine. Yes, it’s a bit complicated, but we can finally export all kind of data we need and send it to, for example, price comparison sites or anything else. The cool thing is, we can export data in the exact way we want, choosing the exact stuff we need.

Yes, but how to? Well, to be honest, there is a reason why this Prestashop export tool has been called SQL Manager. In fact, you have to know a bit of SQL to retrieve the data you want, and this is an enormous obstacle to who doesn’t even understand a single line of html. No fear though! If you only want to export products, I am to provide a simple enough explanation on which query should be used to retrieve values, and which tables are usually storing valuable product data.

The downside – We can only export CSV

Yep, no xml export for retrieved data. But, I guess, since Prestashop only natively supports CSV Import, this has been done subsequently. Of course, this does not mean the system can’t be edited to allow xml export, too!
Furthermore, at this stage we can’t export images, and the sql query is limited to 1000 characters, which means something will need to be left off in most cases.

Export products: Prestashop SQL Manager example

In this example, I’ll be assuming we are exporting data for another Prestashop site, or as a backup for the same store. Therefore I’ll add most of the information needed by the Prestashop CSV Import tool in my csv. Let’s see which data is needed by going to Advanced Parameters -> CSV Import.

Prestashop Export Products - Available fields
A whole bunch of data we need, isn’t it? So, let’s start building our query keeping this pag as a reference. Back to the SQL manager, click Add new.

The request can be given a name, so that it can be stored and used again in the future. I’ll call it “Product Query”.

Step 1 – Product Table

Now, let’s start by grabbing some data from the ps_product table. Please note that “ps_” is the default prefix, and yours might be different. if this is your case, replace “ps_” with your prefix on every occurrence of a table.

1
2
3
"SELECT p.id_product, p.active, p.price, p.id_tax_rules_group, p.wholesale_price, p.reference, p.supplier_reference, p.id_supplier, p.id_manufacturer, p.upc, p.ecotax, p.weight, p.quantity, p.available_for_order, p.date_add, p.show_price, p.online_only, p.condition, p.id_shop_default
FROM ps_product p
GROUP BY p.id_product"

I’m using a group by statement to be sure there a re no duplicate products in the final result.

Finally, you’ll note I had to leave off the field EAN13, since, for some reason, the SQL manager is not accepting numbers in column names

This is all for the ps_product table. Of course, many things are still missing. Let’s deal with translatable fields. As you may know, Prestashop only allows us to import one language at time, so we will need to add a condition that tells the query to only look for a specific language. In this case, English has ID #1.

Step 2 – Let’s grab Translatable Fields

1
2
3
"SELECT p.id_product, p.active, <strong>pl.name,</strong> p.price, p.id_tax_rules_group, p.wholesale_price, p.reference, p.supplier_reference, p.id_supplier, p.id_manufacturer, p.upc, p.ecotax, p.weight, p.quantity, <strong>pl.description_short,</strong> <strong>pl.description,</strong> <strong>pl.meta_title,</strong> <strong>pl.meta_keywords,</strong> <strong>pl.meta_description,</strong> <strong>pl.link_rewrite,</strong> <strong>pl.available_now,</strong> <strong>pl.available_later,</strong> p.available_for_order, p.date_add, p.show_price, p.online_only, p.condition, p.id_shop_default
FROM ps_product p
<strong>LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product) WHERE pl.id_lang = 1</strong>"

So, we added a bunch of terms to look for (the ones in bold) and, after joining the ps_product_lang table, we set the language to be equal to 1, which, as i said, it’s English. Now, if at this point you find yourself rising a gun at your head because you don’t understand the code, you can google for mysql, or have a look at Net Tuts Plus to learn something about mySQL, queries, joins, and so. Of course, you can just copy/paste the end code if all you need is exporting products for a backup, or to another Prestashop store.

Step 3 – Dealing with categories

Let’s take one step further, things start becoming a bit complicated here

1
2
3
4
5
6
7
"SELECT p.id_product, p.active, pl.name, <strong>GROUP_CONCAT(DISTINCT(cl.name) SEPARATOR ",") as categories,</strong> p.price, p.id_tax_rules_group, p.wholesale_price, p.reference, p.supplier_reference, p.id_supplier, p.id_manufacturer, p.upc, p.ecotax, p.weight, p.quantity, pl.description_short, pl.description, pl.meta_title, pl.meta_keywords, pl.meta_description, pl.link_rewrite, pl.available_now, pl.available_later, p.available_for_order, p.date_add, p.show_price, p.online_only, p.condition, p.id_shop_default
FROM ps_product p
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
<strong>LEFT JOIN ps_category_product cp ON (p.id_product = cp.id_product) LEFT JOIN ps_category_lang cl ON (cp.id_category = cl.id_category)</strong>
WHERE pl.id_lang = 1
<strong>AND cl.id_lang = 1</strong>
GROUP BY p.id_product"

At this stage, I’m retrieving categories as a single value, separated by commas, since it’s the default separator for multiple values in the Prestashop CSV Import Tool. I also want to make sure I’m only selecting english category names.

Step 4 – Finishing touch

Finally, if you have multiple shops, don’t forget to add AND id_default_shop = 1 for each table which has multiple shops values. In this case, categories and products. Of course, since we didn’t join the category table before, we must do it now. Be sure to replace the id#1 of the shop i’ve set, to whatever is the one of the shop you’re exporting.

1
2
3
4
5
6
7
8
9
10
11
    "SELECT p.id_product, p.active, pl.name, GROUP_CONCAT(DISTINCT(cl.name) SEPARATOR ",") as categories, p.price, p.id_tax_rules_group, p.wholesale_price, p.reference, p.supplier_reference, p.id_supplier, p.id_manufacturer, p.upc, p.ecotax, p.weight, p.quantity, pl.description_short, pl.description, pl.meta_title, pl.meta_keywords, pl.meta_description, pl.link_rewrite, pl.available_now, pl.available_later, p.available_for_order, p.date_add, p.show_price, p.online_only, p.condition, p.id_shop_default
FROM ps_product p
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_category_product cp ON (p.id_product = cp.id_product)
LEFT JOIN ps_category_lang cl ON (cp.id_category = cl.id_category)
LEFT JOIN ps_category c ON (cp.id_category = c.id_category)
LEFT JOIN ps_product_tag pt ON (p.id_product = pt.id_product)
WHERE pl.id_lang = 1
AND cl.id_lang = 1
<strong>AND p.id_shop_default = 1 AND c.id_shop_default = 1</strong>
GROUP BY p.id_product"

Step 6 – What was not exported

At this stage, the query is complete. Yes, unfortunately not all of the things needed can be exported using a single query. For example, images, which are dinamically generated, can’t be exported with a single query, since there is no url stored in the database. Of course, if you know a way to do this, please, let me know, and I will definitely upgrade the guide. But at any rate, for some weird reason the SQL can only be 1000 chars long, (that’s way i also had to leave off tags), and therefore something must probably be removed in order to add images in any way.

Also, Prestashop allows us to apply multiple discounts to a single product, isn’t it? Yeah. Then why is it allowing us to only import one discount per product? I’ve yet to understand this. Due to this limitation, I’m not going to export discounts (it would be useless to export something incomplete).

Lastly, note that I am leaving out “delete existing image” since there is no place where to retrieve it from. Of course, you should set it to 0 after downloading the csv, or just ignore the field when importing.

Now, to export the CSV, save, and back to the SQL Manager page click the little icon as shown below. You can also take a look at the generated results by clicking the magnifier icon.

Prestashop: export products without any module – Final Source

1
2
3
4
5
6
7
8
9
10
11
12
    "SELECT p.id_product, p.active, pl.name, GROUP_CONCAT(DISTINCT(cl.name) SEPARATOR ",") as categories, p.price, p.id_tax_rules_group, p.wholesale_price, p.reference, p.supplier_reference, p.id_supplier, p.id_manufacturer, p.upc, p.ecotax, p.weight, p.quantity, pl.description_short, pl.description, pl.meta_title, pl.meta_keywords, pl.meta_description, pl.link_rewrite, pl.available_now, pl.available_later, p.available_for_order, p.date_add, p.show_price, p.online_only, p.condition, p.id_shop_default
FROM ps_product p
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_category_product cp ON (p.id_product = cp.id_product)
LEFT JOIN ps_category_lang cl ON (cp.id_category = cl.id_category)
LEFT JOIN ps_category c ON (cp.id_category = c.id_category)
LEFT JOIN ps_product_tag pt ON (p.id_product = pt.id_product)
WHERE pl.id_lang = 1
AND cl.id_lang = 1
AND p.id_shop_default = 1
AND c.id_shop_default = 1
GROUP BY p.id_product"

And this is everything on how to export products in Prestashop without the use of any module. It has some hugelimitations, but at least it’s free. As always, if you have questions, feel free to contact me, and I’ll try to clarify your doubts! And, if you want to learn something more on how to import products using CSV files, you might want to check out my tutorial: Importing data into your store: Prestashop Import CSV Tool

Users’ contributions

Here is a nice addition on how to export tags, by Yoann Masrevery.

1
2
3
4
5
6
7
8
SELECT ps_product.id_product, GROUP_CONCAT( DISTINCT (
ps_tag.name
)
SEPARATOR ”,” ) AS tags
FROM ps_product
LEFT JOIN ps_product_tag ON ( ps_product.id_product = ps_product_tag.id_product )
LEFT JOIN ps_tag ON ( ps_product_tag.id_tag = ps_tag.id_tag )
GROUP BY ps_product.id_product

 

 

 

 

via http://nemops.com/