我有两个商店使用prestshop。我想从第一个导入产品url列表到第二个。
我可以通过使用http://example.com/api/products
访问产品列表我还可以使用
http://example.com/api/products/{ProductID}
通过这种方式,我可以访问所有产品数据,但我找不到产品URL。
是否有办法从prestshop检索产品URL ?
您可以从产品ID生成产品URL:
$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;
如果打开了友好URL,则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
在prestshop> 1.6中可以继续:
- 覆盖产品类, 重新定义定义模式和webserviceParameters模式
- 添加两个字段url
- 创建一个函数"getWsUrl()"返回你的产品的绝对url
工作完成了,下面是我的代码:
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类,并自动在prestshop中获取所有语言。
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文件的控制器文件中定义$your_product_id,然后从tpl文件中调用它,如下所示
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}