Custom csv export from virtuemart

Submit to reddit

One of my clients needed a custom feed of his virtuemart products. I did a quick google check and nothing came up, so I decided to write my own. Please note that I’m not a programmer and I bet it can be done faster, better and more elegant, still this works, so …

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!--?php <br> // Please note those was developed for a Joomla 1.5 installation
// www.sgvulcan.com

$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password

$db_name = "database"; //database name

$wtxt='';
//database connection
$link = mysql_connect($host, $user, $pass);

mysql_select_db($db_name);

$myFile = "product_export.csv";
$fh = fopen($myFile, 'w');

$sqlstr = mysql_query("SELECT * FROM jos_vm_product");
if (mysql_numrows($sqlstr) != 0) {

while ($row = mysql_fetch_array($sqlstr)) {

// category id
$sql_cat_id = mysql_query("SELECT * FROM jos_vm_product_category_xref WHERE product_id = " .
$row['product_id']);
$cat_row = mysql_fetch_array($sql_cat_id);
fwrite($fh, """ . base64_encode($cat_row['category_id']) . """ . ";" );

// category name
$sql_catname_id = mysql_query("SELECT * FROM jos_vm_category WHERE category_id = " .
$cat_row['category_id']);
$catname_row = mysql_fetch_array($sql_catname_id);
fwrite($fh, """ . base64_encode($catname_row['category_name']) . """ . ";" );

// product name
fwrite($fh, """ . base64_encode($row['product_name']) . """ . ";" );

// product sku
fwrite($fh, """ . base64_encode($row['product_sku']) . """ . ";" );

// product short description
fwrite($fh, """ . base64_encode($row['product_s_desc']) . """ . ";" );

// full size photo

fwrite($fh, """ .
base64_encode("http://www.thewebsite.com/components/com_virtuemart/shop_image/product/" .
$row['product_full_image']) . """ . ";" );

// empty
fwrite($fh, """ . """ . ";" );

// empty
fwrite($fh, """ . """ . ";" );

// empty
fwrite($fh, """ . """ . ";" );

// empty
fwrite($fh, """ . """ . ";rn" );

}

}

mysql_close($link);

$domain = $_SERVER['HTTP_HOST'];

$url = "http://" . $domain . "/folder/" . $myFile;

echo "File is available at ";
echo "n<a href="""">" . $url . "</a>";

?>

Note that the fields are base64 encoded as per this client’s requirements, but that can be easily removed and also the virtuemart table names are hardcoded, so that must be checked too.
You can find the code on GitHub .
Customize it to your needs.

  • florin

    I tried this…and for me is not working

  • florin

    can u be more specific how it works….

  • You set the database, user and password. When run, it creates a product_export.csv file in the current folder. Now, this file is customized per this clients requirements, but you can easily change the code to generate whatever format you’d like.

  • With some minor changes, this still works. Thanks

  • With some minor changes, this still works. Thanks