-
Sending order data to a script
I'm looking to create a process where if a certain product is ordered, a function will be fired off to send specific order data to a web script, likely as post variables. In looking, it seems that perhaps an observer may be the way to go here, though there are quite a few related to checkout.
First, am I on the right track with this? I would need to grab new orders as they came in, check to see if a specific item was included in the order, check the status to make sure it is set to processing meaning a payment was received, and then post the variables to the website script (customer name, E-mail address, etc.)
Any guidance would be much appreciated.
-
Re: Sending order data to a script
-
Re: Sending order data to a script
Great. This seems like it may work out. Will post back if I get stuck, which will probably be in about 6 minutes, grin.
-
Re: Sending order data to a script
I seem to have the simple part of this.
<?php
class orderNotifier extends base {
function orderNotifier() {
$this->attach($this, array('NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL'));
}
function update(&$Class, $notifier, $paramsArray) {
// Code to look through the order
}
?>
Is there docs somewhere to show me where the info on the order would be? Would it be under $class or do I pull it from session variables? For example, I'm looking for customer name/email, status, and ids of products ordered.
Thanks much.
-
Re: Sending order data to a script
$class->info array
and the
$class->products multidimensional array
If you use print_r() or var_dump() on those you'll see what they contain.
-
Re: Sending order data to a script
Quote:
Originally Posted by
DrByte
$class->info array
and the
$class->products multidimensional array
If you use print_r() or var_dump() on those you'll see what they contain.
I tried adding the class and autoloader but this just makes my Shopping acrt and other pages show a blank page. Tried adding error_reporting E_all but this doesn't seem to help debug what's going on.
Code:
<?php
class orderNotifier extends base {
function orderNotifier() {
$this->attach($this, array('NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL'));
}
function update(&$Class, $notifier, $paramsArray) {
// Code to look through the order
print_r($paramsArray);
print_r($Class->info);
print_r($Class->products);
}
}
?>
and then this
Code:
<?php
$autoLoadConfig[10][] = array('autoType'=>'class',
'loadFile'=>'observers/class.orderNotifier.php');
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
'className'=>'orderNotifier',
'objectName'=>'orderNotifier');
?>
Thanks for your patience.
-
Re: Sending order data to a script
error reporting is redirected: https://www.zen-cart.com/tutorials/index.php?article=82
And you'll probably need to wrap a die() around your print_r outputs, since they need to be echoed and then execution halted in order to see anything.
This is what I'll often do:
die('info: <pre> ' . print_r($var1, true) . print_r($var2, true) . print_r($var3, true));
-
Re: Sending order data to a script
Got it. I've got all of the product variables now along with order info from the info class. The only thing I'm not seeing in either of these is the customer data. Is this in a different class?
-
Re: Sending order data to a script
Nevermind, that would be $Class->customer (lucky guess). So I'm thinking I'm going to write the observer class to post several variables from each order to an external script and then let that script handle the order from there, so I don't have to fiddle with the observer class for each new case. Does this seem like a sane way to go? Will share my results when I'm done with this.
-
Re: Sending order data to a script
Hello. I am just about there, but getting a 500 server error when trying to send out the URL with CURL. I do have CURLSSL installed on the server and the rest of the script appears to check out codewise. I have it narrowed down to when my CURL function is being called. Didn't see a matching error in my Apache logs but perhaps I'm looking in the wrong spot.
PHP Code:
<?php
class orderNotifier extends base {
function orderNotifier() {
$this->attach($this, array('NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL'));
}
function update(&$Class, $notifier, $paramsArray) {
$num = $paramsArray[0];
$cust = $Class->customer;
$country = urlencode($cust['country']['title']);
foreach ($cust as $k => $v)
if (!is_array($k))
$cust[$k] = urlencode($v);
$inf = $Class->info;
if ($inf['order_status'] == 1) {
foreach($Class->products as $prod) {
$url = 'http://www.mystore.com/blabla.php?num=' . $num . '&firstname=' . $cust['firstname'] . '&lastname=' . $cust['lastname'] . '&email=' . $cust['email_address'] . '&company=' . $cust['company'] . '&address=' . $cust['street_address'] . '&city=' . $cust['city'] . '&state=' . $cust['state'] . '&postcode=' . $cust['postcode'] . '&country=' . $country . '&telephone=' . $cust['telephone'] . '&qty=' . $prod['qty'] . '&prod_name=' . urlencode($prod['name']) . '&id=' . $prod['id'];
submit_form($url);
}
}
}
function submit_form($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_INTERFACE, 'my_ip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
}
}
?>
-
Re: Sending order data to a script
You're probably right about thinking you need to check the server's logs. But I'm guessing it's your server's mod_sec logs you need, and that you've probably tripped a rule that's intended to block hackers because something you're passing is very similar.
The script you're posting TO isn't running any Zen Cart code, is it?
-
Re: Sending order data to a script
It turned out to be a PHP error after all, including a problem from calling that submit_form function from within the class. There was also an error in the urlencode code for countries. Here's the finished product, which will post to our custom script every time a new item is ordered and paid for. Thanks very much for your help.
PHP Code:
<?php
class orderNotifier extends base {
function orderNotifier() {
# $this->attach($this, array('NOTIFY_CHECKOUT_PROCESS_BEGIN', 'NOTIFY_HEADER_START_CHECKOUT_PAYMENT', 'NOTIFIER_CART_SHOW_TOTAL_START'));
$this->attach($this, array('NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL'));
}
function update(&$Class, $notifier, $paramsArray) {
$num = $paramsArray[0];
$cust = $Class->customer;
$country = urlencode($cust['country']['title']);
foreach ($cust as $k => $v)
if (!is_array($v))
$cust[$k] = urlencode($v);
$inf = $Class->info;
if ($inf['order_status'] == 2) {
foreach($Class->products as $prod) {
$url = 'http://www.yoururl.com/yourpage.php?num=' . $num . '&firstname=' . $cust['firstname'] . '&lastname=' . $cust['lastname'] . '&email=' . $cust['email_address'] . '&company=' . $cust['company'] . '&address=' . $cust['street_address'] . '&city=' . $cust['city'] . '&state=' . $cust['state'] . '&postcode=' . $cust['postcode'] . '&country=' . $country . '&telephone=' . $cust['telephone'] . '&qty=' . $prod['qty'] . '&prod_name=' . urlencode($prod['name']) . '&id=' . urlencode($prod['id']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_INTERFACE, 'yourip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
}
}
}
}
?>
-
Re: Sending order data to a script
It looks like y'all knocked this out pretty well. I have a couple of questions, so I can understand how this was accomplished.
Is 'NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL' the event that happens when an admin changes the order status via the drop down in the orders area?
Where does the new php code go? in orders.php somewhere? If so is this really the only file that gets modified in this mod?
Thanks
Paul
-
Re: Sending order data to a script
How do I find out what other variables are available like this one? $cust['lastname']
For example I'm after customer ID, I'll bet it's $cust['id'], but I imagine there is a defining location for it. I used the DEV tool kit and found all kinds of things, but nothing definate.
Thanks
Paul
-
Re: Sending order data to a script
Quote:
Originally Posted by
glitzsfa
Is 'NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL' the event that happens when an admin changes the order status via the drop down in the orders area?
No. It's only related to the storefront side. It has nothing to do with the admin side.
If you're targeting admin-related updates, that's a completely separate topic beyond the scope of the discussion in this thread.
Quote:
Originally Posted by
glitzsfa
Where does the new php code go? in orders.php somewhere? If so is this really the only file that gets modified in this mod?
See my first post in this thread, as it contains links to examples which answer that question.
Quote:
Originally Posted by
glitzsfa
How do I find out what other variables are available like this one? $cust['lastname']
You can output your own debug code for seeing what else is in the $cust array by using something like this in your code:
Code:
die('$cust data=' . print_r($cust, true));
-
Re: Sending order data to a script
I'm successfully sending product information to my external notifier. However, I also need to extract the attributes for a given product. Is there an attributes class under products tha can get me this, or is there another prefered way?
I'm using the code from earlier in this thread.
-
Re: Sending order data to a script
When hooking the NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL notifier point, the $Class->products array contains many components, including a sub-array of all that product's attributes details. So, you've already got all that information.
-
Re: Sending order data to a script
Great. I added this and am now sending attributes with the data as well.
foreach($prod['attributes'] as $attr) {
$url .= '&attr_' . $attr['option_id'] . '=' . urlencode($attr['value']);
}
Thanks for the help.
-
Re: Sending order data to a script
Rather than doing the various print_r's when I'm in 'debug mode', I use the Super Globals plugin and (when enabled) have it dump "all" the superglobals. That way, I've got clues as to the various data elements that were created as part of the page.
-
Re: Sending order data to a script
That's a cool idea.
One minor problem.
It doesn't seem to be firing when the customer uses Paypal as payment method.
I'm currently using NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL.
Should I be using a different notifier to catch these orders?
-
Re: Sending order data to a script
Use Express Checkout instead of Website Payments Standard, and then the normal notifiers will fire properly.
-
Re: Sending order data to a script
Thanks again for the quick response. That solved the problem in short order.
-
Re: Sending order data to a script
Using this approach, how would I grab the stock quantity from the ordered products? Since that information is not part of $Class->products i suspect that it would need to be added from the order class. Im looking at the stock_limited section but havent quite figured out how to adapt it.
It would be quite useful to have the out-of-stock products passed along so I could automate the backorder process.
-
Re: Sending order data to a script
Quote:
Originally Posted by
SHK
Using this approach, how would I grab the stock quantity from the ordered products? Since that information is not part of $Class->products i suspect that it would need to be added from the order class. Im looking at the stock_limited section but havent quite figured out how to adapt it.
It would be quite useful to have the out-of-stock products passed along so I could automate the backorder process.
The simplest way would be to simply run a quick query in your observer class to retrieve the necessary information to pass along.
-
Re: Sending order data to a script
Quote:
Originally Posted by
DrByte
The simplest way would be to simply run a quick query in your observer class to retrieve the necessary information to pass along.
Right you are. Dont know what I was thinking. Thanks for reminding me.
Now my little observer compares order qty with stock qty and changes the order status to backorder.
Backorder items(model/qty) are sent to our main system and placed on the next order to our suppliers.
Like jsquared I also send the shipping information sent to our logistics partners.
Quite useful little things, these observers.
-
Re: Sending order data to a script
And/or, You could set up a cron to check for 0 or low-stock items at regular intervals and do whatever you wish from there. Depends on how fast your inventory moves I guess.