我有两家使用 Prestashop 的商店。我想从第一个到第二个导入产品 URL 列表。
我可以使用
http://example.com/api/products
http://example.com/api/products/{ProductID}
通过这种方式我可以访问所有产品数据,但找不到产品 URL。
有没有办法从 Prestashop 检索产品 URL?
您可以根据产品 ID 生成产品 URL:
$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;
如果打开友好 URL,则 URL 将被重写。
对于那些希望在其商店内生成绝对网址的人,您可以执行以下操作:
$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
在 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>
在 PrestaShop™ 1.4.5.1 上 我用
public function getProductLink($id)
{
global $cookie;
$productUrl = "http://".$_SERVER['HTTP_HOST'].'/product.php?id_product=' . $id;
return $productUrl;
}
除了 @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;
}
在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;
}
}
你的tpl文件.tpl
{url entity='product' id=$product_id}
如果你想通过MySQL获取产品的友好URL和图像的URL,你可以这样做(我用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;
注意:请记住将“https://example.com/”替换为您的网上商店的 URL :)