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