Jump to content

Save prestashop order as text file


Recommended Posts

Is there any way of saving the orders as a text(txt) file, after the customers order? because i am making a system for a sms printer, that need to print the orders out, after they out, but there is only 2 ways.. In a messed up string like so:

 

#55*2*27*Quantity,Order name%%%%,*;;39.00;;Firstname lastname;;;;7;;12232255*#

 

 

Like that, or the orders could be saved in a txt format, which the printer can read from.. Thanks in advance.

 

ietax

Link to comment
Share on other sites

I need a tools that export automatically txt orders for each order.

Important it will be in a specific format....

 

#55*2*27*Quantity,Order name%%%%,*;;39.00;;Firstname lastname;;;;7;;12232255*#

 

And each file must have order number in a tile

Es. 003456.txt

 

Thanks

Link to comment
Share on other sites

Create a module that implements the newOrder hook.  Everytime a new order is placed, the module will be notified and it can create a text file in a folder that you define.

 

If you are asking for someone to do this, then just ask and provide the exact requirements for the text file.  You should also state if you are looking for someone to do this for free, or if you are asking for a professional to hire.

Link to comment
Share on other sites

define the file layout

#55*2*27*Quantity,Order name%%%%,*;;39.00;;Firstname lastname;;;;7;;12232255*#

what do each of these represent (the firstname and lastname are obvious), the others are less obvious

 

 

I need this layout

NAME SURNAME|ADDRESS|||ZIP CODE|CITY|UK|TEL|MAIL||FLR||||0|COMPANY||ADDRESS|||CAP|PARIS|FR|1|3,5|0,00|||||||||||U|0|0,00|||0,00|222,00||||||USD||
Link to comment
Share on other sites

ADDRESS (position 2): Is the address line 1?  Is this delivery or invoice address?

ZIP CODE (position 5): Is the address line 1?  Is this delivery or invoice address?

CITY (position 6): Is the address line 1?  Is this delivery or invoice address?

UK (position 7): Is this country? Should it be hardcoded to UK always? Is this delivery or invoice address?

 

Hopefully you see where I am going..  Please update your specification so it is clear

Link to comment
Share on other sites

ADDRESS (position 2): ok address line 1

ZIP CODE (position 5): zip code delivery address

CITY (position 6): city to delivery

UK (position 7): Is this country, maybe change, UK IT FR DE ecc...

 

In TXT file I need all delivery address. thanks

 

 

ADDRESS (position 2): Is the address line 1?  Is this delivery or invoice address?

ZIP CODE (position 5): Is the address line 1?  Is this delivery or invoice address?

CITY (position 6): Is the address line 1?  Is this delivery or invoice address?

UK (position 7): Is this country? Should it be hardcoded to UK always? Is this delivery or invoice address?

 

Hopefully you see where I am going..  Please update your specification so it is clear

Link to comment
Share on other sites

ok, so you failed to see my point.  You need to provide requirements for every single field in your file layout.

 

sorry

 

I need this file type TXT

name file: ordernumber.txt

position: pc folder

 

(name) | (surname) | (company) | (address 1) | (address 2) | (ZIP code) | (city) | (country) | (phone) | (mail)

 

it is possible? it is clear?

Link to comment
Share on other sites

yes this is much better, your previous post had a lot of 0 or what would seem to be dollar amounts.  I'll assume you no longer need that.

 

some follow up questions now...

1) When you say 'pc folder'.  What PC?  Are you saying that you want the text file to appear in a folder on your desktop?  Does your website run on your PC?  Or is the website hosted on a server?

 

2) You would expect each file to represent a new order, therefore this file would have a single line. Please confirm.

 

3) (mail) I assume this is the customers email address?

 

4) (phone) Should this be the home or mobile phone?  What if both are provided?

Link to comment
Share on other sites

1- pc folder. I need all txt file in folder on my PC. I have a website hostesd on a server, I prefer to have TXT file on my PC. But it is ok also on server.

 

2- I confirm single line for one TXT order

 

3- yes, customer mail address

 

4- we can add a new field, one for home and one for mobile.

 

Thanks

Link to comment
Share on other sites

If the website is running on a remote server, then the file would be created there.  Do you run an FTP service, or have a network drive mapped to your PC?  There is not really too many options available that would securely send this TXT file from the server to your PC.

 

So I believe you have now provided enough details for someone to create the module for you.  Hopefully someone will offer to create the module for you, but if you wish to hire a professional, then send me a PM and I can provide you a estimate for creating the module.

Link to comment
Share on other sites

Hi,

 

Clearly, a module would be the 'best' way to handle with this problem.

Another way could be a small php-script.

 

You could create a new boolean field in the order table called 'saved_as_txt' in the order-table.

 

Then you need a php-script, something like this:

$host = "localhost"; 
$user = "test"; 
$pass = "test"; 
$db = "testdb"; 

mysql_connect($host, $user, $pass); 
mysql_select_db($db);
unset($host,$user,$pass,$db); 


$query = "SELECT id_order FROM ps_orders WHERE saved_as_txt = 0"; 

$result = mysql_query($query);
  while ($row = mysql_fetch_array($result))
{
    // Here you can perform get the information stored in related tables
    // and write them into a txt-file
}  

You could make a CRON-Job to create the txt-files every xxx minutes/hours.

 

If your server supports SSH you could write a small script that downloads the txt-files from the server and saves them to you desktop.

Edited by Qvixx (see edit history)
Link to comment
Share on other sites

This example does NOT write email and country into the txt-file (would be more complex).

 

First, you need to add a new field to your 'ps_orders' table.

Let's call it 'saved_as_txt'

Type: INT

Length: 1

Default-Value: 0

 

You can try this php-code (backup before !!! - I did NOT test it!)

 

It creates the files in the same folder as it is saved in.

Remember to change connection-informations!

<?php

// Start connection-part
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "ps_db";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);
// End connection-part

$query = "SELECT id_order, id_address_delivery FROM ps_orders WHERE saved_as_txt = 0"; //all order-IDs which are not already saved as txt

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) //goes through all the results
{
    $id_order = $row['id_order']; //saves the current order ID in a variable
    $id_address_delivery = $row['id_address_delivery']; //saves the current delivery-address in a variable
    $filename = $id_order.".txt";
    
    $file = fopen($filename,"w"); //creates a new file called [order-id].txt
    
    $query2 = "SELECT * FROM ps_address WHERE id_address = " . $id_address_delivery;
    
    $result2 = mysql_query($query2);
    while ($row2 = mysql_fetch_array($result2)) //goes through delivery-adresses which have the same ID (should be only one)
    {
        fwrite($file, $row2['lastname'] . " | ");
        fwrite($file, $row2['firstname'] . " | ");
        fwrite($file, $row2['company'] . " | ");
        fwrite($file, $row2['address1'] . " | ");
        fwrite($file, $row2['address2'] . " | ");
        fwrite($file, $row2['postcode'] . " | ");
        fwrite($file, $row2['city'] . " | ");
        fwrite($file, $row2['phone']);
    }
    
    fclose($file);
    
    // update the saved_as_txt field (otherwise a file would be created every time)
    $query3 = "UPDATE ps_orders SET saved_as_txt = 1 WHERE id_order = " . $id_order;
    mysql_query($query3);
    
}

echo "Files created!";

?>
Link to comment
Share on other sites

nice job Qvixx.  A simple solution to the problem. 

 

Using hooks and a module would make it more portable and re-usable, and also would avoid altering core database tables, but does come with the added complexity and management of a module.

 

ietax, I'm not going to reply to your PM at this point since it seems you are on your way using this as a foundation to grow the script.  If you still require professional support, send me another PM.

Link to comment
Share on other sites

 

This example does NOT write email and country into the txt-file (would be more complex).

 

First, you need to add a new field to your 'ps_orders' table.

Let's call it 'saved_as_txt'

Type: INT

Length: 1

Default-Value: 0

 

You can try this php-code (backup before !!! - I did NOT test it!)

 

It creates the files in the same folder as it is saved in.

Remember to change connection-informations!

<?php

// Start connection-part
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "ps_db";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);
// End connection-part

$query = "SELECT id_order, id_address_delivery FROM ps_orders WHERE saved_as_txt = 0"; //all order-IDs which are not already saved as txt

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) //goes through all the results
{
    $id_order = $row['id_order']; //saves the current order ID in a variable
    $id_address_delivery = $row['id_address_delivery']; //saves the current delivery-address in a variable
    $filename = $id_order.".txt";
    
    $file = fopen($filename,"w"); //creates a new file called [order-id].txt
    
    $query2 = "SELECT * FROM ps_address WHERE id_address = " . $id_address_delivery;
    
    $result2 = mysql_query($query2);
    while ($row2 = mysql_fetch_array($result2)) //goes through delivery-adresses which have the same ID (should be only one)
    {
        fwrite($file, $row2['lastname'] . " | ");
        fwrite($file, $row2['firstname'] . " | ");
        fwrite($file, $row2['company'] . " | ");
        fwrite($file, $row2['address1'] . " | ");
        fwrite($file, $row2['address2'] . " | ");
        fwrite($file, $row2['postcode'] . " | ");
        fwrite($file, $row2['city'] . " | ");
        fwrite($file, $row2['phone']);
    }
    
    fclose($file);
    
    // update the saved_as_txt field (otherwise a file would be created every time)
    $query3 = "UPDATE ps_orders SET saved_as_txt = 1 WHERE id_order = " . $id_order;
    mysql_query($query3);
    
}

echo "Files created!";

?>

 

I inserted all seller address inside PHP file...

I have another question: if i insert "id_country" it show me number country, but i need "iso_cod" indise "ps_country table".. it is possible? Thanks a lot...

Link to comment
Share on other sites


Thanks, I really like to help me trying to learning me but I can't solve, I'm studying SQL and PHP recently and I'm not ready to solve alone, I'm stop here

 

 

$query2 = "SELECT * FROM ps_address WHERE id_address = " . $id_address_delivery AND iso_code FROM ps_country

Link to comment
Share on other sites

Try this Code.

I added some comments for you.

 

Again, the code is not tested!

 

And btw it isn't good SQL but it works.

In this case you don't need a perfect perfomance.

<?php

// Start connection-part
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "ps_db";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);
// End connection-part

$query = "SELECT id_order, id_address_delivery FROM ps_orders WHERE saved_as_txt = 0"; //all order-IDs which are not already saved as txt

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) //goes through all the results
{
    $id_order = $row['id_order']; //saves the current order ID in a variable
    $id_address_delivery = $row['id_address_delivery']; //saves the current delivery-address in a variable
    $filename = $id_order.".txt";
    
    $file = fopen($filename,"w"); //creates a new file called [order-id].txt
    
    $query2 = "SELECT * FROM ps_address WHERE id_address = " . $id_address_delivery;
    
    $result2 = mysql_query($query2);
    while ($row2 = mysql_fetch_array($result2)) //goes through delivery-adresses which have the same ID (should be only one)
    {
    
        $id_country = $row2['id_country']; //Save the value as a variable
    
        fwrite($file, $row2['lastname'] . " | ");
        fwrite($file, $row2['firstname'] . " | ");
        fwrite($file, $row2['company'] . " | ");
        fwrite($file, $row2['address1'] . " | ");
        fwrite($file, $row2['address2'] . " | ");
        fwrite($file, $row2['postcode'] . " | ");
        fwrite($file, $row2['city'] . " | ");
        
        // If you place the new query here the Country-Code will appear after city and before phone
        // It should be a new query because you read values from another table (ps_address -> ps_country)
        $query4 = "SELECT iso_code FROM ps_country WHERE id_country = " . $id_country; // could also be "... WHERE id_country = " . $row2['id_country'];
        
        $result4 = mysql_query($query4);
        while ($row4 = mysql_fetch_array($result4)) //goes through id_country's which have the same ID as the current address (should be one)
        {
        
            fwrite($file, $row4['iso_code'] . " | ");
            
        }
        
        fwrite($file, $row2['phone']);
    }
    
    fclose($file);
    
    // update the saved_as_txt field (otherwise a file would be created every time)
    $query3 = "UPDATE ps_orders SET saved_as_txt = 1 WHERE id_order = " . $id_order;
    mysql_query($query3);
    
}

echo "Files created!";

?>
Edited by Qvixx (see edit history)
Link to comment
Share on other sites

Perfect, tanks a lot... for you is very simple to do it...

I studied your code and I inserted new value, email address and complete seller address.

 

And it works... I need more ;-)

 

I nedd new field:

 

check id_country on (ps_address table)

if id_zone (table ps_country) is 1 write "xxx"

if id_zone (table ps_country) is 2 write "yyy"

if id_zone (table ps_country) is 7 write "zzz"

 

i tried to transalte it in code but i can't start.. give me direction please...

 

 

thanks so much

Link to comment
Share on other sites

Sorry, I don't exactly understand what you need.

 

You don't want the country-iso-code anymore instead you want to insert own values?

 

For Example:

if id_country = 1 then write USA, if 2 then Germany, if 3 then Englang, etc.

 

something like that?

if ($id_country == 1) {
     fwrite($file, "USA | ");
} 

elseif ($id_country == 2) {
    fwrite($file, "Germany | ");
} 

//Here you can just insert more elseif-blocks 

else {
    //Code if you did not catch the value
}
Edited by Qvixx (see edit history)
Link to comment
Share on other sites

No. sorry for my bad explain,

 

I must verify id_zone of customer address, I have 3 zone configurated on PS

 

If customer buy from zone 1 (europe for ex) - write on new row  "EU" in txt file

If customer buy from zone 2 (extra cee for ex) - write "extra" in txt file

If customer buy from zone 3 (special for ex) - write "SPE" in txt file

 

is better now my explain? I hope yes

Link to comment
Share on other sites

SOrry but i must declare new variable before maybe

Because I need 3 diffent possible value.

 

I must verify id_country on (ps_address table)..in customer address...

and after table ps_country for find id_zone

 

maybe I cannot explain very well... but it must write a different code (XXX YYY ZZZ)

 for each zone

 

I have 3 different zone configurated.

 

If customer come from France (or Italy or Uk or DE, ecc..) will be XXX

If customer come from USA (or CAN, or Norway, ecc..) will be YYY

If customer come from others (Brazil, Argnetina, ecc) will be ZZZ

 

Ok?

Link to comment
Share on other sites

Then yu can just use

SELECT id_zone, iso_code ...

And then use the if-then-else formula:

(Don't forget to save the id_zone-value in the variable $id_zone -> otherwise the example below won't work)

if ($id_zone == 1) {
     fwrite($file, "USA | ");
} 

elseif ($id_zone == 2) {
    fwrite($file, "Germany | ");
} 

//Here you can just insert more elseif-blocks 

else {
    //Code if you did not catch the value
}

If you need help just post you non-working code here

Link to comment
Share on other sites

Hi, thank you very much...!

But I cannot link 2 tables

 

ps_address table

 

AND

 

ps_country table

 

$query8 = "SELECT * FROM ps_address WHERE id_coutry = " . $id_coutry;
        
        
        if ($id_zone == 1) {
        fwrite($file, "XXX|"); //38
        }

        elseif ($id_zone == 2) {
        fwrite($file, "YYY|"); //38
        }
        
        elseif ($id_zone == 7) {
        fwrite($file, "ZZZ|"); //38
        }

Link to comment
Share on other sites

I inserted DAte, mail, buyer account informatino but i cannot solve this "is_zone"..

 

can you help me to resolve please? i tried but i have this

 

1
Notice: Undefined index: id_zone in C:\xampp\htdocs\shop\test.php on line 96
Files created!

 

more times, i cannot declare id_zone variable....

Link to comment
Share on other sites

Hi, i SOLVED

 

can you check it please? it is correct? it works....

 

 

$query8 = "SELECT id_zone FROM ps_country WHERE id_country = " . $id_country;
        $result8 = mysql_query($query8);
        while ($row8 = mysql_fetch_array($result8))
        
        $id_zone = $row8['id_zone'];
        
        if ($id_zone == 1) {
        fwrite($file, "ECX|"); //38
        }

        elseif ($id_zone == 2) {
        fwrite($file, "DOM|"); //38
        }
        
        elseif ($id_zone == 7) {
        fwrite($file, "WPX|"); //38
        }

 

 

I tried to save all paid orders.. but i have an error, i tried to modify this query:

 

14 - $query = "SELECT id_order, id_address_delivery FROM ps_orders WHERE saved_as_txt = 0 AND current_state = 2";

 

16 - $result = mysql_query($query);
17 - while ($row = mysql_fetch_array($result))

 

but i have this error on line 17

 

 

1
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\shop\test2.php on line 17
Files created!

 

 

 

Thanks, if you have time...

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...