php 使用 Prestashop API 获取产品 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22633395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get product URL using Prestashop API
提问by Fabien Papet
I have two stores using Prestashop. I would like to import a products URLs list from the first to the second.
我有两家使用 Prestashop 的商店。我想从第一个到第二个导入产品 URL 列表。
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
我也可以通过使用访问产品信息
http://example.com/api/products/{ProductID}
http://example.com/api/products/{ProductID}
By this way I can access to all products data but I can't find product URL.
通过这种方式,我可以访问所有产品数据,但找不到产品 URL。
Is there a way to retrieve a product URL from Prestashop ?
有没有办法从 Prestashop 检索产品 URL?
回答by yenshirak
You can generate the product URL from the product ID:
您可以从产品 ID 生成产品 URL:
$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;
If Friendly URL is turned on then the URL will be rewritten.
如果打开友好 URL,则 URL 将被重写。
回答by Cyril N.
For those who are looking to generate the absolute url inside their store, you can do the following :
对于那些希望在其商店内生成绝对 url 的人,您可以执行以下操作:
$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
http://your.prestashop-website.com/fr/1-T-shirts-a-manches-courtes-delaves.html
回答by Robin Delaporte
In prestashop > 1.6 you can proceed :
在 prestashop > 1.6 中,您可以继续:
- Override productclass,
- Redefine the definitionschema and the webserviceParametersschema
- Add both fields "url"
- create a function "getWsUrl()" that returns the absolute url of your product
- 覆盖产品类,
- 重新定义定义架构和webserviceParameters架构
- 添加两个字段“ url”
- 创建一个函数“ getWsUrl()”,它返回您产品的绝对网址
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:
WebServiceOutputBuilder 将调用您的函数并返回 url 路径作为关联。喜欢:
<associations>
<url nodeType="url" api="url">
<url>
<url>
<![CDATA[ http://prestashop.dev/14-prod.html ]]>
</url>
</url>
</url>
</associations>
回答by Luis
In add to @robin-delaporte you can use this overriding Product class and automatically get for all languages in your prestashop.
除了@robin-delaporte,您还可以使用这个覆盖的 Product 类并自动获取您的 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;
}
回答by Elia Weiss
On PrestaShop? 1.4.5.1 I use
在 PrestaShop 上?1.4.5.1 我用
public function getProductLink($id)
{
global $cookie;
$productUrl = "http://".$_SERVER['HTTP_HOST'].'/product.php?id_product=' . $id;
return $productUrl;
}