How do I count the number of items of same product class in cart? I'm a newbie to PHP, I and can't figure out how to do this.

I'm trying to alter the  price of each items depending on the quantity of the items of same product class in the cart. Let's say I have a product class "fruit" and 10 items like "apple" ,"orange", "banana" and so on. And no matter which combination, I would like to make the first item $10, the second one $0, the third one $0. So I tried this code by using UC Custom Price but doesn't work.

$firstPrice = 10;
$secondPrice = 0;
$thirdPrice = 0;

if ($item-&gt;qty = 1) {
  $item-&gt;price = $firstPrice;
}
elseif($item-&gt;qty = 2) {
  $item-&gt;price = $secondPrice;
}
elseif ($item-&gt;qty = 3) {
  $item-&gt;price = $thirdPrice;
}


It obviously seems that $item-&gt;qty is the quantity of a single item (the same node).  So how can I call do this with the number of the items of this product class "fruit "in the cart?  $item-&gt;price is the final price which will be used.
Your code is wrong because, instead of comparing the value of $item-&gt;qty, you are setting them in each control statement. Replace = with ==.

if ($item-&gt;qty == 1) {
  $item-&gt;price = $firstPrice;
}
elseif($item-&gt;qty == 2) {
  $item-&gt;price = $secondPrice;
}
elseif ($item-&gt;qty == 3) {
  $item-&gt;price = $thirdPrice;
}


The code you wrote works because an assignment statement is also an expression that have the assigned value as its value.

Instead of writing an IF-statement, you could used a SWITCH-statement.

switch ($item-&gt;qty) {
  case 1:
    $item-&gt;price = $firstPrice;
    break;

  case 2:
    $item-&gt;price = $secondPrice;
    break;

  case 3:
    $item-&gt;price = $thirdPrice;
    break;
}