我有两个使用Prestashop的商店。我想从第一个到第二个导入产品URL列表。
我可以使用http://example.com/api/products
来访问产品列表
我也可以使用
http://example.com/api/products/{ProductID}
通过这种方式,我可以访问所有产品数据,但我找不到产品网址。
有没有办法从Prestashop检索产品网址?
答案 0 :(得分:13)
您可以从产品ID生成产品网址:
$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;
如果启用了友好URL,则会重写URL。
答案 1 :(得分:13)
对于那些希望在商店内生成绝对网址的人,您可以执行以下操作:
$product = new Product(Tools::getValue('id_product'));
$link = new Link();
$url = $link->getProductLink($product);
会产生类似的结果:
http://your.prestashop-website.com/fr/1-T-shirts-a-manches-courtes-delaves.html
答案 2 :(得分:2)
在prestashop> 1.6你可以继续:
工作完成了,这是我的代码:
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)));
}
WebServiceOutputBuilder将调用您的函数并将url路径作为关联返回。喜欢:
<associations>
<url nodeType="url" api="url">
<url>
<url>
<![CDATA[ http://prestashop.dev/14-prod.html ]]>
</url>
</url>
</url>
</associations>
答案 3 :(得分:0)
在PrestaShop™1.4.5.1上 我用
public function getProductLink($id)
{
global $cookie;
$productUrl = "http://".$_SERVER['HTTP_HOST'].'/product.php?id_product=' . $id;
return $productUrl;
}
答案 4 :(得分:0)
除了添加到@ 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;
}
答案 5 :(得分:0)
在tpl文件的controller文件中定义$your_product_id,然后从tpl文件中调用如下
您的控制器.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}