Skip Navigation

[Resolved] Split: mc4wp (MailChimp for WordPress) – how to access checkbox value using php

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 18 replies, has 2 voices.

Last updated by jayesonE 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1871251

Now I'm adding a checkbox and trying to use the same snippet.
How would I use the same snippet for a checkbox?

this is the code I'm trying for the checkbox
add_filter( 'mc4wp_user_sync_subscriber_data', function( MC4WP_MailChimp_Subscriber $subscriber, WP_User $user ) {

$subscriber->interests[c3033aaca6][] = types_render_usermeta( 'position-type', array( "user_id" => $user->ID ) );

return $subscriber;
}, 10, 2 );

Here is the debug info.
Mailchimp User Sync: Debug user data
MC4WP_MailChimp_Subscriber Object
(
[email_address] => jill@jpearl.ca
[interests] => Array
(
[c3033aaca6] => Array
(
[0] => Seconded In
)

)

[merge_fields] => Array
(
[FNAME] => Jillian
[LNAME] => Hills
[NAME] => jills
[COUNSELPOS] => Counsel
[MSTARTDATE] => 2020-12-17
[CNTRACTEND] => 2020-12-31
)

[status] =>
[email_type] =>
[ip_signup] =>
[language] =>
[vip] =>
[tags] =>
)

#1871253

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to access the value of the checkbox stored in databse? If yes:

You can access the checkbox value as given under:

types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID));

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/#checkbox

#1871285

I'm trying to update the mailchimp checkbox fields with the values stored in the database. But with checkboxes how do you turn the checkboxes on or off or to match what is on the wordpress site?

#1871287

This is the output when adding the code you suggested
MC4WP_MailChimp_Subscriber Object
(
[email_address] => jill@jpearl.ca
[interests] => Array
(
[c3033aaca6] => Array
(
[0] => Permanent, Seconded In
)

)

[merge_fields] => Array
(
[FNAME] => Jillian
[LNAME] => Hills
[NAME] => jills
[COUNSELPOS] => Counsel
[MSTARTDATE] => 2020-12-17
[CNTRACTEND] => 2020-12-31
)

[status] =>
[email_type] =>
[ip_signup] =>
[language] =>
[vip] =>
[tags] =>

#1871289

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to again confirm you have checkbox field or checkboxes field. did you created checkbox or checkboxes field using Types or MailChimp?

#1871291

I have 4 checkboxes

#1871293

I created the checkboxes with types.

#1871295

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Its still not clear. Did you used the checkboxes as field type and added 4 options to it or you used checkbox as field type and added 4 different checkbox field?

#1871303

I'm sorry for not being clear. Yes I have a checkbox field with 4 options

#1871305

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To ensure - would you mind to share the screenshot of your User custom field group where you added those checkbox field with options?

#1871307
Screen Shot 2020-12-10 at 2.02.11 AM.png

Heres a screenshot

#1871317

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - thats clear now, you are using checkboxes field not checkbox.

To get values from the checkboxes field you can use the following line of code:

types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID,"separator"=>","));

You will get the comma separated values for the checkboxes that is checked.

Now, how to inject those values to mailchim you will require to check with mailchimp support.

#1871341

Is there a way to output the true or false value of the checkbox option rather than the name value?

#1871349

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The value will be returned as per the values you configured with your checkboxes options.

There is a Types custom field API available but the thing is that it supports custom post fields not custom user fields:
=> https://toolset.com/documentation/programmer-reference/toolset-custom-field-api/

Your requirement requires custom programming and that is beyond the scope of our support policy.

Another way to retrieve the value for checkboxes field is you can target the specific checkbox option. For example:

$option0 = types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID,'option'=>0,"state"=>"checked"));

$option1 = types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID,'option'=>1,"state"=>"checked"));

$option2 = types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID,'option'=>2,"state"=>"checked"));

$option3 = types_render_usermeta("position-type", array("output" => "raw","user_id" => $user->ID,'option'=>3,"state"=>"checked"));

If(!empty($option0)){
$value0 = true;
}else{
$value0 = false;
}

If(!empty($option1)){
$value1 = true;
}else{
$value1 = false;
}

If(!empty($option2)){
$value2 = true;
}else{
$value2 = false;
}

If(!empty($option3)){
$value3 = true;
}else{
$value3 = false;
}

You can adjust the above code as per your requirement.

#1871361

Thank you Minish, I'll work on this to see if it will solve the problem.