Zen Cart Logo
Forums / Managing Customers and Orders / Extract Transaction's Customer Info to be written to a file?

Extract Transaction's Customer Info to be written to a file?

Locked

Views: 2,952

Results 1 to 16 of 16
This thread is locked. New replies are disabled.
12 Aug 2008, 7:58 PM
#1
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Extract Transaction's Customer Info to be written to a file?

I would like to "intercept" a transaction's customer details, in order to write it to a text file.

I know that this is possible (because customer information is in the database, accessible by Zen Cart's PHP scripts, and displayable accordingly), but I don't know what is the correct approach to implement it?

Should I hack an existing PHP file to pipe this information to the text file?

Or is there already a "hook" somewhere in Zen Cart that allows me to do that more cleanly and/or elegantly?

Thanks,
Daniel

13 Aug 2008, 12:25 AM
#2
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

I would like to tap/hook into the PHP page that sends order confirmation email, so that I can retrieve customer information from there and write it to a temporary file.

Where do I do that?

Thanks,
Daniel

13 Aug 2008, 12:49 AM
#3
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: Extract Transaction's Customer Info to be written to a file?

You can look in includes/modules/checkout_process.php about line 75

//send email notifications
$order->send_order_email($insert_id, 2);
$zco_notifier->notify('NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL');
13 Aug 2008, 2:01 AM
#4
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

kobra:

You can look in includes/modules/checkout_process.php about line 75

//send email notifications
$order->send_order_email($insert_id, 2);
$zco_notifier->notify('NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL');


Thank you! I was mistakenly looking for it under /includes/modules/pages/checkout_process and I really needed this tip to know where to start. At least now I can start to see the light at the end of the tunnel.

I understand that I now need to create a handler (observer class) for the NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL event, right?

I think that the following tutorial can be helpful:

<http://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials>

Any other tips you can recommend?

Perhaps there already is a handler/observer/hook in the contrib section that can save me the time of development and testing?

Thanks,
Daniel
13 Aug 2008, 3:50 AM
#5
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: Extract Transaction's Customer Info to be written to a file?

You should be on the correct track with that and the correct file to start with

20 Aug 2008, 8:06 PM
#6
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

kobra:

You should be on the correct track with that and the correct file to start with
Thank you. After some learning, I managed to create an auto_loader config file in includes/auto_loader/ (first step required).

Then, the file containing the actual intelligence of my mod, needs to be created. It's an observer class file in includes/classes/observers/ in which the main trick, IMHO, is to find the right object to attach. After some learning and research (with the help of DrByte, thank you!) I concluded that that object should be 'orders' (please correct if I am wrong). Thus, the resulting observer.writecustinfo.php looks like:

<?php
/**
 * Observer class to write customer info to a temp file.
 */
class writecustinfo extends base {

 /**  constructor method !
   *  Attaches our class to the $orders class and watches for 1 notifier event.
   */
  function writecustinfo() {
    $orders->attach($this, array('NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL'));
  }

 /**   Actual Method to write data to file
   *  Called by observed class when any of our notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function writetofile(&$class, $eventID) {
    /**
	* Write the following fields to a file
	*/

	orders->fields[orders_id];
	orders->fields[customers_id];
	orders->fields[customers_name];
	orders->fields[customers_company];
	orders->fields[customers_street_address];
	orders->fields[customers_suburb];
	orders->fields[customers_city];
	orders->fields[customers_postcode];
	orders->fields[customers_state];
	orders->fields[customers_country];
	orders->fields[customers_telephone];
	orders->fields[customers_email_address];
	orders->fields[customers_address_format_id];
	orders->fields[delivery_name];
	orders->fields[delivery_company];
	orders->fields[delivery_street_address];
	orders->fields[delivery_suburb];
	orders->fields[delivery_city];
	orders->fields[delivery_postcode];
	orders->fields[delivery_state];
	orders->fields[delivery_country];
	orders->fields[delivery_address_format_id];
	orders->fields[billing_name];
	orders->fields[billing_company];
	orders->fields[billing_street_address];
	orders->fields[billing_suburb];
	orders->fields[billing_city];
	orders->fields[billing_postcode];
	orders->fields[billing_state];
	orders->fields[billing_country];
  }
}
?>

Does the above make sense at all?

Do you identify fundamental flaws in the code or in my understanding of the process?

(for simplicty, I didn't include the actual file writing code, I will start working on it shortly).

Thanks,
Daniel

20 Aug 2008, 9:27 PM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Extract Transaction's Customer Info to be written to a file?

That is the concept, yes

21 Aug 2008, 3:39 PM
#8
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

DrByte:

That is the concept, yes
Concepts are great but when it comes to real life... :smile:

When I tried to implement the above using $orders->attach(), the entire store blanked! This puzzled me for a moment since the 'freeProduct' example in the Developers API Tutorial works on my system without any problem (or modification).

At first, I tried changing the $autoLoadConfig offset (aka 'action point'), in the config file, from 90 to 210, thinking that perhaps this has to do with the fact that $orders is not instantiated until offset 180. That didn't help (same exact white blank page).

So, after some research I came across this excellent thread: Use Of Notifiers - How To.

I then realized that, despite having different symptoms, I probably have the same exact problem: attempting to use a class ($orders) instead of the 'stub' notifier class, $zco_notifier. It is also the class that actually sends the notification. That solved the blanking problem! :clap:

So, I can now start working on the actual functionality of this observer.

21 Aug 2008, 5:16 PM
#9
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

OK, I got the update() method of the observer class to act upon email confirmation and to write something to a test text file. :smile:

The only problem now is that... all $orders->fields[] elements seem to be empty. :huh:

Using SuperGlobals Plus I can see at the successful checkout page that the $orders->fields[] elements are there and contain valid and correct values. So it seems that by the time my observer class receives the NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL notification, the $orders->fields[] array has gone??? :blink:

I will try tying my observer class to NOTIFY_HEADER_START_CHECKOUT_SUCCESS to see if I can intercept that $orders->fields ...

Thanks again Kobra & DrByte for all your help and guidance. It's fun to program in such great environment.

Daniel

21 Aug 2008, 6:24 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Extract Transaction's Customer Info to be written to a file?

Isn't the object named $order, not $orders ?

21 Aug 2008, 7:01 PM
#11
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

DrByte:

Isn't the object named $order, not $orders ?
No, it is $orders, as can be seen in the attached image.

I am still lost as to why the contents of the $orders->fields[] array is empty or what is the proper stage to "snatch" it. I tried the following notificatications - to no avail:

NOTIFY_HEADER_START_CHECKOUT_SUCCESS
NOTIFY_HEADER_END_CHECKOUT_SUCCESS
NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL

I am still successful at writing test text (and variables) to the test file in upon any of the above notifications. However, the $orders->fields[] seems to be empty when I do that, since non of those fields get written to the file.

Any idea how to snatch correctly those $orders->fields[] values?

Thanks,
Daniel

21 Aug 2008, 7:04 PM
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Extract Transaction's Customer Info to be written to a file?

A "queryFactoryResult" object is the returned data from a database query.

The actual order object that contains the data for the currently in-progress order while checkout_process is creating and storing an order, is $order.

21 Aug 2008, 7:22 PM
#13
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

DrByte:

A "queryFactoryResult" object is the returned data from a database query.

The actual order object that contains the data for the currently in-progress order while checkout_process is creating and storing an order, is $order.
Thanks for pointing this out. I tried the same exact thing with $order (instead of $orders) but I am receiving the same results. :(

By now I am almost certain that my problem is not a syntax bug, but something conceptual that I am missing.

What could it be?

21 Aug 2008, 11:10 PM
#14
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

Well, I haven't solved the mystery yet, but I can already admit to one conceptual mistake: I forgot to read the source code (and relied, instead, on documentation and existing threads in the Zen Cart forums). :blush:

Also, so that I can save other beginners from the same syntax mistake, the correct syntax for accessing the $orders->field[] array elements is $orders->fields['customers_name'], not $orders->fields[customers_name] (long live single quotes...)

Fixing this syntax mistake didn't help, of course, because IMHO my problem lies within the scope/lifetime of the objects I am trying to "snatch".

In the meanwhile, I am providing here a snapshot of the my partially working solution, along with what actually gets written to the file:

includes\auto_loaders\config.writeCustInfo.php:

<?php

$autoLoadConfig[90][] = array('autoType'=>'class',
                              'loadFile'=>'observers/class.writeCustInfo.php');
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
                              'className'=>'writeCustInfo',
                              'objectName'=>'writeCustInfo');

?>

includes\classes\observers\class.writeCustInfo.php:

<?php
/**
 * Observer class to write customer info to a temp file.
 */
class writeCustInfo extends base {

 /**  constructor method !
   * 
   * Attach observer class to the global $zco_notifier and watches for a single notifier event.
   */
  function writeCustInfo() {
	global $zco_notifier;
    $zco_notifier->attach($this, array('NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL'));
  }

 /**   Actual Method that writes customer info to file
   * 
   * Called by observed class when any of the notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function update(&$class, $eventID) {
	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");

	/**
	* Write the following fields to a file
	*/
	$stringData = "This is the first line of ";
	$stringData = $stringData . $myFile;
	fwrite($fh, $stringData);
	fwrite($fh, "\r\n");
	
	$customerInfo = "Customer Information: \r\n";
	$customerInfo = $customerInfo . $myFile;
	$customerInfo = $customerInfo . $order->fields['customers_name'];
	$customerInfo = $customerInfo . $order->fields['customers_company'];
	$customerInfo = $customerInfo . $order->fields['customers_street_address'];
	fwrite($fh, $customerInfo);
	fwrite($fh, "\r\n");

	fclose($fh);	
  }
}
?>

The output, in testFile.txt:

This is the first line of testFile.txt
Customer Information:
testFile.txt
I will post here the complete solution, once I have it. But if you notice any gross mistake in my code that could explain the mystery, that would help me post the solution faster. :smile:

Thanks,
Daniel

22 Aug 2008, 2:29 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Extract Transaction's Customer Info to be written to a file?

Try making the following alterations ...

from this:$customerInfo = "Customer Information: \r\n"; $customerInfo = $customerInfo . $myFile; $customerInfo = $customerInfo . $order->fields['customers_name']; $customerInfo = $customerInfo . $order->fields['customers_company']; $customerInfo = $customerInfo . $order->fields['customers_street_address'];to this:```
global $order;
$customerInfo = "Customer Information: \r\n";
$customerInfo .= 'First: ' . $order->customer['firstname'] . "\r\n";
$customerInfo .= 'Last: ' . $order->customer['lastname'] . "\r\n";
$customerInfo .= 'Email: ' . $order->customer['email_address'] . "\r\n";
$customerInfo .= "\r\n\r\n\r\nOther information available is described here:\r\n" . print_r($order, true) . "\r\n";

22 Aug 2008, 4:53 AM
#16
zcnb avatar

zcnb

Zen Follower

Join Date:
Jun 2008
Posts:
338
Plugin Contributions:
0

Re: Extract Transaction's Customer Info to be written to a file?

Dr. Byte - you seem to be a true doctor :smile: - Thank you so much!

I did discover by myself that the class 'order' has no member called 'fields' but has a member named 'customer' (array) - and I tried a fix similar to what you suggested, which of course didn't work because... I was missing the line

global $order;

That was my "conceptual" mistake. Although I remembered to place a similar line in the observer's constructor:

global $zco_notifier;

I failed to tell update() that $order is global.

Now all is well and here is the final result (config.writeCustInfo.php remains unchanged and so I will post the complete code for class.writeCustInfo.php only):

<?php
/**
 * Observer class to write customer info to a temp file.
 */
class writeCustInfo extends base {

 /**  constructor method !
   * 
   * Attach observer class to the global $zco_notifier and watches for a single notifier event.
   */
  function writeCustInfo() {
	global $zco_notifier;
    $zco_notifier->attach($this, array('NOTIFY_CHECKOUT_PROCESS_AFTER_SEND_ORDER_EMAIL'));
  }

 /**   Actual Method that writes customer info to file
   * 
   * Called by observed class when any of the notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function update(&$class, $eventID) {
	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");

	/**
	* Write the following fields to a file
	*/
	$stringData = "This is the first line of ";
	$stringData = $stringData . $myFile;
	fwrite($fh, $stringData);
	fwrite($fh, "\r\n");

	
    global $order;
    $customerInfo = "Customer Information: \r\n";
    $customerInfo .= 'First: ' . $order->customer['firstname'] . "\r\n";
    $customerInfo .= 'Last: ' . $order->customer['lastname'] . "\r\n";
    $customerInfo .= 'Email: ' . $order->customer['email_address'] . "\r\n";
 //   $customerInfo .= "\r\n\r\n\r\nOther information available is described here:\r\n" . print_r($order, true) . "\r\n";

	
	fwrite($fh, $customerInfo);
	fwrite($fh, "\r\n");

	fclose($fh);	
  }
}
?>

Thanks again!
Daniel