Skip to content

Fix invalid price field id

Created by: ahed-compucorp

Overview

This PR fixed the issue when adding more than one line item at a time on CiviCRM 5.35.1 .

Before

Peek 2021-03-29 15-34

After

Peek 2021-03-29 15-37

Technical Details

This extension will create price field for each new line item inside the foreach loop.

foreach ($lineItemParams as $key => $lineItem) {
  if ($lineItem['price_field_value_id'] == 'new') {
    list($lineItem['price_field_id'], $lineItem['price_field_value_id']) = CRM_Lineitemedit_Util::createPriceFieldByContributionID($entityID);
  }
      
  ...
  $newLineItem[] = civicrm_api3('LineItem', 'create', $newLineItemParams)['id'];
}

When creating the line item CiviCRM internally will validate each price field and one of the validation steps is fetching the potential values/options to check against - see utils.php#L2276.

$options = civicrm_api($entity, 'getoptions', $options_lookup_params);

CiviCRM will cache getoptions result in PseudoConstant.php#L278

\Civi::$statics[__CLASS__][$cacheKey] = $output;

Everything is fine for one line item creating but when we create two items or more in one go, the price fields ids was cached in the first iteration during the creating of the first line item and CiviCRM will throw error when creating the second line item because the price field is invalid.

To overcome this issue, I moved the creation of line items outside of the foreach loop. That way the main foreach loop will prepare the params and create the required price fields and then will create the line items in the new foreach loop.

Merge request reports

Loading