generated from compucorp/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammad Shahrukh
committed
Sep 8, 2024
1 parent
99958b2
commit c3aecb0
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Civi/Financeextras/Hook/Post/UpdateLineItemPriceFieldValues.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Civi\Financeextras\Hook\Post; | ||
|
||
class UpdateLineItemPriceFieldValues { | ||
|
||
public function __construct( | ||
private string $op, | ||
private string $objectName, | ||
private int $objectId, | ||
private $objectRef, | ||
) { | ||
} | ||
|
||
public function run() { | ||
$this->updatePriceFieldValues(); | ||
} | ||
|
||
private function updatePriceFieldValues(): void | ||
{ | ||
try { | ||
$record = (array) $this->objectRef; | ||
|
||
if ($this->objectName !== 'LineItem' || !in_array($this->op, ['create', 'edit']) | ||
|| ((!in_array($record['price_field_id'], [NULL, 'null'])) && (!in_array($record['price_field_value_id'], [NULL, 'null'])))) { | ||
return; | ||
} | ||
|
||
$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); | ||
|
||
$lineItem = \Civi\Api4\LineItem::update(FALSE); | ||
|
||
if (in_array($record['price_field_id'], [NULL, 'null'])) { | ||
$lineItem->addValue('price_field_id', $priceFieldID); | ||
} | ||
if (in_array($record['price_field_value_id'], [NULL, 'null'])) { | ||
$lineItem->addValue('price_field_value_id', $priceFieldValueID); | ||
} | ||
|
||
$lineItem->addWhere('id', '=', $this->objectId)->execute(); | ||
|
||
} catch (\Throwable $e) { | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters