5 replies [Last post]
dalleyasaurus's picture
Offline
Joined: 12/21/2011
Juice: 11

I'm not sure why this code won't work - I'm sure someone else will pick this up instantly.

I'm trying to give the buyer 1 userpoint for every product purchased. Basically they are purchasing credits and there is a volume discount for buying more but they still need to get one credit per product not one credit per dollar. Im using the userpoints and the userpoints ubercart modules in conjuction with conditional actions and custom php to try to make this happen.

userpoints_userpointsapi($order->products[0]->qty);

When I use this it works: userpoints_userpointsapi(50);

So the error is in the $order->products[0]-qty variable or some other thing.

Thank to all those who are smarter than I am.

CCBoise's picture
Offline
Joined: 11/02/2010
Juice: 11
Re: Why won't this custom php code work in conditional actions

I think you're right. It is userpoints_userpointsapi($order->products[0]->qty);

I think it should be userpoints_userpointsapi($order->products[0]['qty']);

dalleyasaurus's picture
Offline
Joined: 12/21/2011
Juice: 11
It actually didnt' work

I tried $order->products[0]['qty']

and this $order->products[0][qty]

and this $order->products[0]->qty

I also tried this

$order->products[1]['qty']

and this $order->products[1][qty]

It's a bit confusing.

Does anyone know what to do?

I also wondered if the 0 in the variable is the first item in the shopping cart or the product number in the store - if the product number in the store then 0 would not be right for me, but if the first time in the cart it would be right because I'm only putting the 1 item in to test the purchase. How can I make sure it works for everything in the cart? (gives a credit for every quantity of every product purchased)

CCBoise's picture
Offline
Joined: 11/02/2010
Juice: 11
Re: It actually didnt' work

The 0 is the order of the product within the $order only. It's the index of the $order array.

DanLanglois's picture
Offline
Joined: 01/11/2012
Juice: 48
Re: Why won't this custom php code work in conditional actions

Ok, you're not sure why this code won't work: userpoints_userpointsapi($order->products[0]->qty);
when you use this it works: userpoints_userpointsapi(50)

I'll start w/the point that I don't think I agree w/#1: "I think it should be userpoints_userpointsapi($order->products[0]['qty']);"
Rather, you were right w/$order->products[0]->qty. However, you need to have that $order populated. How are you populating it, are you using a hook? I think that (I'm just wading into this, actually, I dunno) an order will belong to a user. That user will have a uid. Not all users, however, will have an account. Not all users will have access to, for example, view orders. You need to be sure that you've got an order that is from an account that belongs to a user. So in your code, I'm not sure what $order is from, how did you set it? Off the cuff, I'll suggest that you consider how a link like 'user/' $account->uid.'/orders' should bring up an order history. Are you to the point, in your custom php, where you've got this ability would that work? Hope this is putting us on the right track..

CCBoise's picture
Offline
Joined: 11/02/2010
Juice: 11
Re: Why won't this custom php code work in conditional actions

Probably a better way to do this is to use foreach to roll through the products in an order. Once you do that you can access each product individually. Since you are trying to give points for products purchased overall and not individually.

So maybe something like the following:

<?php
$countpoints
= 0;
foreach(
$order->products as $product) {
 
$countpoints .= $product->qty;
}
userpoints_userpointsapi($countpoints);
?>