Jump to content

[SOLVED]: Offer customers CSV download of order history


Recommended Posts

Hi all,

Have been stuck with this issue since the last 4 days and checked everywhere but I cannot find a solution.

 

I need to offer customers (for a b2b site) csv downloads for each of their order histories. This is what I have done:

 

1- Overridden OrderDetailController.php and written the following code under 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');

}

 

The order-detail-csv.tpl is:

 

col1, col2, col3, col4

1,2,3,4

5,6,7,8

 

(I've got hardcoded data in the template for testing purposes)

 

The problem is, order-detail-csv.tpl is appending the following at the bottom when downloading:

 

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>

<br />

<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><br />

<br />

<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><br />

<li class="first_item"><a href="<br />

<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><br />

<br />

<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><br />

<br />

<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><br />

 

How can I replace the default layout to an empty one (no header or footer) so I don't get any footer appended at the bottom? Is this even the right way to do it or should I use a different but simple approach?

Edited by Zach.Saeed (see edit history)
Link to comment
Share on other sites

Thanks for your interest Pascal. Much appreciated.

 

Actually I have checked further and the problem is that the message printing in the end is from a module hook called 'CMS block'.

If I go into admin-> modules->positions-> select CMS block, I can remove this hook from the footer area by typing 'orderdetail' in the exceptions field.

 

But then the error message is replaced by another error message for another hook and if I remove that aswell, it goes on and on. It is not practical edit every module position and add orderdetail as an excaption. There must be a shorter way of doing this.

 

One hackish way I can think of sending a CSV download to a customer is as follows: (Technique only. Tell me what you think):

1- Generate the CSV file in a string variable

2. Write and save the CSV to the file system.

3- Get a URL to that CSV file in a string var

4-Generate a normal HTML template for the user

5- On loading, this HTML pae redirects the user to the CSV URL and hence the download window popsup in browser.

 

Haven't tried this yet. What do you think. Any ideas? Feedback?

Link to comment
Share on other sites

Ok rather than using using the prestashop MVC to serve CSV order histories to customers (was a nightmare!), I have created a standalone php script and placed it under the prestashop root directory and I access it directly with the required GET params:

 

<?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() . "<br/>";

die();

}

 

?>

 

Obviously the above script needs conditional checks to stop it from crapping out if accessed by someone who isn't logged in or with the wrong GET params (Which I will do later. Out of this topic).

 

It at least does the task I needed it to do .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...