Orders Gateway Template - how to access deeply nested item array

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
kendo451

Orders Gateway Template - how to access deeply nested item array

Post by kendo451 »

Hi folks.

I've been working on my first implementation of Products, Orders, Cart, PaypalGateway for the past couple of weeks.

Thanks to Calguy and others excellent work on these modules, everything is working great!

However, I am having difficulty figuring out how to access the Orders->items object in the Gateway Template (after the order comes back from the gateway).

I used {get_template_vars} and {$order|@print_r} to obtain this structure of the object:

Code: Select all

$Order
225JoeSmith3610 River RoadMendotaVA24270US2766693455joeSmith@gmail.com0.02000.02proposed2009-08-04 07:16:242009-08-04 07:16:24Array ( [0] => Array ( [id] => 3 [order_id] => 2 [shipping_first_name] => Joe [shipping_last_name] => Smith [shipping_address1] => 3610 River Road [shipping_address2] => [shipping_city] => Mendota [shipping_state] => VA [shipping_postal] => 24270 [shipping_country] => US [shipping_phone] => 2766693455 [shipping_fax] => [shipping_email] => joeSmith@gmail.com [shipping_message] => [subtotal] => 0.02 [tax] => 0 [shipping_cost] => 0 [total] => 0.02 [weight] => 0 [create_date] => 2009-08-04 07:16:24 [modified_date] => 2009-08-04 07:16:24 [items] => Array ( [0] => Array ( [id] => 3 [order_id] => 2 [shipping_id] => 3 [item_id] => 1 [quantity] => 2 [product_name] => Test Payment  [details] => [price] => 0.01 [weight] => 0 [status] => notshipped [create_date] => 2009-08-04 07:16:24 [modified_date] => 2009-08-04 07:16:24 ) ) ) ) Array 
$item
32312Test Payment 0.010notshipped2009-08-04 07:16:242009-08-04 07:16:24Array
However, I haven't been able to figure out how to print_r or access the items.

I tried {$order->items|@print_r} but that just returns a 1.

Thanks for any help!

Cart 1.4.2
Products 2.4
Orders 1.4
PaypalGateway 2.0
CGPaymentGatewayBase 1.0
Last edited by kendo451 on Mon Aug 10, 2009 5:06 pm, edited 1 time in total.
kendo451

Re: Orders Gateway Template - how to access item array

Post by kendo451 »

I post a lot of dumb questions, but I'm trying to figure this out for myself.

First, in the Gateway Complete template, there are several Smarty variables that give access to the important information:

$ordernumber = the Orders module order number
$email_address = buyer's email address
$status = SUCCESS if order was successful
$gateway_success = the actual success code from the Gateway, in the case of Paypal success = "payment_approved"
$transaction_id = the Gateway's transaction ID
$tmp = a number that appears to be the transaction amount. Not sure if this is intended to be used.

There are also array/objects:

$item = Array(12) ?  I have not gotten this to print_r as anything but 1, maybe I'm doing it wrong.
$shipping = Array(22)

********************************************************

Ok, so I want to write a UDT that takes the order number and does something with it.  It appears there are two different ways I could do this:

1. Put a UDT in the Gateway Complete Template of the Orders module.  With this I could use the $ordernumber to look up a transaction in the Orders database and then do something with it.

OR

2. Orders Module registers an event "PostGatewayComplete".  We can write a UDT and use Event Manager to call it when PostGatewayComplete is fired.  Question: what data is captured or passed in this event?

Looking in the Orders Code, you can find the relevant piece of info:

At the very end of action.gateway_complete.php you find the DoEvent call:

Code: Select all

// Send Event w/ Order information
Events::SendEvent('Orders', 'PostGatewayComplete', array('order_id' => $order_id, 'order' => $order));
So from this I see that two parameters are passed in the $params array: 'order_id' and 'order'

So now I can write a UDT that takes that info and does something.

Code: Select all

global $gCms;
$ordernumber = $params['order_id'];
$order = $params['order'];

code to do something....
I want to know more about the "order" object that is being passed here, so I can retrieve the item id's and quantities.  So search around in the Orders module code some more I notice that $orders, $order, and $item are arrays of key=>value pairs, not objects as I had thought.  So I was using the object->notation and that is why I was failing to get the values printed from item.

Here is the structure of the orders array in a way that is easy to read:

Code: Select all

Array ( 
	[0] => Array ( 
		[id] => 3 
		[order_id] => 2 
		[shipping_first_name] => Joe 
		[shipping_last_name] => Smith 
		[shipping_address1] => 3610 River Road 	
		[shipping_address2] => 
		[shipping_city] => Mendota 
		[shipping_state] => VA 
		[shipping_postal] => 24270 
		[shipping_country] => US 
		[shipping_phone] => 2766693455 
		[shipping_fax] => 
		[shipping_email] => joeSmith@gmail.com 
		[shipping_message] => 
		[subtotal] => 0.02 
		[tax] => 0 
		[shipping_cost] => 0 
		[total] => 0.02 
		[weight] => 0 
		[create_date] => 2009-08-04 07:16:24 
		[modified_date] => 2009-08-04 07:16:24 
		[items] => Array ( 
			[0] => Array ( 
				[id] => 3 
				[order_id] => 2 
				[shipping_id] => 3 
				[item_id] => 1 
				[quantity] => 2 
				[product_name] => Test Payment  
				[details] => 
				[price] => 0.01 
				[weight] => 0 
				[status] => notshipped 
				[create_date] => 2009-08-04 07:16:24 
				[modified_date] => 2009-08-04 07:16:24 
				) 
			) 
		) 
	) 
 

Likewise, here is the $item array in the same format:

Code: Select all

Array ( 
				[id] => 3 
				[order_id] => 2 
				[shipping_id] => 3 
				[item_id] => 1 
				[quantity] => 2 
				[product_name] => Test Payment  
				[details] => 
				[price] => 0.01 
				[weight] => 0 
				[status] => notshipped 
				[create_date] => 2009-08-04 07:16:24 
				[modified_date] => 2009-08-04 07:16:24 
				) 
This information is available in the Gateway Complete Template as well as passed by the PostGatewayComplete event.

I hope this will be helpful to other intermediate beginners like me.

Have a nice day.
Last edited by kendo451 on Wed Aug 05, 2009 12:12 am, edited 1 time in total.
kendo451

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by kendo451 »

Unfortunately, I know the data is in $order, but I've tried every which way I can think of and I can't access it.

{$order->items} doesn't work

{$order['items']} doesn't work

So, I tried making a UDT that attaches to the event, PostGateWayComplete because it passes $orders as a parameter.

But after lots of errors and hour of debugging, I found that Orders doesn't include an $items array in the 'order' param that it passes to the event, even though it does pass $items in the order to the PostGateway template.

So, I'm going to have to fish the items out of the database, which is well beyond my depth at this point in my career as a CMSMS hack.

Here's what I tried but it doesn't work.  Can anyone help me correct this db query so that I can pull the items from the Order database?

Code: Select all

$order = $params['order'];
$order_id = $params['order_id'];

// $order doesn't include items so we'll have to pull it from the db
$query = 'SELECT * FROM '.cms_db_prefix().'module_orders_items
	           WHERE order_id = ?';
$items = $db->GetArray($query,array($order_id));

$test = 'Order_ID = '.$order_id.', Items = '.print_r($items).', Order = '.print_r($order);
I then emailed  $test to myself and here is what it had:

Order_ID = 15, Items = 1, Order = 1

Sigh. Pulling my hair out over this.  A wee bit of documentation on Orders would be wonderful.
Jeff
Power Poster
Power Poster
Posts: 961
Joined: Mon Jan 21, 2008 5:51 pm

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by Jeff »

This isn't tested, but if you look in the smarty manual(http://www.smarty.net) I believe to access array elements is $order.item
kendo451

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by kendo451 »

I never figured out how to get what I needed out of the $orders array, but I did find that there is an array, $item that had what I needed.

I don't know if it would have been "$items" if more than one product were purchased, but for now this works to pass the value to my UDT:

{post_gateway_complete pid=$item.item_id qty=$item.quantity}
Deak

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by Deak »

I am having very similar issues/confusion over the Change Settings Template for the FEU module. By default it loops through the Member Property fields and displays them in an unhelpful order. If I could figure out the syntax to access the array and display the fields manually it would be massively helpful!
kendo451

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by kendo451 »

Deak, you can control the order of the fields using the up/down arrows on the FEU Groups Tab.  Just click the group name and when you see the list you can adjust sort-order up and down.
Deak

Re: Very NOT SOLVED!!! Orders Gateway Template - how to access item array

Post by Deak »

Kendo, thanks for point that out... I think I had some kind of brain malfunction here. There was a re-ordering problem, but that's a JavaScript error in IE8 but I got around that by using Firefox. God knows what I was thinking with my question -- juggling too many things.

However, I was interested in knowing how to dump out these properties so that I could group them perhaps. Not so important, though.
kendo451

Re: Orders Gateway Template - how to access deeply nested item array

Post by kendo451 »

Update:

PaypalGateway v 2.0 and Orders v 1.4

In the PostGatewayComplete Event the Orders module passes the $order array, but it does not include the items in that order.  So, I was trying to find the items in the array, but they aren't there in the first place.  If you want to write a UDT that does something based on which items were in the order, you have to pull the order's record from the database yourself.

In the PostGatewayComplete template, displayed to the buyer, the $order array does include an $items array nested down in it.  This template also include $item, but I have not experimented looking at this in order with multiple items.

As for my original problem, how to access the items buried several levels deep in the $order, I never did solve the smarty syntax for that.
Locked

Return to “Modules/Add-Ons”