Zen Cart Logo
Forums / General Questions / php help please - set variable based on items on order

php help please - set variable based on items on order

Views: 1,296

Results 1 to 12 of 12
7 Aug 2014, 3:24 PM
#1
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

php help please - set variable based on items on order

Hi guys, one for the php gurus, hoping someone will be able to advise a quick lightweight solution.

I wish to set a variable to true if the following conditions on a specific order exists:

NUMBER of Products ordered = 1 & Quantity of that product ordered = 1

Order ID will obviously be known in this script so based on that I just want to do a quick check for the condition and if true set the variable.

Cheers in advance.

Phil

7 Aug 2014, 3:40 PM
#2
lat9 avatar

lat9

Administrator

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

Re: php help please - set variable based on items on order

I've checked this out in a couple of scenarios and it seems to work!

$order_check = $db->Execute ("SELECT count(*) as total, products_quantity FROM" . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = $orders_id");
$one_product_ordered = (!$order_check->EOF && $order_check->fields['total'] == 1 && $order_check->fields['products_quantity'] == 1);
7 Aug 2014, 3:50 PM
#3
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

I was just about to post my poor attempt at the logic, yours look much neater! thank you :)

$restult1 = SELECT count(*) as count FROM `orders_products` WHERE `orders_id` = 14532;

if $result1 = 1 {  

$result2 = SELECT `products_quantity` FROM `orders_products` WHERE `orders_id` = 14532;

if $result2 = 1 { 

$oneItemOrder = true;

}

} 
7 Aug 2014, 3:54 PM
#4
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

lat9:

I've checked this out in a couple of scenarios and it seems to work!

$order_check = $db->Execute ("SELECT count(*) as total, products_quantity FROM" . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = $orders_id");
$one_product_ordered = (!$order_check->EOF && $order_check->fields['total'] == 1 && $order_check->fields['products_quantity'] == 1);


thanks again for this, apologies as its been way to long since I've played with php. am I right in assuming that if I want to do something based on this I can simply use:

```php
if ($one_product_ordered) {
do something
}

or does it need to be

if ($one_product_ordered = true) {
do something
}

?

7 Aug 2014, 3:54 PM
#5
lat9 avatar

lat9

Administrator

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

Re: php help please - set variable based on items on order

You're welcome!

[edit, responding to additional question] Either of the code fragments that you posted will work, making sure to change the 2nd one to

if ($one_product_ordered == true) {
do something
}  
7 Aug 2014, 3:59 PM
#6
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

perfect thanks. Once I have worked out the rest of my code that goes in the do something bit I will report back and let you know how I got on :)

7 Aug 2014, 7:54 PM
#7
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

lat9:

I've checked this out in a couple of scenarios and it seems to work!

$order_check = $db->Execute ("SELECT count(*) as total, products_quantity FROM" . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = $orders_id");
$one_product_ordered = (!$order_check->EOF && $order_check->fields['total'] == 1 && $order_check->fields['products_quantity'] == 1);


WORKS PERFECTLY! thank you so much :)
13 Aug 2014, 5:22 PM
#8
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

Hi lat9.

So I have everything working perfectly on a test file. However, when I include the code to the checkout process I get the following error:

[13-Aug-2014 16:58:02 UTC] PHP Fatal error: Call to a member function Execute() on a non-object in /home/site/public_html/includes/mycode-includes.php on line 285

I include the code using the following:
include('includes/mycode-includes.php');

this file is included directly after the sending of emails in the includes/classes/order.php file

This works for my code I already had previously but it doesn't seem to like the new code.

Its worth noting that code I previously used never used the execute function hence why I haven't had this error before. When I look at the code in the order.php file there are many times it is using the Execute function so I am a little confused why its not liking it?

Any ideas?

Thanks

Phil

13 Aug 2014, 5:41 PM
#9
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

I have as a test simply added this line only directly to the includes/classes/order.php file

$order_check = $db->Execute ("SELECT count(*) as total, products_quantity FROM" . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = $orders_id");

I still get the error:
[13-Aug-2014 17:37:17 UTC] PHP Fatal error: Call to a member function Execute() on a non-object in /home/site/public_html/includes/classes/order.php on line 1106

I am confused :shocking:

13 Aug 2014, 5:57 PM
#10
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

also worth noting that actually my code is included in the function send_order_email

I notice that at the top of the function there is the following:
global $currencies, $order_totals;

actually when I previously said other instances of the db->Execute were being used, I've noticed this are obviously within other functions. An looking at these functions these actually have this at the top:
global $db;

So am I right in thinking that is I amend the function send_order_email first part to:
global $currencies, $order_totals, $db;

then it should resolve my problem???

13 Aug 2014, 6:18 PM
#11
lat9 avatar

lat9

Administrator

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

Re: php help please - set variable based on items on order

philip937:

Hi lat9.

So I have everything working perfectly on a test file. However, when I include the code to the checkout process I get the following error:

[13-Aug-2014 16:58:02 UTC] PHP Fatal error: Call to a member function Execute() on a non-object in /home/site/public_html/includes/mycode-includes.php on line 285

I include the code using the following:
include('includes/mycode-includes.php');

this file is included directly after the sending of emails in the includes/classes/order.php file

This works for my code I already had previously but it doesn't seem to like the new code.

Its worth noting that code I previously used never used the execute function hence why I haven't had this error before. When I look at the code in the order.php file there are many times it is using the Execute function so I am a little confused why its not liking it?

Any ideas?

Thanks

Phil
Since your code is now running within a function, make sure that you've got 'access' to the $db class; add the following to your new module:

global $db;
13 Aug 2014, 6:27 PM
#12
philip937 avatar

philip937

Totally Zenned

Join Date:
Aug 2009
Location:
Bedford, England
Posts:
968
Plugin Contributions:
0

Re: php help please - set variable based on items on order

lat9:

Since your code is now running within a function, make sure that you've got 'access' to the $db class; add the following to your new module:

global $db;


yey I was right. thanks for confirming. Now seems to be working :)