Open MayorUshanka opened 3 years ago
When formatting it like this:
$MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", [
'tags' => ["
\"name\": \"$tag\",
\"status\":active
"]
]);
It seems to be correctly formatted. Except mailschimp now complains that the field "name" is required - even though I clearly filled it in (I even tried hardcoding the $tag
).
[errors] => Array ( [0] => Array ( [field] => tags[0].name [message] => This value should not be blank. ) )
did you find a workaround for this ?
Also getting this issue
I believe its due to https://github.com/drewm/mailchimp-api/blob/master/src/MailChimp.php#L390 when it is encoding the array of tags it is creating a JSON object instead.
I've got it working by doing this
[
'tags' => array_values($tags),
]
Which seems to trigger it to encode correctly 🤞
I tried this snippet but it didn't work. It creates the tag on mailchimp and the user doesn't receive the mail.
$added = $this->mailchimp_api->post('/lists/' . $this->list_id . '/members/' . $email_hash . '/tags', [
"tags" => array_values([
[
"name" => "Influencer",
"status" => "active"
]
])
]
);
How can I subscribe a user with a specific tag?
Ok, here is the code that seems to work for me
class MAILCUCK{ // MAILChimp Utilities Class Kernel
private $MailChimp;
private $debug;
function __construct($debug = false){
$this->MailChimp = new MailChimp('apikey');
$this->debug = $debug;
}
function subscribe_or_add_tag($list_id, $email, $tag, $merge_fields=[]){
// we have an audience (list).
// we want to add an email with a tag to this list
// if the email is already in the list, just append the tag
// if not, subscribe along with the tag
/*
$merge_fields = [
'FNAME' => "testF",
'LNAME' => "testL",
'ADDRESS' => "addr",
'PHONE' => "testtel",
'FULLNAME' => "TestF TestL"
];
*/
$payload;
if(empty($merge_fields)){
$payload = [
'email_address' => $email,
'status' => 'subscribed',
'tags' => [$tag],
];
} else {
$payload = [
'email_address' => $email,
'status' => 'subscribed',
'tags' => [$tag],
'merge_fields' => $merge_fields,
];
}
$result = $this->MailChimp->post("lists/$list_id/members", $payload);
if ($this->debug){
echo "Result: ".$result;
}
if ($result['title'] == "Member Exists"){
if ($this->debug){
echo "Member alreay exists, appending tag... ";
}
$subscriber_hash = md5(trim(strtolower($email)));
$payload = [
'tags'=>[
[
"name" => $tag,
"status" => "active"
]
]
];
// update member's tags
$upd = $this->MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", $payload);
if ($this->debug){
print_r($upd);
}
} else {
if ($this->debug){
print_r($result);
}
}
}
}
Mailchimp documentation:
https://mailchimp.com/developer/api/marketing/list-member-tags/add-or-remove-member-tags/
Code:
output:
When I double wrap the tags (so it becomes an array of arrays), mailchimp complains that it should be a string. What does mailchimp actually want from me? Is this a bug?