首页 文章

无法为Prestashop中的客户创建订单历史记录CSV下载

提问于
浏览
1

自从过去4天以来一直坚持这个问题,并在任何地方检查,但我找不到解决方案 .

在Prestashop,我需要为客户(针对b2b网站)提供每个订单历史记录的csv下载 . 这就是我所做的:

1-重写OrderDetailController.php并在initContent()下编写以下代码:

if ( !array_key_exists('csv_download', $_GET) ) {
    parent::initContent();
} else if ( array_key_exists('csv_download', $_GET) ) {
    header('Content-type: text/csv');
    header('Content-Type: application/force-download; charset=UTF-8');
    header('Cache-Control: no-store, no-cache');
    header('Content-disposition: attachment; filename="'.'abc'.'_'.date('Y-m-d_His').'.csv"');
    $this->context->smarty->display(_PS_THEME_DIR_.'order-detail-csv.tpl');
}

order-detail-csv.tpl是:

col1, col2, col3, col4
1,2,3,4
5,6,7,8

(我在模板中有硬编码值用于测试目的)

问题是,当单击链接以访问此链接时,CSV的底部会附加以下内容:

col1, col2, col3, col4
1,2,3,4
5,6,7,8
<!-- MODULE Block footer -->
<div class="block_various_links" id="block_various_links_footer">
<p class="title_block">Information</p>
<ul>

<b>Notice</b>: Undefined index: PS_CATALOG_MODE in <b>D:\Program_Files\xampp\htdocs \prestashop\cache\smarty\compile\66\40\fc\6640fcf250c9a844925d45d85c39618c4233b46e.file.blockcms.tpl.php</b> on line <b>93</b>

<b>Notice</b>: Trying to get property of non-object in <b>D:\Program_Files\xampp\htdocs\prestashop\cache\smarty\compile\66\40\fc\6640fcf250c9a844925d45d85c39618c4233b46e.file.blockcms.tpl.php</b> on line <b>93</b>
<li class="first_item"><a href="
<b>Notice</b>: Undefined index: link in <b>D:\Program_Files\xampp\htdocs\prestashop\cache\smarty\compile\66\40\fc\6640fcf250c9a844925d45d85c39618c4233b46e.file.blockcms.tpl.php</b> on line <b>93</b>

<b>Notice</b>: Trying to get property of non-object in <b>D:\Program_Files\xampp\htdocs\prestashop\cache\smarty\compile\66\40\fc\6640fcf250c9a844925d45d85c39618c4233b46e.file.blockcms.tpl.php</b> on line <b>93</b>

<b>Fatal error</b>: Call to a member function getPageLink() on a non-object in <b>D:\Program_Files\xampp\htdocs\prestashop\cache\smarty\compile\66\40\fc\6640fcf250c9a844925d45d85c39618c4233b46e.file.blockcms.tpl.php</b> on line <b>93</b>

进一步检查,问题是CSV底部的消息打印来自名为“CMS块”的模块挂钩 . 如果我进入admin-> modules-> positions->选择CMS块,我可以通过在例外字段中键入“orderdetail”从页脚区域中删除此挂钩 .

但是然后错误消息被另一个钩子的另一个错误消息所取代,如果我删除它,那么它会继续 . 编辑每个模块位置是不实际的,并添加orderdetail作为例外 .

必须有一个正确的方法,以便prestashop可以忽略smarty布局和所有位置模块挂钩并发送纯csv(由上面的order-detail-csv.tpl表示) .

1 回答

  • 1

    好的找到了解决方案 . 理想情况下不是prestashop方式,而是完成工作 .

    我没有使用prestashop MVC向客户提供CSV订单历史(这是一场噩梦!),而是创建了一个独立的php脚本并将其放在prestashop根目录下,并使用所需的GET参数直接访问它:

    (我仍然使用Prestashop配置连接到db并使用上下文类来访问用户的cookie数据)

    <?php
     require(dirname(__FILE__).'/config/config.inc.php');
    
    $context = Context::getContext();
    
    header('Content-type: text/csv');
    header('Content-Type: application/force-download; charset=UTF-8');
    header('Cache-Control: no-store, no-cache');
    header('Content-disposition: attachment; filename="'.'abc'.'_'.date('Y-m-d_His').'.csv"');
    
    try {
        $csv_string="";
    
        $conn = new PDO('mysql:host='._DB_SERVER_.';dbname='._DB_NAME_, _DB_USER_, _DB_PASSWD_);
    
        $query = " SELECT
                o.*,
                od.*
            FROM
                orders o
            INNER JOIN
                order_detail od ON od.id_order = o.id_order
            WHERE
                o.id_customer = ".$context->customer->id;
    
        if(isset($_GET['id_order']) && !empty($_GET['id_order'])) {
            $query = $query." AND o.id_order = ".$_GET['id_order'];
        } else if(isset($_GET['from']) && !empty($_GET['from'])) {
            $query = $query." AND o.date_add > '".$_GET['from']."' AND o.date_add < '".$_GET['to']."'";
        }
    
        //Get column names
        $fetch_type = PDO::FETCH_ASSOC;
        $result = $conn->query($query);
        $col_row = $result->fetchAll($fetch_type);
        $columns = empty($col_row) ? array() : array_keys((array)$col_row[0]);
        //assemble csv
        foreach($columns as $col) {
            $csv_string = $csv_string.$col.', ';
        }
    
        //get values
        $result = $conn->query($query);
        //assemble csv
        foreach($result as $row) {
             $csv_string = $csv_string."\n";
             foreach($columns as $col) {
                 $csv_string = $csv_string . $row[$col].', ';
             }
        }
    
        echo($csv_string);
    
        $conn = null;
    
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "
    "; die(); } ?>

    显然,上面的脚本需要有条件的检查才能阻止它被未登录或使用错误的GET参数的人访问(我将在稍后讨论 . 本主题之外) .

    它至少完成了我需要它完成的任务 .

相关问题