在prestashop中获取图片url

问题描述 投票:0回答:8

我需要为我的网站制作一个供比较网站的提要。它必须是一条sql语句。 现在我有这个:

select  pl.name as Titel,
    ROUND(p.price*1.21,2) as Price,
    replace(concat('http://', ifnull(conf.value,'domain/'), cl.name, '/', p.id_product, '-' , pl.name, '.html'),' ','-') as Link,
     concat('http://', ifnull(conf.value,'domain'), '/img/p/', p.id_product, '-' , pi.id_image, '.jpg') as "Image-location",
     cl.name as Categorie,
    p.id_product AS ID
from dbrb_product p
left join dbrb_image pi on p.id_product = pi.id_product
left join dbrb_product_lang pl on p.id_product = pl.id_product
left join dbrb_category_lang cl on p.id_category_default = cl.id_category
left join dbrb_configuration conf on conf.name = 'dbrb_SHOP_DOMAIN'
left join dbrb_product_carrier x on p.id_product = x.id_product
group by p.id_product

但是现在有了新的 prestashop 1.6 版本,该图像不再起作用。

现在图片路径为:domain.com/img/p/number/number/number/image.png 我不明白其中的逻辑,有人可以告诉我吗?

还有一个我必须处理的问题,因为有些产品具有相同的图像。

有人可以完成 SQL 代码或进一步帮助我吗?

谢谢!

php sql prestashop
8个回答
6
投票

以下是获取所有可能的产品详细信息(包括图像的正确网址)的 SQL 代码:

SELECT p.id_product AS 'ID',
pl.id_lang AS 'ID_LANG',
p.active AS 'Active (0/1)',
pl.name AS 'Name',
p.id_category_default AS 'Default Category',
p.price AS 'Price tax excl.',
p.id_tax_rules_group AS 'Tax rules ID',
p.wholesale_price AS 'Wholesale price',
p.on_sale AS 'On sale (0/1)',
p.reference AS 'Reference #',
p.quantity AS 'Quantity',
pl.description_short AS 'Short description',
pl.description AS 'Description',
pl.meta_title AS 'Meta-title',
pl.meta_keywords AS 'Meta-keywords',
pl.meta_description AS 'Meta-description',
pl.link_rewrite AS 'URL rewritten',
pl.available_now AS 'Text when in stock',
pl.available_later AS 'Text when backorder allowed',
p.available_for_order AS 'Available for order',
p.date_add AS 'Product creation date',
p.show_price AS 'Show price',
p.online_only AS 'Available online only',
p.condition AS 'Condition',
concat( 'http://YOUR-URL.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 url_image
FROM ps_product p
INNER JOIN ps_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN ps_image im ON p.id_product = im.id_product
WHERE 1=1
and p.active = 1

3
投票

很简单,将查询中的 concat 替换为这个:

concat('http://', ifnull(conf.value,'example.com'), '/img/p/',SUBSTRING(pi.id_image from -4 FOR 1),'/',SUBSTRING(pi .id_image from -3 FOR 1),'/',SUBSTRING(pi.id_image from -2 FOR 1),'/',SUBSTRING(pi.id_image from -1 FOR 1),'/' , pi.id_image, ' .jpg') 作为产品图像,


2
投票

图像路径中的数字是其 ID 的数字,例如 ID 为 121 的图像将具有以下路径:

http://domain.com/img/p/1/2/1/121.jpg

但是,MySQL 没有任何内置函数来执行此操作(据我所知),因此您需要构建一个用户定义的函数。


1
投票

嗯,我正在使用这个:

concat('http://', ifnull(conf.value,'example.com'), '/img/c/', c.id_category, '.jpg') as url_image,

它工作完美。


1
投票

图像路径基于图像id。您分割所有数字并在它们之间添加斜杠以获取存储图像的文件夹。

-- build the image path
CONCAT('http://',
        -- get the shop domain
        IFNULL(conf.value, 'undefined_domain'),
        -- the path to the pictures folder
        '/img/p/',
        -- now take all the digits separetly as MySQL doesn't support loops in SELECT statements
        -- assuming we have smaller image id than 100'000 ;)
        IF(CHAR_LENGTH(pi.id_image) >= 5, 
            -- if we have 5 digits for the image id
            CONCAT(
                -- take the first digit
                SUBSTRING(pi.id_image, -5, 1),
                -- add a slash
                '/'),
            ''),
        -- repeat for the next digits
        IF(CHAR_LENGTH(pi.id_image) >= 4, CONCAT(SUBSTRING(pi.id_image, -4, 1), '/'), ''),
        IF(CHAR_LENGTH(pi.id_image) >= 3, CONCAT(SUBSTRING(pi.id_image, -3, 1), '/'), ''),
        if(CHAR_LENGTH(pi.id_image) >= 2, CONCAT(SUBSTRING(pi.id_image, -2, 1), '/'), ''),
        IF(CHAR_LENGTH(pi.id_image) >= 1, CONCAT(SUBSTRING(pi.id_image, -1, 1), '/'), ''),
        -- add the image id
        pi.id_image,
        -- put the image extension
        '.jpg') as image_url

0
投票

使用prestashop类“IMAGE”而不是用函数加载MySql

public static function getImgFolderStatic($id_image)

返回新文件系统中包含图像的文件夹的路径

它将图像的编号剥离到 prestashop 使用的子文件夹中。

示例:

foreach($SQLresult as $key=>$value)
   {
      $imageUrl=_PS_PROD_IMG_DIR_.Image::getImgFolderStatic($value['id_image']).$value['id_image'].".jpg"
   }

0
投票

这里 $productID 是您的产品 ID :)

$prod = new Product($productID);
$imgArray = $prod->getImages('1');
if (count($imgArray)>0) {
    $imgID = $imgArray[0]["id_image"];
    $imageUrl=_THEME_PROD_DIR_.Image::getImgFolderStatic($imgID).$imgID.".jpg";
}

0
投票

基于 SunnyBoy 之前的答案,我写了这个,它还生成了产品的友好 URL(我已经使用 PrestaShop 8.1.1 对其进行了测试):

SELECT 
    p.id_product AS 'ID',
    pl.id_lang AS 'ID_LANG',
    p.active AS 'Active (0/1)',
    pl.name AS 'Name',
    p.id_category_default AS 'category_id',
    cl.link_rewrite AS 'category_urlslug',
    concat( 'https://example.com/', cl.link_rewrite, '/', p.id_product, '-', pl.link_rewrite, '.html') AS 'product_url',
    p.price AS 'Price tax excl.',
    p.id_tax_rules_group AS 'Tax rules ID',
    p.wholesale_price AS 'Wholesale price',
    p.on_sale AS 'On sale (0/1)',
    p.reference AS 'Reference #',
    p.quantity AS 'Quantity',
    pl.description_short AS 'Short description',
    pl.description AS 'Description',
    pl.meta_title AS 'Meta-title',
    pl.meta_keywords AS 'Meta-keywords',
    pl.meta_description AS 'Meta-description',
    pl.link_rewrite AS 'URL rewritten',
    pl.available_now AS 'Text when in stock',
    pl.available_later AS 'Text when backorder allowed',
    p.available_for_order AS 'Available for order',
    p.date_add AS 'Product creation date',
    p.show_price AS 'Show price',
    p.online_only AS 'Available online only',
    p.condition AS 'Condition',
    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
and p.active = 1;
© www.soinside.com 2019 - 2024. All rights reserved.