0

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(&$items)
{
  print '<pre>'; print_r($items); die(); <--- this makes apache run out of memory
  print '<pre>'; print_r(arrray_key($items)); die(); <--- this prints out "2", and then "3" and then "5" etc. 
  $items[45]->price = 29393; <--- this doesnt adjust the price

}

I was hoping to do something like this:

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

How can I accomplish this?

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

0

Figured it out:

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

Hope it helps someone!

** IMPORTANT **

It looks like if you use

$item->price->setValue($item->price->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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.