Hello,

I am trying to do two different things and I know this is the right module to do it. #1 To fetch a real time market price of a commodity using an API key through a different website. #2 Use that price to create different prices based on quantity of units ordered in four different categories: 1-19, 20-49, 50-100, 100+.

Now this is the code I enter into the custom php field of the custom price UI:

// vars

$apiKey="5af5b6d3d8d2f0b8b27bc2f37a27b72212848dbd";
$apiPath="www.opencurrency.com/argentum/";

// get dynamic prices from Argentum

$basePriceDynamic=file_get_contents('https://'.$apiKey.':X@'.$apiPath.'product/8/price','r');

if ($basePriceDynamic) {

$firstPrice=$basePriceDynamic*1.22;
$secondPrice=$basePriceDynamic*1.20;
$thirdPrice=$basePriceDynamic*1.18;
$fourthPrice=$basePriceDynamic*1.16;

}
if($item->qty = range (1,19,1)) {
$item->price = $firstPrice;
}
if($item->qty = range (20,49,1)) {
$item->price = $secondPrice;
}
if($item->qty = range (50,99,1)) {
$item->price = $thirdPrice;
}
if($item->qty > 100) {
$item->price = $fourthPrice;
}

What this returns is a critical error in uc_price.inc on line 138 which is:

$values['original'] = $price_info['price'] * $price_info['qty'];

This error caused my entire drupal installation to fail and I had to go into myPHP admin and remove the code by hand in the db, which of course corrected the issue.

It seems that I am screwing up $price_info[price] when doing this. Do I need to define some things here with tokens?

Also, just because I am newish to php, am I setting the qty categories wrong by trying to use an array? Is it a mistake to mess with $item->qty this way? Or have I just gotten the syntax wrong?

I am also thinking that I may need to learn how to create a whole new handler here? I am just up against my knowledge wall on this.

Thanks,
Will S.

Comments

tr’s picture

Category: task » support
Issue tags: -price

Your code could use a little improvement. Consider what happens if $basePriceDynamic is FALSE or not set - then your prices will be NULL. I'm guessing that's what happened, and is why uc_price failed. Also, you should use elseif instead of repeated if statements, or use a switch instead.

You can test your code as a standalone PHP script, and print out the values of the variables so you understand what's happening.

The uc_custom_price module is very useful for prototyping pricing schemes such as this one, but once you have it working it would be faster, more maintainable, and safer to put your code into hook_cart_item() in your own custom module.

tr’s picture

Status: Active » Fixed

One other thing, when you say if($item->qty = range (1,19,1)) { you are NOT checking to see if $item->qty is between 1 and 19. What you're actually doing is creating an array with 19 elements, then making $item->qty equal that array instead of a number. What you probably WANT to do is compare the quantity to a number to test if the quantity is between 1 and 19. To do that, you should write if ($item->qty >= 1 && $item->qty <= 19) {.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.