Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get product URL using Prestashop API

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

http://example.com/api/products/{ProductID}

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 ?

like image 691
Fabien Papet Avatar asked Mar 25 '14 11:03

Fabien Papet


3 Answers

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.

like image 151
yenshirak Avatar answered Oct 04 '22 01:10

yenshirak


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

like image 37
Cyril N. Avatar answered Oct 04 '22 00:10

Cyril N.


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>
like image 37
Robin Delaporte Avatar answered Oct 04 '22 01:10

Robin Delaporte