Get product URL using Prestashop API
Asked Answered
L

7

6

I have two stores using Prestashop. I would like to import a products URLs list from the first to the second.

I can access to the product list by using http://example.com/api/products
I can also access to the product information by using

By this way I can access to all products data but I can't find product URL.

Is there a way to retrieve a product URL from Prestashop?

Laddy answered 25/3, 2014 at 11:36 Comment(0)
M
17

You can generate the product URL from the product ID:

$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;

If Friendly URL is turned on then the URL will be rewritten.

Merrygoround answered 25/3, 2014 at 18:8 Comment(2)
Thanks, it works fine but I have another question for you :) Is there a way to get rewritten Url ?Laddy
@Fabien $ch = curl_init($productUrl); curl_exec($ch); $rewrittenUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);Merrygoround
R
15

For those who are looking to generate the absolute url inside their store, you can do the following :

$product = new Product(Tools::getValue('id_product'));
$link = new Link();
$url = $link->getProductLink($product);

Will result with something like :

http://your.prestashop-website.com/fr/1-T-shirts-a-manches-courtes-delaves.html

Rattletrap answered 11/9, 2014 at 9:21 Comment(3)
Thanks for answer but no relevant as I want the URL from a distant storeLaddy
Oh yes, indeed. My bad.Rattletrap
Is there a way I can get friendy url from pages that are created under Preferences -> SEO & URLs based od ' $pageID ' ? Something like $page = new Page(Tools::getValue('page_id')).. ?Orten
S
3

In prestashop > 1.6 you can proceed :

  • Override product class,
  • Redefine the definition schema and the webserviceParameters schema
  • Add both fields "url"
  • create a function "getWsUrl()" that returns the absolute url of your product

And the job is done, here is my code:

 protected $webserviceParameters = array(
    'objectMethods' => array(
        'add' => 'addWs',
        'update' => 'updateWs'
    ),
    'objectNodeNames' => 'ProductForWs',
    'fields' => array(
        'id_default_image' => array(
            'getter' => 'getCoverWs',
            'setter' => 'setCoverWs',
            'xlink_resource' => array(
                'resourceName' => 'images',
                'subResourceName' => 'products'
            )
        )
    ),
    'associations' => array(
        'url' => array('resource' => 'url',
            'fields' => array(
                'url' => array('required' => true)
            ),
            'setter' => false
        )
    ),
);

public static $definition = array(
    'table' => 'product',
    'primary' => 'id_product',
    'fields' => array(
        'name' =>                        array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
        'url' =>                        array('type' => self::TYPE_STRING),
    'associations' => array(),
    ),
);

public function getWsUrl(){
    $link = new Link();
    $product = new Product($this->id);
    return array(array("url" => $link->getProductLink($product)));
}

The WebServiceOutputBuilder will call your function and return the url path as an association. Like:

<associations>
   <url nodeType="url" api="url">
     <url>
       <url>
         <![CDATA[ http://prestashop.dev/14-prod.html ]]>
       </url>
     </url>
   </url>
</associations>
Siege answered 8/7, 2016 at 7:59 Comment(0)
G
0

On PrestaShop™ 1.4.5.1 I use

public function getProductLink($id)
{
    global $cookie;
    $productUrl = "http://".$_SERVER['HTTP_HOST'].'/product.php?id_product=' . $id;
    return $productUrl;

}
Gentility answered 23/6, 2016 at 9:26 Comment(0)
A
0

In add to @robin-delaporte you can use this overriding Product class and automatically get for all languages in your prestashop.

protected $webserviceParameters = array(
    'objectMethods' => array(
        'add' => 'addWs',
        'update' => 'updateWs'
    ),
    'objectNodeNames' => 'ProductForWs',
    'fields' => array(
        'id_default_image' => array(
            'getter' => 'getCoverWs',
            'setter' => 'setCoverWs',
            'xlink_resource' => array(
                'resourceName' => 'images',
                'subResourceName' => 'products'
            )
        )
    ),
    'associations' => array(
        'url' => array(
            'resource' => 'url',
            'fields' => array(
                'url' => array()
            ),
            'setter' => false
        ),
    ),
);

public function getWsUrl(){
    $languages = Language::getLanguages(true, $this->context->shop->id);
    $link = new Link();
    $product = new Product($this->id);


    if (!count($languages))
        return array(array("url" => $link->getProductLink($product)));;

    $default_rewrite = array();
    $rewrite_infos = Product::getUrlRewriteInformations($this->id);
    foreach ($rewrite_infos as $infos)
        $default_rewrite[$infos['id_lang']] = $link->getProductLink($this->id, $infos['link_rewrite'], $infos['category_rewrite'], $infos['ean13'], (int)$infos['id_lang']);

    return $default_rewrite;
}
Arjuna answered 29/8, 2018 at 11:14 Comment(0)
M
0

Define $your_product_id in the controller file of the tpl file, then call it from the tpl file as follows

yourcontroller.php

public function hookTheHookYouWant()
    {
$set_product = $this->context->smarty->assign(array(
'product_id' => 27,
));
$set_product = $this->context->smarty->fetch($this->local_path.'views/templates/front/yourtplfile.tpl');
return $set_product;
    }
}

yourtplfile.tpl

{url entity='product' id=$product_id}
Martinson answered 1/7, 2021 at 21:23 Comment(0)
C
0

If you want to fetch the friendly URL for the product and an URL for the image through MySQL, you can do something like this (I tested it with PrestaShop 8.1.1):

SELECT
    concat('https://example.com/', cl.link_rewrite, '/', p.id_product, '-', pl.link_rewrite, '.html') AS 'product_url',
    concat('https://example.com/img/p/',mid(im.id_image,1,1),'/', if (length(im.id_image)>1,concat(mid(im.id_image,2,1),'/'),''),if (length(im.id_image)>2,concat(mid(im.id_image,3,1),'/'),''),if (length(im.id_image)>3,concat(mid(im.id_image,4,1),'/'),''),if (length(im.id_image)>4,concat(mid(im.id_image,5,1),'/'),''), im.id_image, '.jpg') AS 'image_url'
FROM ps_product p
    INNER JOIN ps_product_lang pl ON p.id_product = pl.id_product
    INNER JOIN ps_category_lang cl ON p.id_category_default = cl.id_category
    LEFT JOIN ps_image im ON p.id_product = im.id_product
WHERE p.active = 1;

NOTE: Remember to replace "https://example.com/" with the URL of your webshop :)

Calley answered 9/9, 2023 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.