SQL查询以显示最新的订单信息,包括产品图片(Prestashop)

时间:2019-04-26 15:47:51

标签: mysql sql prestashop

使用来自Prestashop(1.6和1.7)的sql查询,我需要输出一个csv文件,该文件显示(x金额)我最新订单的详细信息。这必须包括订购的每个项目的详细信息以及每个订单的每个项目的产品图像的位置。

我“认为”我有sql的两个部分,但我不知道如何加入它们。

我(了解并)已经开始运行此部分

SELECT o.reference, c.firstname, c.lastname, a.lastname, a.firstname, a.address1, a.address2, a.postcode, a.city, a.phone, a.phone_mobile, o.id_order
FROM ps_orders o
LEFT JOIN ps_customer c on (c.id_customer = o.id_customer)
LEFT JOIN ps_address a on (a.id_address = o.id_address_delivery)
OUTER JOIN  (SELECT  *  from ps_order_detail where id_order = o.id_order limit 1) od1 on (o.id_order = o.id_order)

order by id_order desc limit 5

这-我在堆栈交换中借来的

select
p.`id_product`
,pl.`name`
,concat('http:/mydomain.com/',cl.`link_rewrite`,'/',pl.`link_rewrite`,'-',p.`id_product`,'.html') as "ProductURL"
,case
    when length(im.`id_image`)=6 then
     concat('http:/mydomain.com/img/p/',insert(insert(insert(insert(insert(im.`id_image`,2,0,'/'),4,0,'/'),6,0,'/'),8,0,'/'),10,0,'/'),'/',im.`id_image`,'.jpg')
    when length(im.`id_image`)=5 then
     concat('http:/mydomain.com/img/p/',insert(insert(insert(insert(im.`id_image`,2,0,'/'),4,0,'/'),6,0,'/'),8,0,'/'),'/',im.`id_image`,'.jpg')
    when length(im.`id_image`)=4 then
     concat('http:/mydomain.com/img/p/',insert(insert(insert(im.`id_image`,2,0,'/'),4,0,'/'),6,0,'/'),'/',im.`id_image`,'.jpg')
    when length(im.`id_image`)=3 then
     concat('http:/mydomain.com/img/p/',insert(insert(im.`id_image`,2,0,'/'),4,0,'/'),'/',im.`id_image`,'.jpg')
    when length(im.`id_image`)=2 then
     concat('http:/mydomain.com/img/p/',insert(im.`id_image`,2,0,'/'),'/',im.`id_image`,'.jpg')
    when length(im.`id_image`)=1 then
     concat('http:/mydomain.com/img/p/',insert(im.`id_image`,2,0,'/'),im.`id_image`,'.jpg')  

    else ''
    end as "ImgURL_1"
FROM `ps_product` p
join `ps_product_lang` pl on pl.`id_product`= p.`id_product`
join `ps_category_lang` cl on cl.`id_category`= p.`id_category_default`
join `ps_image` im on im.`id_product`= p.`id_product`
where pl.`id_lang`=1 and cl.`id_lang`=1 and im.`cover`=1 and p.`active`=1

enter image description here

基本上,我需要您在正常的Prestashop送货单上获得的所有信息,即客户名称,地址,产品名称等以及产品图片的位置。然后将其放入Adobe Indesign中以获取自定义的交货单。谢谢

2 个答案:

答案 0 :(得分:1)

此功能与PrestaShop 1.6 / 1.7配合使用,将生成包含您所需要的CSV文件,包括图像:

<?php

include('config/config.inc.php');
include('init.php');

$context = Context::getContext();
$id_lang = (int)Configuration::get('PS_LANG_DEFAULT', (int)$context->shop->id);

$order_details = Db::getInstance()->ExecuteS('
SELECT o.reference, c.firstname, c.lastname, a.lastname, a.firstname, a.address1, a.address2, a.postcode, s.name state, a.city, a.phone, a.phone_mobile, o.id_order,
od.product_id, od.product_name, od.product_quantity, od.product_reference, i.id_image, pl.link_rewrite
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'customer c on (c.id_customer = o.id_customer)
LEFT JOIN '._DB_PREFIX_.'address a on (a.id_address = o.id_address_delivery)
LEFT JOIN '._DB_PREFIX_.'state s on (a.id_state = s.id_state)
LEFT JOIN '._DB_PREFIX_.'order_detail od ON (od.id_order = o.id_order)
LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = od.product_id)
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = od.product_id)
WHERE o.valid = 1 AND i.cover = 1 AND pl.id_lang = '.(int)$id_lang.' AND pl.id_shop = '.(int)$context->shop->id.'
ORDER BY o.id_order DESC
LIMIT 5');

foreach ($order_details as &$od)
    $od['product_image'] = $context->link->getImageLink($od['link_rewrite'], $od['id_image'], 'home_default');

if (count($order_details))
{
    /* CSV Separator */
    $csv_sep = ',';
    $csv_delim = '"';

    /* CSV Header */
    $csv = '';
    foreach ($order_details[0] as $k => $val)
        $csv .= $k.$csv_sep;
    $csv = rtrim($csv, $csv_sep)."\n";

    /* Ordered products */
    foreach ($order_details as $line)
    {
        foreach ($line as $column)
            $csv .= $csv_delim.$column.$csv_delim.$csv_sep;
        $csv = rtrim($csv, $csv_sep)."\n";
    }

    /* Comment the two lines below if you'd just like to display instead of forcing download */
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="orders-'.date('YmdHis').'.csv"');
    echo $csv;
}

结果:

CSV preview

我做什么:

  • 添加了_DB_PREFIX_,确保它可以在其他PrestaShop商店中使用,这些商店的数据库表前缀可能不同于ps_
  • 添加了o.valid = 1,仅包括已付款和有效的订单
  • JOIN表上添加了image,以选择产品封面图片
  • 添加了州名(例如佛罗里达州,加利福尼亚州等)
  • 添加了产品名称,ID,参考和订购的数量
  • 使用内置的PrestaShop方法检索产品图片
  • 动态生成CSV文件

我希望这会有所帮助!

答案 1 :(得分:-1)

解决方案(包括一些有关客户订单的其他信息)

SET SQL_BIG_SELECTS=1;
 SELECT 
       o.reference          AS order_reference,
       c.firstname          AS customer_first_name,
       c.lastname,
       a.lastname,
       a.firstname,
       a.address1,
       a.address2,
       a.postcode,
       a.city,
       a.phone,
       a.phone_mobile,
       o.id_order,
       od1.product_id       AS Product1ID,
       prod1.name           AS Product1,
       prod1.url_image      AS Product1Image,
       prod1.ProductURL     AS Product1URL,
       od1.product_quantity AS Product1Quantity,
       od2.product_id       AS Product2ID,
       prod2.name           AS Product2,
       prod2.url_image      AS Product2Image,
       prod2.ProductURL     AS Product2URL,
       od2.product_quantity AS Product2Quantity,
       od3.product_id       AS Product3ID,
       prod3.name           AS Product3,
       prod3.url_image      AS Product3Image,
       prod3.ProductURL     AS Product3URL,
       od3.product_quantity AS Product3Quantity,
       od4.product_id       AS Product4ID,
       prod4.name           AS Product4,
       prod4.url_image      AS Product4Image,
       prod4.ProductURL     AS Product4URL,
       od4.product_quantity AS Product4Quantity,
       od5.product_id       AS Product5ID,
       prod5.name           AS Product5,
       prod5.url_image      AS Product5Image,
       prod5.ProductURL     AS Product5URL,
       od5.product_quantity AS Product5Quantity,
       od6.product_id       AS Product6ID,
       prod6.name           AS Product6,
       prod6.url_image      AS Product6Image,
       prod6.ProductURL     AS Product6URL,
       od6.product_quantity AS Product6Quantity,
       o.payment,
       o.invoice_date
       FROM   ps_orders o
       LEFT JOIN ps_customer c
              ON ( c.id_customer = o.id_customer )
       LEFT JOIN ps_address a
              ON ( a.id_address = o.id_address_delivery )
       LEFT JOIN ps_order_detail od1
              ON od1.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  1)
       LEFT JOIN ps_order_detail od2
              ON od2.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  1, 1)
       LEFT JOIN ps_order_detail od3
              ON od3.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  2, 1)
       LEFT JOIN ps_order_detail od4
              ON od4.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  3, 1)
       LEFT JOIN ps_order_detail od5
              ON od5.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  4, 1)
       LEFT JOIN ps_order_detail od6
              ON od6.id_order_detail = (SELECT d.id_order_detail
                                        FROM   ps_order_detail AS d
                                        WHERE  o.id_order = d.id_order
                                        ORDER  BY d.id_order_detail
                                        LIMIT  5, 1)

       LEFT JOIN (SELECT p.id_product         AS 'ID',
                         pl.name              AS 'Name',
        Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
       Concat('https://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) prod1
ON od1.product_id = prod1.id

LEFT JOIN (SELECT p.id_product         AS 'ID',
           pl.name              AS 'Name',
           Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
           Concat('https://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) prod2
ON od2.product_id = prod2.id
LEFT JOIN (SELECT p.id_product         AS 'ID',
           pl.name              AS 'Name',
           Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
           Concat('https://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) prod3
ON od3.product_id = prod3.id
LEFT JOIN (SELECT p.id_product         AS 'ID',
           pl.name              AS 'Name',
           Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
           Concat('https://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) prod4
ON od4.product_id = prod4.id
LEFT JOIN (SELECT p.id_product         AS 'ID',
           pl.name              AS 'Name',
           Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
           Concat('https://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) prod5
ON od5.product_id = prod5.id
LEFT JOIN (SELECT p.id_product         AS 'ID',
           pl.name              AS 'Name',
          Concat('{ImagePath}', pl.`link_rewrite`, '.jpg') AS "ProductURL",
           Concat('https://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) prod6
ON od6.product_id = prod6.id
GROUP  BY o.id_order
ORDER  BY o.id_order DESC
LIMIT  10