\Drupal\commerce_product\Entity\ProductVariationInterface::getListPrice has wrong return type

Created on 15 April 2025, 10 days ago

The \Drupal\commerce_product\Entity\ProductVariationInterface::getListPrice has this signature:

/**
 * Gets the list price.
 *
 * @return \Drupal\commerce_price\Price
 *   The list price.
 */
public function getListPrice();

However, the default implementation \Drupal\commerce_product\Entity\ProductVariation::getListPrice can return NULL:

```php
public function getListPrice() {
if (!$this->get('list_price')->isEmpty()) {
return $this->get('list_price')->first()->toPrice();
}

return NULL;
}
```

I think the return type should be @return \Drupal\commerce_price\Price|null, because the list price is an optional field.

The interesting thing about it is that ::getPrice() has a nullable return type, which makes ::getListPrice() confusing, because it's unclear how a variation can have a list price but not have a regular price.

📌 Task
Status

Active

Version

3.0

Component

Developer experience

Created by

🇷🇺Russia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @niklan
  • 🇷🇺Russia niklan Russia, Perm
  • Merge request !433Make ::getListPrice() nullable → (Merged) created by niklan
  • 🇷🇺Russia niklan Russia, Perm

    BTW, this raises the question regarding \Drupal\commerce_product\Entity\ProductVariationInterface::setListPrice, because it accepts only @param \Drupal\commerce_price\Price $list_price. It means there is no direct method to unset the list price. This method could accept NULL for unsetting the list price, or a new method could be introduced ::unsetListPrice(), otherwise it looks like the interface forces you to provide a list price all the time.

    But I think it is not for this issue, just noted.

  • Pipeline finished with Success
    10 days ago
    Total: 606s
    #473602
Production build 0.71.5 2024