19 replies [Last post]
glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Was this information Helpful?

Hi

Am I able to use a token (specifically [order-id]) as a php variable?

I have a custom content field called "Venue" which I want to extract and show on my invoice.

Also, I need to display the start and end time for the event they have purchased.

Thanks

Glenn

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Use token as a php variable?

No, Tokens only function as text replacements, not as variable in any sort of PHP afaik.

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: Re: Use token as a php variable?

There's no way to echo or print it out?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Use token as a php variable?

If the order object itself is available, you can just use $order->order_id.

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: Re: Re: Re: Use token as a php variable?

OK, I'll give that a whirl.

Thanks

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: Re: Re: Re: Re: Use token as a php variable?

Is there a list somewhere of all the variable that can be used?

e.g.

$order->order_id
$order->product_category
$product->field_custom
$event->start

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Re: Re: Re: Use token as a php variable?

Unfortunately, no, but you can use print '<pre>'. print_r($order, TRUE) .'</pre>'; to see it all.

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
This returns: [products] =>

This returns:

[products] => Array
        (
            [0] => stdClass Object
                (
                    [order_product_id] => 77
                    [order_id] => 1002453
                    [nid] => 53

So could I use $products->nid to return the node id to use in a SELECT query?

Then I can go

$nid = $products->nid;
db_query("SELECT venue, event-start, event-end FROM {node} WHERE nid = %d")

Thanks

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: This returns: [products] =>

Part of the whole thing it returns i:

[products] => Array
        (
            [0] => stdClass Object
                (
                    [order_product_id] => 77
                    [order_id] => 1002453
                    [nid] => 53
                    [title] => New Course
                    [manufacturer] => 
                    [model] => 
                    [qty] => 1
                    [cost] => 0.00
                    [price] => 1000.00
                    [weight] => 0
                    [data] => Array
                        (
                            [attributes] => Array
                                (
                                    [Course Modules] => Theory module
                                )

                            [model] => 
                            [shippable] => 0
                            [restrict_qty] => 0
                        )

                )

        )

How would I get the

[Course Modules] => Theory module

returned as a variable that I can use?

Thanks

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: Re: This returns: [products] =>

Ryan, on a very similar line, I need some help with this.

Thanks heaps.

Glenn

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Notice that $products is an

Notice that $products is an array, so you must specify which product in the array you want. You can't make assumptions about what will be there, though, so we generally loop over arrays and act on each member of it. To print out the Course Modules value for each product on the order, I'd use:

<?php
foreach ($order->products as $product) {
  print
check_plain($product->data['attributes']['Course Modules']);
}
?>
glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: Notice that $products is an

Great, thanks. What about #7?

Cheers

Glenn

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Notice that $products is an

You'd use the same loop as I posted above but with $product->nid instead.

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Awesome, thanks heaps. I am

Awesome, thanks heaps.

I am putting all of this code into page nodes. A friend of mine who is a very experienced Drupal developer was horrified when I told him this, and suggested I create modules instead.

This is a great idea, except I've no idea how. Is there a good, simple, on-line how to for developing modules? I see that Pro Drupal Development seems to be a good book, but it will take a month for it to arrive in NZ, and I can't find it in the library. Is there on online resource?

Thanks
Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Ryan This is my code at the

Ryan

This is my code at the moment:

<?php
foreach ($order->products as $product) {
 
$nid = ($product->nid);
}
$venue = db_query("SELECT field_venue_value FROM {content_type_course} WHERE nid = %d", $nid['nid']);
$dates = db_query("SELECT event_start, event_end FROM {event} WHERE nid = %d", $nid['nid']);
echo
"<p><strong>Venue: </strong>" . $venue['venue'] . "</p>";
echo
"<p><strong>Dates: </strong>" . date('d M Y - h:m', $dates['event_start']) . " to " . date('d M Y - h:m', $dates['event_end']) . "</p>";
?>

This isn't working at the moment, the venue, start and end times aren't working.

This looks like the rationale won't work, but each order will only ever have products from the same node.

Thanks

Glenn

marcus178's picture
Offline
Joined: 04/13/2009
Juice: 113
Re: Use token as a php variable?

Did you ever work this out. I'm trying to load a cck event date for each product but I can't work it out. It's OK for one item but it goes all wrong if you have 2 different products

Chris Herberte's picture
Offline
Joined: 10/01/2008
Juice: 48
Re: Use token as a php variable?

Answer to OP's question. Yes, use single quote, eg..

<?php
$orderid
= '[order-id]';
?>
Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Re: Use token as a php variable?

I don't think that's what the OP meant at all. If you do that, you've just got a string that can be replaced like a token. However, if you're going to be doing that replacement, then you need an order object, and if you have an order object, then you can just do

<?php
$order_id
= $order->order_id;
?>

and not worry about tokens at all.

Tokens are meant to be used when you have a bunch of data you want to insert into some text. If all you're worried about is the data itself, then you don't need to use tokens.

junior's picture
Offline
Joined: 12/07/2009
Juice: 35
Problem with variables

Hi

Firstly, sorry for my english!!!

I need to control one token through a if then sentence, the problem is that the server does not recognise the token, and i can´t control it.

I have tested to put print_r($order), and this solution does not function too.

I don´t know what can i do.....

Thanks a lot!!!

junior's picture
Offline
Joined: 12/07/2009
Juice: 35
My solution

Hi!!

Finally, i obtained one solution searching in sql in table uc_orders.

Thanks!