How do I count the number of items of same product class in cart?
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;
}