Vince Vince - 4 months ago 19
PHP Question

Mailchimp API 2.0 Subscribing users with groups - checkbox value

I am stuck on how to retrieve and send the value of my checkbox in a Mailchimp form using the API 2.0.
At the end of my form I have this checkbox

<input type="checkbox" checked data-dojo-type="dijit/form/CheckBox" id="group_1" name="group[4909][1]" value="1" class="av-checkbox"><span id="message-check">YES, I agree to be contacted by ... for promotions and special offers.</span>


here my php code to connect and send data through the API:

$email = array('email' => htmlentities($_REQUEST['MERGE0']));
$merge_vars = array(
'MMERGE3' => $_REQUEST['MERGE3'], //Voucher value
'MMERGE4' => $_REQUEST['MERGE4'], //Date value
'MMERGE6' => $_REQUEST['MERGE6'], //Sales Person value
'FNAME' => $_REQUEST['MERGE1'], //First Name value
'LNAME' => $_REQUEST['MERGE2'], //Last Name value
'MMERGE5' => $_REQUEST['MERGE5'], //Phone value
'groupings' => array(
'id' => '4909',
'name' => "Offers",
'groups' => $_REQUEST['group']
)
);
require('Mailchimp.php');
$Mailchimp = new Mailchimp( $api_key );
$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
$subscriber = $Mailchimp_Lists->subscribe($list_id, $email, $merge_vars, $email_type, $double_optin);


I followed this documentation https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

All the fields Voucher, date,sales,first and last name and Phone are sent and the user is subscribed on my list. The only value i am missing is the checkbox. I would like to send at least "Yes" or "1" to my list for that checkbox.
Cannot figure out!

Any suggestion guys?

Thanks.
Vince.

Answer

OK 1h later of posting my problem i found the solution.

Here the correct code that works for me:

$merge_vars = array(
'MMERGE3' => $_REQUEST['MERGE3'], //Voucher value
'MMERGE4' => $_REQUEST['MERGE4'], //Date value
'MMERGE6' => $_REQUEST['MERGE6'],  //Sales Person value
'FNAME' => $_REQUEST['MERGE1'],   //First Name value
'LNAME' => $_REQUEST['MERGE2'],   //Last Name value
'MMERGE5' => $_REQUEST['MERGE5'], //Phone value
'GROUPINGS' => array(
    0 => array(
        'id' => '4909', // need grouping ID
        'groups' => array( 'YES, I agree to be contacted by ... for promotions and special offers.' )
    )
),
);

In other words I printed out my group id that i have in my list:

//Print the current group of a list
/*$current_groupings = $Mailchimp->call( 'lists/interest-groupings', array(
            'id' => 'your_list_ID',
        ) );
var_dump($current_groupings);*/

I got the entire array of group:

array(1) { 
[0]=> array(5) {
    ["id"]=> int(4909) 
    ["name"]=> string(6) "Offers" 
    ["form_field"]=> string(10) "checkboxes" 
    ["display_order"]=> string(1) "0" 
    ["groups"]=> array(1) { 
        [0]=> array(5) { 
            ["id"]=> int(17445) 
            ["bit"]=> string(1) "1" 
            ["name"]=> string(89) "YES, I agree to be contacted by ... for promotions and special offers." 
            ["display_order"]=> string(1) "1" 
            ["subscribers"]=> NULL 
        } 
    } 
} 
}

Therefore it was really easy to understand that I need to use:

  1. Group ID in this case 4909
  2. and the groups name that in this case is: "YES, I agree to be contacted by ... for promotions and special offers."

That's it. Works! Hope this help someone else.

Cheers, Vince.

Comments