Answers.Body: <p>Would suggest using the <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildAmount/" rel="nofollow noreferrer">hook_civicrm_buildAmount</a> hook as you won't need to handle the amount change at multiple places.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-17T14:17:57.073Z
Answers.Text: Thanks @Pradeep Nayak. I tried this but didn't work: `add_action('civicrm_buildAmount', function ($pageType, $form, $amount) { $amount[737]['options'][1486]['amount'] = 29; }, 10, 3);`
Answers.UserDisplayName: marcelo2605
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-20T08:54:55.333Z
Answers.Text: did you replace it correctly?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-17T14:01:49.500Z
Answers.LastActivityDate: 2024-05-17T14:01:49.500Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I'm trying to use the hook <code>civicrm_buildForm</code> to modify the contribution value based on specific logic.</p>
  <p>I know I can use <code>setDefaults()</code> but I don't know how to get the field ID.</p>
  <p>For this contribution form, I'm using a Price set.</p>
  <h3>Update</h3>
  <p>I tried this, but it didn't work:</p>
  <pre><code>if (isset($form-&gt;_priceSet['fields'])) {
      foreach ($form-&gt;_priceSet['fields'] as &amp;$field) {
          if ($field['name'] == 'amount') {
              foreach ($field['options'] as &amp;$option) {
                  if ($option['label'] == 'Amount') {
                      $option['amount'] = 50;
                  }
              }
          }
      }
  }
  </code></pre>
  
Tags: <wordpress><contribution-page><hooks><price-sets>
Title: Change the contribution value programatically
ThreadText: # Change the contribution value programatically
  
  (Asked by: username_0 on 2024-05-17)
  
  I'm trying to use the hook `civicrm_buildForm` to modify the contribution value based on specific logic.
  
  I know I can use `setDefaults()` but I don't know how to get the field ID.
  
  For this contribution form, I'm using a Price set.
  
  ### Update
  
  I tried this, but it didn't work:
  
  ```
  if (isset($form->_priceSet['fields'])) {
      foreach ($form->_priceSet['fields'] as &$field) {
          if ($field['name'] == 'amount') {
              foreach ($field['options'] as &$option) {
                  if ($option['label'] == 'Amount') {
                      $option['amount'] = 50;
                  }
              }
          }
      }
  }
  
  ```
  
  **Tags:** wordpress, contribution-page, hooks, price-sets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-05-17. Score: 0
  
  Would suggest using the [hook\_civicrm\_buildAmount](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildAmount/) hook as you won't need to handle the amount change at multiple places.
  
  > **Comments:**
  > **username_0**: Thanks @username_1. I tried this but didn't work: `add_action('civicrm_buildAmount', function ($pageType, $form, $amount) { $amount[737]['options'][1486]['amount'] = 29; }, 10, 3);`  
  > **username_1**: did you replace it correctly?  
  
  ---
  
  