Skip to content

Commit

Permalink
COMCL-770: Use the price field and price field value of default priceset
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Sep 19, 2024
1 parent 2e6cfee commit 0758ade
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 16 additions & 3 deletions CRM/Financeextras/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,24 @@ public function upgrade_1003() {
try {
$priceSetId = \CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
$priceSet = current(\CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
$priceFieldID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($priceSet);
$priceFieldValueID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($priceSet);
$priceField = NULL;
foreach ($priceSet['fields'] as $field) {
if ($field['name'] == 'contribution_amount') {
$priceField = $field;
break;
}
}

if (empty($priceField)) {
return TRUE;
}
$priceFieldValueID = current($priceField['options'])['id'] ?? NULL;
if (empty($priceFieldValueID)) {
return TRUE;
}

\Civi\Api4\LineItem::update(FALSE)
->addValue('price_field_id', $priceFieldID)
->addValue('price_field_id', $priceField['id'])
->addValue('price_field_value_id', $priceFieldValueID)
->addClause('OR', ['price_field_id', 'IS NULL'], ['price_field_value_id', 'IS NULL'])
->execute();
Expand Down
19 changes: 16 additions & 3 deletions Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,26 @@ private function updatePriceFieldValues(): void {

$priceSetId = \CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
$priceSet = current(\CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
$priceFieldID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($priceSet);
$priceFieldValueID = \CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($priceSet);
$priceSet = current(\CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
$priceField = NULL;
foreach ($priceSet['fields'] as $field) {
if ($field['name'] == 'contribution_amount') {
$priceField = $field;
break;
}
}
if (empty($priceField)) {
return;
}
$priceFieldValueID = current($priceField['options'])['id'] ?? NULL;
if (empty($priceFieldValueID)) {
return;
}

$lineItem = \Civi\Api4\LineItem::update(FALSE);

if (in_array($record['price_field_id'], [NULL, 'null'])) {
$lineItem->addValue('price_field_id', $priceFieldID);
$lineItem->addValue('price_field_id', $priceField['id']);
}
if (in_array($record['price_field_value_id'], [NULL, 'null'])) {
$lineItem->addValue('price_field_value_id', $priceFieldValueID);
Expand Down

0 comments on commit 0758ade

Please sign in to comment.