VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: borro on September 29, 2016, 16:25:06 PM

Title: How to get the main product image url correctly and unambiguously?
Post by: borro on September 29, 2016, 16:25:06 PM
Hello!

Please tell how to get the main product image url in database query or in PHP uniquely? What is the criteria to select it correctly and unambiguously? VM 3
Title: Re: How to get the main product image url correctly and unambiguously?
Post by: PRO on September 29, 2016, 19:24:14 PM
On product page
<?php echo $this->product->images[0]->file_url ;?>
On category page
<?php echo $product->images[0]->file_url ;?>

select it from DB (by product id)

<?php function pmedia($pid){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__virtuemart_product_medias'));
$query->where('ordering=1');
$query->where($db->quoteName('virtuemart_product_id')." = ".$db->quote($pid) );
$db->setQuery($query);
$row = $db->loadAssoc();
$mid=$row['virtuemart_media_id'];
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__virtuemart_medias'));
$query->where($db->quoteName('virtuemart_media_id')." = ".$db->quote($mid));
$db->setQuery($query);
$row2 =$db->loadAssoc();
$image=$row2['file_url'];
return $image ;
}
?>

<?php echo pmedia(PRODUCT-ID-HERE);?>
Title: Re: How to get the main product image url correctly and unambiguously?
Post by: borro on September 30, 2016, 09:07:19 AM
Thank you!

I've found that in my db the combination of virtuemart_product_id and ordering in a virtuemart_product_medias table is not unique. I suggest development team to create the unique index on that fields combination in future if it's not already done
Title: Re: How to get the main product image url correctly and unambiguously?
Post by: GJC Web Design on September 30, 2016, 10:59:59 AM
but that is what the ordering is for....
Title: Re: How to get the main product image url correctly and unambiguously?
Post by: Milbo on September 30, 2016, 11:01:46 AM
and never do this
echo $this->product->images[0]->file_url_thumb

use the getFileUrlThumb function instead. The best teacher is anyway the code.
Title: Re: How to get the main product image url correctly and unambiguously?
Post by: Milbo on September 30, 2016, 11:03:55 AM
Quote from: borro on September 30, 2016, 09:07:19 AM
Thank you!

I've found that in my db the combination of virtuemart_product_id and ordering in a virtuemart_product_medias table is not unique. I suggest development team to create the unique index on that fields combination in future if it's not already done

Why must this be unique?