Forums

put virtuemart product details into form fields

konsument 03 Dec, 2010
Hi Folks,

in virtuemart theres a "ask question" form which is pretty nice but unfortunately not scaleable.

Now I want to insert some virtuemart product informations into hidden fields and submit them with an own "question-form" just like the virtuemart form does.

I can get the product or category id eg with an url like this:

index.php?option=com_chronocontact&chronoformname=myform&product_id=1&category_id=1


So how can I get the right virtuemart fields from the db to insert the values of the product id into my form? Or isnt this possible?

Help would be highly appreciated!
GreyHead 03 Dec, 2010
Hi konsument ,

I don't use VirtueMart but I think this has been answered before if you search the forums.

You'd need to write a MySQL query to get the information you need from the VirtueMart database tables. I have no idea what these tables are though.

Bob
konsument 03 Dec, 2010
Hi Bob,

unfortunately Ive found nothing except threads with unsolved questions.

However, after playing around Ive finally got it work:


function getProductTitle() {
$id = $_GET["product_id"];
$db = & JFactory::getDBO();
   $query = 'SELECT product_name FROM #__vm_product  ' . ' WHERE product_id = ' . (int) $id;
   $db->setQuery($query);
   $product_name = $db->loadResult(); 
   echo $product_name;
}
function getProductDesc() {
$id = $_GET["product_id"];
$db = & JFactory::getDBO();
   $query = 'SELECT product_s_desc FROM #__vm_product  ' . ' WHERE product_id = ' . (int) $id;
   $db->setQuery($query);
   $product_s_desc = $db->loadResult(); 
   echo $product_s_desc;
}
function getProductThumb() {
$id = $_GET["product_id"];
$db = & JFactory::getDBO();
   $query = 'SELECT product_thumb_image FROM #__vm_product  ' . ' WHERE product_id = ' . (int) $id;
   $db->setQuery($query);
   $product_thumb_image = $db->loadResult(); 
   echo $product_thumb_image;
}
<input type="hidden" name="productTitle" value="<? getProductTitle(); ?>" />
<input type="hidden" name="productDesc" value="<? getProductDesc(); ?>" />
<input type="hidden" name="productThumb" value="<? getProductThumb(); ?>" />


Im pretty sure the way I did it is just a qucik and dirty solution - in fact Im not well experienced in php.

Maybe you or somebody could help me to shorten the method a bit - lets say to get an array or else? After this we could post it in the "How-to"-Section. There I can explain the other steps.
konsument 06 Dec, 2010
Hi again,

I finally reduced the function to get an array of all wanted product fields entered in the SQL-Query. To complete the "How-To" I will post the steps I did to reach my goal:

Im using VM 1.1.6 and ChronoForms 3.2

First of all I wanted to have the the modified "ask question" link also in my browse pages. For this Ive needed to get the product_id there.

Open your shop.browse.php located in administrator/components/com_virtuemart/html/

Almost at the end go and find

$products[$i]['product_flypage'] = $url;
$products[$i]['contact_product_id'] = $contact_product_id;
$products[$i]['product_thumb_image'] = $product_thumb_image;


and insert

// Get Product-ID in browsing pages
$contact_product_id = $db_browse->f("product_id");

$products[$i]['product_flypage'] = $url;
$products[$i]['contact_product_id'] = $contact_product_id;
$products[$i]['product_thumb_image'] = $product_thumb_image;


After done this you can use

<?php echo $contact_product_id ?>


In your detail pages you can use

<?php echo $product_id ?>


Now create a link to you form and put the product ID at the end of the url:

index.php?option=com_chronocontact&chronoformname=yourform&product_id=<?php echo $contact_product_id ?>


Example with SEF-Urls:

http://www.domain.com/link/to/your/form?product_id=<?php echo $contact_product_id ?>


To get the necessary informations you need from your product we need to connect the VM tables:

Go to your form in Backend and put these lines on top of your form code:

    <?php
    function getProduct($id) {
    $db = & JFactory::getDBO();
       $query = 'SELECT product_name,product_s_desc,product_thumb_image FROM #__vm_product  ' . ' WHERE product_id = ' . $id;
       $db->setQuery($query);
       $product_details = $db->loadRowList();
       return $product_details;
    }
    if(isset($_GET["product_id"])){
      $product = getProduct($_GET["product_id"]);
      $product_name = $product['0']['0'];
      $product_s_desc = $product['0']['1'];
      $product_thumb_image = $product['0']['2'];
    }
    ?>


If you have HTML-Tags within your product_s_desc or product_desc and dont need them in your E-Mail template you must strip them before the output or you will get problems in your input values:

echo strip_tags($product_s_desc);


Then fill the inputs with your vars:

<input type="hidden" name="productTitle" value="<? echo $product_name; ?>" />
<input type="hidden" name="productDesc" value="<? echo $product_s_desc; ?>" />
<input type="hidden" name="productThumb" value="<? echo $product_thumb_image; ?>" />


If you need a dynamic subject just like "I have a question to ..." just create a new hidden field

<input type="hidden" name="subject" value="I have a question to <? echo $product_name; ?>" />


and use the name of the field in your dynamic subject field.

Have fun🙂

Maybe one Admin can move this post to the "How-To-Section"?
This topic is locked and no more replies can be posted.