<p>What you're probably looking for is:</p>

<pre><code>if ($order-&gt;signupForNewsletter-&gt;contains('joinMailingList'))
{
    echo "I've opted in";
}
else
{
    echo 'Not matched';
}
</code></pre>

CC BY-SA 3.0
2016-10-05T16:56:28.557Z
2016-10-05T16:56:28.557Z
Brad Bell
<p>I am trying to check the value of a checkbox as part of my checkout process. I have the following function at the moment.</p>

<pre><code>public function init()
{
    craft()-&gt;on('commerce_orders.onBeforeOrderComplete', function (Event $event) {
        $order = $event-&gt;params['order'];
        echo '&lt;pre&gt;';
        print_r($order-&gt;signupForNewsletter);
            if ($order-&gt;signupForNewsletter == 'joinMailingList') {
                echo "I've opted in";
            } else {
                echo 'Not matched';
            }
        echo '&lt;/pre&gt;';
        exit;
    });
}
</code></pre>

<p>But whether the checkbox is ticked or not I am only get the 'Not matched' statement, so my if statement is always false. I have tried several things, but cannot see how to resolve this.
I think perhaps because the signupForNewsletter is a checkbox in the custom order fields, so it is an array rather than an object. </p>

<p>Here is the print_r results of this code;</p>

<pre><code>    Craft\MultiOptionsFieldData Object
(
    [_options:Craft\MultiOptionsFieldData:private] =&gt; Array
        (
            [0] =&gt; Craft\OptionData Object
                (
                    [label] =&gt; Join Mailing List
                    [value] =&gt; joinMailingList
                    [selected] =&gt; 1
                )

        )

    [storage:ArrayObject:private] =&gt; Array
        (
        )

    )
    Not matched
</code></pre>

<p>The checkbox was ticked in this instance as you can see in the value field.</p>

<plugin-craftcommerce><custom-field><checkboxes>
Checking value of customer order field within Plugin
# Checking value of customer order field within Plugin

(Asked by: username_0 on 2016-10-05)

I am trying to check the value of a checkbox as part of my checkout process. I have the following function at the moment.
public function init()
{
    craft()->on('commerce_orders.onBeforeOrderComplete', function (Event $event) {
        $order = $event->params['order'];
        echo '';
        print_r($order->signupForNewsletter);
            if ($order->signupForNewsletter == 'joinMailingList') {
                echo "I've opted in";
            } else {
                echo 'Not matched';
            }
        echo '';
        exit;
    });
}

But whether the checkbox is ticked or not I am only get the 'Not matched' statement, so my if statement is always false. I have tried several things, but cannot see how to resolve this.
I think perhaps because the signupForNewsletter is a checkbox in the custom order fields, so it is an array rather than an object. 
Here is the print_r results of this code;
    Craft\MultiOptionsFieldData Object
(
    [_options:Craft\MultiOptionsFieldData:private] => Array
        (
            [0] => Craft\OptionData Object
                (
                    [label] => Join Mailing List
                    [value] => joinMailingList
                    [selected] => 1
                )

        )

    [storage:ArrayObject:private] => Array
        (
        )

    )
    Not matched

The checkbox was ticked in this instance as you can see in the value field.

**Tags:** plugin-craftcommerce, custom-field, checkboxes

---

## 1 answers

### Answer by username_1 on 2016-10-05. Score: 3 | Accepted answer

What you're probably looking for is:
if ($order->signupForNewsletter->contains('joinMailingList'))
{
    echo "I've opted in";
}
else
{
    echo 'Not matched';
}


---

