Get product image path

  • Posts: 992
  • Thank you received: 84
  • Hikashop Business
2 weeks 1 day 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: 66046
  • Thank you received: 9668
  • MODERATOR
2 weeks 1 day 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: 992
  • Thank you received: 84
  • Hikashop Business
2 weeks 14 hours 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: 66046
  • Thank you received: 9668
  • MODERATOR
2 weeks 5 hours 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: 992
  • Thank you received: 84
  • Hikashop Business
1 week 6 days 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.059 seconds
Powered by Kunena Forum