Monday 23 August 2010

Virtuemart Related Product problem and hacked solution.

It seems to be a common problem with Virtuemart that when adding or editing products if you go to the 'related products' tab - then use the drop down list to search for the products you wish to add - then you get either nothing or an incomplete list. 

There's a few solutions to try on this post https://forum.virtuemart.net/index.php?topic=42558.msg135503   however none of these worked for me.  As I am on a limited budget for the projects I have quickly knocked together a tool that help me apply all the related products I needed quickly.  with extra time it would be prefferable to intergrate this tool into Joomla itself. If anyone wants to sponsor me to do so then let me know. Otherwise this was a five minute ugly fix ;)  

I have a folder which I used to run batch mysql tasks needed for when I transffered over a store from 'ruby on the rails' - In this folder i have a config.php file in the folder 'includes'  -

in here you'll need to add your details for connection

#################config.php#########################

//Database connection info
$hostname = "localhost";
$username = "mysqlusername";
$password = "mysqlpsswrd";
$dbName = "mysqldatabasename";
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
@mysql_select_db( "$dbName") or die( "Unable to select database");
 

####################################################



then I wrote the following page - that lets me update a product.  

############################add_related_products.php#############################

include ("includes/config.php");

if (isset($_POST['add_related_products']))
{

$product_id = $_POST['product_id'];
$related_products = $_POST['related_id1'];
$related_id2 = $_POST['related_id2'];
$related_id3 = $_POST['related_id3'];
$related_id4 = $_POST['related_id4'];

if ($related_id2 !=NULL) {

$related_products .= '|'.$related_id2;

}

if ($related_id3 !=NULL) {

$related_products .= '|'.$related_id3;

}

if ($related_id4 !=NULL) {

$related_products .= '|'.$related_id4;

}



$sql = "INSERT INTO jos_vm_product_relations (product_id, related_products) VALUES ('$product_id', '$related_products')";

$runsql = mysql_query($sql);



echo 'this sql statement has been run -
'.$sql.'

';


}
else {

echo 'Add related products to another product fill in the following details
<br> <BR>';
echo '
<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '
<input type="hidden" name="add_related_products" value="TRUE">';
echo '
<input type="text" name="product_id" value="product id"<br>';
echo '
<input type="text" name="related_id1" value="related id 1"<br>';
echo '
<input type="text" name="related_id2" value="related id 2"<br>';
echo '
<input type="text" name="related_id3" value="related id 3"<br>';
echo '
<input type="text" name="related_id4" value="related id 4"<br>';
echo '
<input type="submit" value="Submit">
</form>';
}






?>


and it does what i need it to, so i hope this helps someone else. 





No comments: