Jump to content

Get product image URL via API. PS 1.7.7.2


tbutron12
 Share

Recommended Posts

Hello,

I want to obtain product image url.

The endpoint I have found returns it only on authenticated requests. I need an public url to avoid redundant saves on my server.

 

Exemplary public link aka img src:

https://MY-DOMAIN.com/7-large_default/mug-the-adventure-begins.jpg

 

I can also scrap for this link on website, but firstly, I would need to get the product url via API by passing product_id.

 

Any ideas on that?

Thanks

Share this post


Link to post
Share on other sites

Find protected $webserviceParameters in ./classes/Product.php

There you can add your own parameter or edit an existing parameter.

Now it returns the image id.

You also have a function there:

public function getWsImages()
    {
        return Db::getInstance()->executeS('
            SELECT i.`id_image` as id
            FROM `'._DB_PREFIX_.'image` i
            '.Shop::addSqlAssociation('image', 'i').'
            WHERE i.`id_product` = '.(int)$this->id.'
            ORDER BY i.`position`');
    }

You can modify this function and add your own parameter.

E.g.

public function getWsImages()
{
	
	$get_images = Db::getInstance()->executeS('
		SELECT i.`id_image` as id, i.id_product
		FROM `'._DB_PREFIX_.'image` i
		'.Shop::addSqlAssociation('image', 'i').'
		ORDER BY i.`position`');
		
	$custom_ws = array();
	
	foreach ($get_images as $images){
		$protocol_link = Configuration::get('PS_SSL_ENABLED') || Tools::usingSecureMode() ? 'https://' : 'http://';
		$use_ssl = Configuration::get('PS_SSL_ENABLED') || Tools::usingSecureMode() ? true : false;
		$protocol_content = $use_ssl ? 'https://' : 'http://';
		$id_product = $images['id_product'];
		$image = Image::getCover($id_product);
		$product = new Product($id_product, false, Context::getContext()->language->id);
		$link = new Link($protocol_link, $protocol_content);
		$image_link = $link->getImageLink($product->link_rewrite, $image['id_image'], 'large_default');
		$custom_ws[] = array('id' => $images['id'], 'image_url' => $image_link); 
	}
	
	
	return $custom_ws;
}

 

and $webserviceParameters:

'images' => array(
                'resource' => 'image',
                'fields' => array('id' => array())
            ),

 

changed to:

'images' => array(
                'resource' => 'image',
                'fields' => array('id' => array(), 'image_url' => array())
            ),

 

Share this post


Link to post
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More