You are here:  » Export File Live Products


Export File Live Products

Submitted by johberg77 on Sun, 2013-05-26 22:49 in

I have now filtered all the products I want to show on my website.
How can I now export all the details of the products to one file (txt or csv)?

I would need at least product url (not affiliate url), mapped category, price and product title.

Thanks,

John

Submitted by support on Mon, 2013-05-27 09:11

Hi John,

Here's a minimal export.php script, upload and browse to from your Price Tapestry installation folder, e.g. /pt/export.php. The script uses unbuffered queries incase the result set is very large, and Content-Type: headers to force download of products.csv (based on code from PriceTapestry.com which has more export examples, including per-merchant etc.)

<?php
  
require("includes/common.php");
  
header("Content-Type: application/octet-stream");
  
header("Content-Disposition: attachment; filename=products.csv");
  print 
"name,minprice,productHREF,category\n";
  
$link mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword,TRUE);
  
mysql_select_db($config_databaseName,$link);
  
$sql "SELECT *,MIN(price) AS minPrice FROM `".$config_databaseTablePrefix."products` GROUP BY name";
  
$result mysql_query($sql,$link);
  while(
$row mysql_fetch_array($result,MYSQL_ASSOC))
  {
    print 
str_replace(","," ",$row["name"]).",";
    print 
$row["minPrice"].",";
    print 
"http://".$_SERVER["HTTP_HOST"].$config_baseHREF.tapestry_productHREF($row).",";
    print 
$row["category"]."\n";
  }
?>

The above will remove any commas from the product name just incase to avoid conflict with the CSV format.

Cheers,
David.
--
PriceTapestry.com

Submitted by johberg77 on Mon, 2013-05-27 12:30

Fantastic!

But the URLs in the file contains /pt//pt. How do I remove these, since they cause broken links?

John

Submitted by support on Mon, 2013-05-27 12:47

Ah right - for the plugin version of the product HREF's assuming that you are using the default /product/ prefix then $config_baseHREF can be dropped and replace with "/" from this line:

    print "http://".$_SERVER["HTTP_HOST"].$config_baseHREF.tapestry_productHREF($row).",";

...for example:

    print "http://".$_SERVER["HTTP_HOST"]."/".tapestry_productHREF($row).",";

Cheers,
David.
--
PriceTapestry.com

Submitted by johberg77 on Mon, 2013-05-27 13:03

Ah, almost.

The URLs now show //pt/. Should the export file be moved from/pt to somewhere else?

John

Submitted by support on Mon, 2013-05-27 13:41

Ah sorry John, it would need to re-create the WordPress version of the product HREF as tapestry_productHREF() include $config_baseHREF. Instead, REPLACE that line with:

    print "http://".$_SERVER["HTTP_HOST"]."/product/".urlencode(tapestry_hyphenate($product["normalised_name"])).".html";

(where "/product/" matches the value for Product Base HREF in your PriceTapestry.org settings)

Cheers,
David.
-
PriceTapestry.com

Submitted by johberg77 on Mon, 2013-05-27 16:25

Almost again,

I got now /product/.html so I am missing the /product/name-whatever-product.html.

I use "/product/" in the settings.

John

Submitted by support on Mon, 2013-05-27 17:14

Ah - $row not $product...!

    print "http://".$_SERVER["HTTP_HOST"]."/product/".urlencode(tapestry_hyphenate($row["normalised_name"])).".html";

Cheers,
David.
--
PriceTapestry.com

Submitted by johberg77 on Mon, 2013-05-27 17:38

Brilliant!

Thanks,

John

Submitted by johberg77 on Tue, 2013-05-28 13:53

Sorry,

Now I see that the category collumn in the file are empty and the file also contain All products from the feed and not just the ones I want to have live.
I also found some strange things happening with my live categories, but I'll create a new topic for that.

John

Submitted by support on Tue, 2013-05-28 15:11

Hello John,

I think the 2 are related, as the export query is generated using a summary query grouped by `name` you would only see a `category` value in the output if the non-summary row the database happened to select contained a category value. This could happen for example if

Merchant - Product Name - Category

Merchant 1 - Blue Widget - Widgets
Merchant 2 - Blue Widget - [No category]

I'll follow up to your new post about this...

Cheers,
David.
--
PriceTapestry.com