How to get the main product image url correctly and unambiguously?

Started by borro, September 29, 2016, 16:25:06 PM

Previous topic - Next topic

borro

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
Wish you happiness!

PRO

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);?>

borro

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
Wish you happiness!

GJC Web Design

GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Milbo

and never do this
echo $this->product->images[0]->file_url_thumb

use the getFileUrlThumb function instead. The best teacher is anyway the code.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Milbo

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?
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/