If this is your first visit, check our documentation. To post, register or login.

Sending order data to a script

Started by jsquared on 11 Jan 2012, 10:37 PM · Views: 4,418

Results 1 to 20 of 26
11 Jan 2012, 10:37 PM
#1
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #1 by jsquared

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.

J.J. Meddaugh
http://www.atguys.com

11 Jan 2012, 10:52 PM
#2
drbyte avatar

drbyte

Sensei

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

Post #2 by drbyte

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

11 Jan 2012, 11:01 PM
#3
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #3 by jsquared

Great. This seems like it may work out. Will post back if I get stuck, which will probably be in about 6 minutes, grin.

J.J. Meddaugh
http://www.atguys.com

12 Jan 2012, 12:02 AM
#4
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #4 by jsquared

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.

J.J. Meddaugh
http://www.atguys.com

12 Jan 2012, 3:27 AM
#5
drbyte avatar

drbyte

Sensei

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

Post #5 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.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Jan 2012, 5:42 AM
#6
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #6 by jsquared

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.

<?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```

<?php $autoLoadConfig[10][] = array('autoType'=>'class', 'loadFile'=>'observers/class.orderNotifier.php'); $autoLoadConfig[90][] = array('autoType'=>'classInstantiate', 'className'=>'orderNotifier', 'objectName'=>'orderNotifier'); ?>
Thanks for your patience.

J.J. Meddaugh
http://www.atguys.com

15 Jan 2012, 5:44 AM
#7
drbyte avatar

drbyte

Sensei

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

Post #7 by drbyte

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));

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Jan 2012, 6:33 AM
#8
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #8 by jsquared

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?

J.J. Meddaugh
http://www.atguys.com

15 Jan 2012, 6:50 AM
#9
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #9 by jsquared

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.

J.J. Meddaugh
http://www.atguys.com

15 Jan 2012, 9:40 AM
#10
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #10 by jsquared

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
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);
}
}
?>

J.J. Meddaugh
http://www.atguys.com

15 Jan 2012, 1:22 PM
#11
drbyte avatar

drbyte

Sensei

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

Post #11 by drbyte

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?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Jan 2012, 7:20 PM
#12
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #12 by jsquared

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
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);
}
}
}
}
?>

J.J. Meddaugh
http://www.atguys.com

10 Feb 2012, 4:46 PM
#13
glitzsfa avatar

glitzsfa

New Zenner

Join Date:
Jan 2012
Location:
Fort Worth, Texas
Posts:
14
Plugin Contributions:
0

Post #13 by glitzsfa

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

10 Feb 2012, 5:50 PM
#14
glitzsfa avatar

glitzsfa

New Zenner

Join Date:
Jan 2012
Location:
Fort Worth, Texas
Posts:
14
Plugin Contributions:
0

Post #14 by glitzsfa

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

11 Feb 2012, 12:01 AM
#15
drbyte avatar

drbyte

Sensei

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

Post #15 by drbyte

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

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:```
die('$cust data=' . print_r($cust, true));

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

21 Aug 2012, 7:17 PM
#16
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #16 by jsquared

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.

J.J. Meddaugh
http://www.atguys.com

21 Aug 2012, 7:56 PM
#17
drbyte avatar

drbyte

Sensei

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

Post #17 by drbyte

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.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

22 Aug 2012, 4:47 PM
#18
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #18 by jsquared

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.

J.J. Meddaugh
http://www.atguys.com

22 Aug 2012, 5:07 PM
#19
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,151
Plugin Contributions:
56

Post #19 by lat9

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.

5 Sep 2012, 5:19 PM
#20
jsquared avatar

jsquared

New Zenner

Join Date:
Dec 2005
Location:
Kalamazoo, MI
Posts:
65
Plugin Contributions:
0

Post #20 by jsquared

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?

J.J. Meddaugh
http://www.atguys.com