我试图根据它的 ID 获取 prestashop 产品的图像 URL。
我正在为 prestashop 提供的方法尝试此代码,但我什么也没得到
<?php
//require('config/settings.inc.php');
//require('classes/Image.php');
require('classes/Link.php');
$ItemID=$_GET["catID"];
$db=mysql_connect(constant('_DB_SERVER_'),constant('_DB_USER_'), constant
$image = Image::getCover($ItemID);
$imagePath = Link::getImageLink($product->link_rewrite, $image['id_image'], 'home_default');
此代码不起作用,我尝试添加所需的类,但它们也产生了问题。此外,如果我评论了最后两行,则 require 本身会产生错误。
我计划手动构建图像 URL 表的其他工作代码正在运行
<?php
require('config/settings.inc.php');
$ItemID=$_GET["catID"];
$db=mysql_connect(constant('_DB_SERVER_'),constant('_DB_USER_'), constant
('_DB_PASSWD_')) or die('Could not connect');
mysql_select_db(constant('_DB_USER_'), $db) or die('');
if (is_numeric($ItemID)) {
$result = mysql_query("SELECT name,date_add,price , IFNULL(imageURL,'') as imageURL
FROM product inner join product_lang on product_lang.id_product=product.id_product
left join app_product_image on product.id_product=app_product_image.id_product where
id_lang=1 and product.id_product='$ItemID'") or die('Could not query');
$json = array();
if(mysql_num_rows($result)){
// $row=mysql_fetch_assoc($result);
while($row=mysql_fetch_row($result)){
// cast results to specific data types
//$test_data[]=$row;
$json['name']=$row[0];
$json['date']=$row[1];
$json['price']=$row[2];
$json['imgURL']=$row[3];
}
// $json['products']=$test_data;
}
echo json_encode($json);
mysql_close($db);
}
上面的代码是最后一个解决方案,但我试图不手动编写产品图像 URL!
最佳答案
这是经过测试的代码,我将此代码放在我的 prestashop 安装的根目录下的 test.php 中,它工作得很棒。
<?php
require_once dirname(__FILE__).'/config/config.inc.php';
require_once dirname(__FILE__).'/init.php';
$id_product = 1;//set your product ID here
$image = Image::getCover($id_product);
$product = new Product($id_product, false, Context::getContext()->language->id);
$link = new Link;//because getImageLInk is not static function
$imagePath = $link->getImageLink($product->link_rewrite, $image['id_image'], 'home_default');
echo $imagePath;
从您的代码中,您应该知道 getImageLink 不是静态函数,并且您的第一个代码段 $product 未使用任何产品进行初始化。
为了确认我已将此文件上传到我的服务器,您可以在此处查看:
Click here
关于Prestashop 图片网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051475/