1

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 '<pre>';
        print_r($order->signupForNewsletter);
            if ($order->signupForNewsletter == 'joinMailingList') {
                echo "I've opted in";
            } else {
                echo 'Not matched';
            }
        echo '</pre>';
        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.

3

What you're probably looking for is:

if ($order->signupForNewsletter->contains('joinMailingList'))
{
    echo "I've opted in";
}
else
{
    echo 'Not matched';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.