繁体   English   中英

在prestashop中获取图片网址

[英]Get image url in prestashop

我需要为我的网站制作一个比较网站的Feed。 它必须是一个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代码还是帮助我?

谢谢!

很简单,从您的查询替换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')作为product_image,

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

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

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

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

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

嗯,我使用这个: concat('http://', ifnull(conf.value,'example.com'), '/img/c/', c.id_category, '.jpg') as url_image,

它完美无缺。

图像路径基于图像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

使用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"
   }

这里$ 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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM