1 Attachment(s)
Re: Extract Transaction's Customer Info to be written to a file?
Quote:
Originally Posted by
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
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.
Re: Extract Transaction's Customer Info to be written to a file?
Quote:
Originally Posted by
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?
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 Code:
<?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 Code:
<?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:
Quote:
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
Re: Extract Transaction's Customer Info to be written to a file?
Try making the following alterations ...
from this:
Code:
$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:
Code:
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";
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
That was my "conceptual" mistake. Although I remembered to place a similar line in the observer's constructor:
PHP Code:
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 Code:
<?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