How do I alter prices? This is a follow up to Ubercart alter product price when product added to cart.

I have an attribute called "Billing Term". It has options: 6 months, 12 months and 24 months. If 6 months is selected, 0% discount must be applied. 6 months, 5% discount must be applied. and 24 months, 10% discount must be applied.

I set up the attributes to make no change to the price, because I can't find a way to make attributes adjust a price by percentage.

The best solution to my problem would be to find a way to specify attribute discounts in %. But I couldn't find a way to do that. If you know of a way, please do tell, as that is for sure better than my other options below.

So, with all attribute adjustments to be 0, I figured I could something similar to what was done here (but this post is for Drupal 7). So I tried this:

function comply_advantage_uc_cart_alter(&amp;$items)
{
  print '&lt;pre&gt;'; print_r($items); die(); &lt;--- this makes apache run out of memory
  print '&lt;pre&gt;'; print_r(arrray_key($items)); die(); &lt;--- this prints out "2", and then "3" and then "5" etc. 
  $items[45]-&gt;price = 29393; &lt;--- this doesnt adjust the price


}

I was hoping to do something like this:

$items[5]-&gt;price = $items[5]-&gt;price * 0.95; // to apply 5% discount


How can I accomplish this?

I found out that $items[5] type is CartItem.
Figured it out:

function comply_advantage_uc_cart_alter(&amp;$items)
{
    $item-&gt;price-&gt;setValue($item-&gt;price-&gt;value * 0.95);
}


Hope it helps someone!

** IMPORTANT **

It looks like if you use 

$item-&gt;price-&gt;setValue($item-&gt;price-&gt;value * 0.95);


Yo might end up altering the price several times. So I had to actual recalculate the price completely, without using $item->price->value, or it would deduct 5% multiple times.