Get product image path

  • Posts: 1119
  • Thank you received: 114
5 years 5 months ago #307472

Hi,

How would i get product image in custom module. Doing like this:

<?php
$productClass = hikashop_get('class.product');
$product = $productClass->get($id);
var_dump($product);
?>

Doesn't return image at all inside product variable.

The only way i could get it, was using sql query like this:
$db = JFactory::getDbo();
$sql = 'SELECT file_path FROM '.hikashop_table('file').' WHERE file_type="product" AND file_ref_id='.$id;
$db->setQuery($sql);
$path = $db->loadObjectList();

But i think there should be a better way doing this with hikashop code it self??

Please advise.

Thank you

Please Log in or Create an account to join the conversation.

  • Posts: 82868
  • Thank you received: 13375
  • MODERATOR
5 years 5 months ago #307474

Hi,

If you do this, you'll get the images data preloaded in $product:

<?php
$productClass = hikashop_get('class.product');
$product = $productClass->getProduct($id);
var_dump($product);
?>
Then, if you want the URL of the image it's really easy:
<?php
foreach($product->images as $image) {
 echo JURI::base(true).'images/com_hikashop/upload/'.$image->file_path;
}
?>
And you can replace images/com_hikashop/upload/ with the path you actually configured in the "upload folder" setting of the HikaShop configuration if you changed the default one.

The following user(s) said Thank You: kyratn

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
5 years 5 months ago #307494

Hi,

Well that's strange. Doing var_dump($product) i couldn't see image or file path at all... Now it works.

Thank you

Please Log in or Create an account to join the conversation.

  • Posts: 82868
  • Thank you received: 13375
  • MODERATOR
5 years 5 months ago #307495

Hi,

The code you gave in your first message was using the "get" function of class.product which doesn't load the images. The code I gave you uses the "getProduct" function which also load the images.

The following user(s) said Thank You: kyratn

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
5 years 5 months ago #307535

Hi,

Thank you Nicolas. I haven't noticed it :)

Would be great if you could improve development documantation under code samples. Because sometimes it's hard to find some code samples even via forum.

Thank you

The following user(s) said Thank You: nicolas

Please Log in or Create an account to join the conversation.

Time to create page: 0.064 seconds
Powered by Kunena Forum