<p>In your current example, you are not actually saving an entry. You are just setting up an <code>onSaveEntry</code> event listener. To save the entry you will need to actually save the entry using the [saveEntry][1] method.</p>

<p>UPDATE: I'm not exactly sure what your setup is — but some variation on the following should work for you.</p>

<ol>
<li><p>In the CMS, add a field called 'invoiceID', which you will use to store the invoice entryId to the charge (or 'invoiceEntryId' or whatever you want to call it).</p></li>
<li><p>Add the 'invoiceId' field to the charge field layout in charge->settings->fields.</p></li>
<li><p>In the charge form, add the 'invoiceId' field to your form (this assumes that you are making the charge from the invoice entry page, otherwise you will need to use a reference to the invoice entry id through whatever relationship you have set up):</p>

<pre><code>&lt;input type="hidden" name="fields[invoiceId]" value="{{ entry.id }}"/&gt;
</code></pre></li>
<li><p>In your plugin, add the onCharge event to grab the 'invoiceId' from the charge event, then retrieve, update, and save the entry.</p>

<pre><code>craft()-&gt;on('charge.onCharge', function(Event $event) {

    // get the charge model
    $charge = $event-&gt;params['charge'];

    // retrieve the invoiceId (aka entryId) saved on the charge model
    $entryId = $charge-&gt;invoiceId;

    // uncomment to test, if needed
    //LogicPlugin::log('Invoice ID: '.$charge-&gt;invoiceId, LogLevel::Info);

    // get the invoice entry from the invoiceId
    $entry = craft()-&gt;entries-&gt;getEntryById($entryId);

    // update the invoicePaid field (in this case I'm using a switch)
    $entry-&gt;getContent()-&gt;invoicePaid = '1';

    // save the entry
    $success = craft()-&gt;entries-&gt;saveEntry($entry);

    // log errors
    if (!$success) {
        LogicPlugin::log('Couldn’t save the entry "'.$entry-&gt;title.'"', LogLevel::Error);
    }

});
</code></pre></li>
</ol>

CC BY-SA 3.0
2018-02-12T05:38:35.620Z
Unfortunately it comes back with  _Undefined index: entry_  error message. Been trying to get this to show anything, but so far no luck
herbo
CC BY-SA 3.0
2018-02-13T17:45:53.380Z
I don't have charge installed, so can't investigate directly, but from the docs it looks like charge provides it's own element type (i.e. charge) and models (i.e. charge, payments, customer, subscription, account). You are probably therefore not working with an 'entry' model. You may need to dig into the charge plugin and see what you are working with, and what methods it uses for saving elements (i.e. `craft()->charge()->saveCharge()` or similar) — or try just using the 'element' methods, which should apply to all elements in craft.
Douglas McDonald
CC BY-SA 3.0
2018-02-13T17:55:36.883Z
More specifically, it looks like the onCharge event returns an attribute called `charge` which contains a reference to the charge model (i.e. `$charge = $event->params['charge'];`. How you modify and save that is another question, but you might try `craft()->charge_charge->saveCharge($charge)` which I found by searching the plugin source files.
Douglas McDonald
CC BY-SA 3.0
2018-02-15T12:53:27.613Z
Using the `$charge = $event->params['charge'];` I'm trying to get the entry Id, which is posted as `meta[id]`. I can get it using `{{ dump(charge['meta']['id']) }}`, but unable to call it in the plugin, so it could be used
 `$id = charge()->meta()->id();
    $entry = craft()->entries()->getEntryById($id);`
herbo
CC BY-SA 3.0
2018-02-15T20:40:30.797Z
I installed the plugin on a test install, and was able to get a solution set up. I updated the answer. See if that works. (FYI... I'm using my LogicPlugin for logging. Change to `Craft::Log` if you want it to go the craft log.)
Douglas McDonald
CC BY-SA 3.0
2018-02-20T03:31:05.667Z
Thank you, this worked. It took a while to see it work as I was having a plugin problem that was affecting it, but it works now.
herbo
CC BY-SA 3.0
2018-02-10T18:00:26.303Z
2018-02-16T21:09:37.620Z
2018-02-16T21:09:37.620Z
Douglas McDonald
<p>I have an invoice that is sent to stripe to be paid. When the thanks.html is shown, I would like the invoices section's invoicePaid dropdown field to change from "No" to "Yes" using the charge.onCharge hook.
I tried the following( and variations ) without success.</p>

<pre><code> {
    craft()-&gt;on('charge.onCharge', function(Event $event) {
        craft()-&gt;on('entries.onSaveEntry', function(Event $event ){

            //Is the entry in the section 'invoices'?
            if ($event-&gt;params['entry']-&gt;section == 'invoices') {

            //Replace values
            $event-&gt;params['entry']-&gt;getContent()-&gt;invoicePaid = 'Yes';
            }
        });


    });
}
</code></pre>

<p>Where am I going wrong, or is there a better way to go about this?</p>

<plugins><plugin-charge><hook>
How do I edit a dropdown field in the charge.onCharge hook?
# How do I edit a dropdown field in the charge.onCharge hook?

(Asked by: username_0 on 2018-02-10)

I have an invoice that is sent to stripe to be paid. When the thanks.html is shown, I would like the invoices section's invoicePaid dropdown field to change from "No" to "Yes" using the charge.onCharge hook.
I tried the following( and variations ) without success.
 {
    craft()->on('charge.onCharge', function(Event $event) {
        craft()->on('entries.onSaveEntry', function(Event $event ){

            //Is the entry in the section 'invoices'?
            if ($event->params['entry']->section == 'invoices') {

            //Replace values
            $event->params['entry']->getContent()->invoicePaid = 'Yes';
            }
        });


    });
}

Where am I going wrong, or is there a better way to go about this?

**Tags:** plugins, plugin-charge, hook

---

## 1 answers

### Answer by username_1 on 2018-02-10. Score: 2 | Accepted answer

In your current example, you are not actually saving an entry. You are just setting up an onSaveEntry event listener. To save the entry you will need to actually save the entry using the [saveEntry][1] method.
UPDATE: I'm not exactly sure what your setup is — but some variation on the following should work for you.

In the CMS, add a field called 'invoiceID', which you will use to store the invoice entryId to the charge (or 'invoiceEntryId' or whatever you want to call it).
Add the 'invoiceId' field to the charge field layout in charge->settings->fields.
In the charge form, add the 'invoiceId' field to your form (this assumes that you are making the charge from the invoice entry page, otherwise you will need to use a reference to the invoice entry id through whatever relationship you have set up):


In your plugin, add the onCharge event to grab the 'invoiceId' from the charge event, then retrieve, update, and save the entry.
craft()->on('charge.onCharge', function(Event $event) {

    // get the charge model
    $charge = $event->params['charge'];

    // retrieve the invoiceId (aka entryId) saved on the charge model
    $entryId = $charge->invoiceId;

    // uncomment to test, if needed
    //LogicPlugin::log('Invoice ID: '.$charge->invoiceId, LogLevel::Info);

    // get the invoice entry from the invoiceId
    $entry = craft()->entries->getEntryById($entryId);

    // update the invoicePaid field (in this case I'm using a switch)
    $entry->getContent()->invoicePaid = '1';

    // save the entry
    $success = craft()->entries->saveEntry($entry);

    // log errors
    if (!$success) {
        LogicPlugin::log('Couldn’t save the entry "'.$entry->title.'"', LogLevel::Error);
    }

});

> **Comments:**
> **username_0**: Unfortunately it comes back with  _Undefined index: entry_  error message. Been trying to get this to show anything, but so far no luck  
> **username_1**: I don't have charge installed, so can't investigate directly, but from the docs it looks like charge provides it's own element type (i.e. charge) and models (i.e. charge, payments, customer, subscription, account). You are probably therefore not working with an 'entry' model. You may need to dig into the charge plugin and see what you are working with, and what methods it uses for saving elements (i.e. `craft()->charge()->saveCharge()` or similar) — or try just using the 'element' methods, which should apply to all elements in craft.  
> **username_1**: More specifically, it looks like the onCharge event returns an attribute called `charge` which contains a reference to the charge model (i.e. `$charge = $event->params['charge'];`. How you modify and save that is another question, but you might try `craft()->charge_charge->saveCharge($charge)` which I found by searching the plugin source files.  
> **username_0**: Using the `$charge = $event->params['charge'];` I'm trying to get the entry Id, which is posted as `meta[id]`. I can get it using `{{ dump(charge['meta']['id']) }}`, but unable to call it in the plugin, so it could be used  `$id = charge()->meta()->id();     $entry = craft()->entries()->getEntryById($id);`  
> **username_1**: I installed the plugin on a test install, and was able to get a solution set up. I updated the answer. See if that works. (FYI... I'm using my LogicPlugin for logging. Change to `Craft::Log` if you want it to go the craft log.)  
> **username_0**: Thank you, this worked. It took a while to see it work as I was having a plugin problem that was affecting it, but it works now.  

---

