Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Will there be a way to output lists, categories and interests? #291

Open
latestscoop opened this issue Feb 6, 2020 · 0 comments
Open

Will there be a way to output lists, categories and interests? #291

latestscoop opened this issue Feb 6, 2020 · 0 comments

Comments

@latestscoop
Copy link

Currently I am using the following class:

class MailChimp_Extend extends MailChimp {
	/*
	Returns structure array: list > category > interest
	*/
	public function get_structure(){
		$lists = $this->get('lists');
		//print_r($lists);
		$structure = array();
		foreach($lists['lists'] as $list){
			$this_list_id = $list['id'];
			$this_list_name = $list['name'];
			$structure[$this_list_id]['id'] = $this_list_id;
			$structure[$this_list_id]['name'] = $this_list_name;
			$structure[$this_list_id]['categories'] = array();

			$categories = $this->get('lists/' . $this_list_id . '/interest-categories/');
			//print_r($categories);
			foreach($categories['categories'] as $category){
				$this_category_id = $category['id'];
				$this_category_name = $category['title'];
				$structure[$this_list_id]['categories'][$this_category_id]['id'] = $this_category_id;
				$structure[$this_list_id]['categories'][$this_category_id]['name'] = $this_category_name;
				$structure[$this_list_id]['categories'][$this_category_id]['interests'] = array();

				$interests = $this->get('lists/' . $this_list_id . '/interest-categories/' . $this_category_id . '/interests');
				//print_r($interests);
				foreach($interests['interests'] as $interest){
					$this_interest_id = $interest['id'];
					$this_interest_name = $interest['name'];
					$structure[$this_list_id]['categories'][$this_category_id]['interests'][$this_interest_id]['id'] = $this_interest_id;
					$structure[$this_list_id]['categories'][$this_category_id]['interests'][$this_interest_id]['name'] = $this_interest_name;
				}
			}
		}
		//print_r($structure);
		return $structure;
	}
}

Using it like this:

$MailChimp = new DrewM\MailChimp\MailChimp_Extend('key');
$structure = $MailChimp->get_structure();
print_r($structure);

Which putputs an array:

Array(
    ["list id"] => Array(
            [id] => "list id"
            [name] => "list name"
            [categories] => Array(
                    ["categoy id"] => Array(
                            [id] => "categoy id"
                            [name] => "categoy name"
                            [interests] => Array (
                                    ["interest id"] => Array(
                                            [id] => "interest id"
                                            [name] => "interest name"
                                    )
                                    ["interest id"] => Array(
                                            [id] => "interest id"
                                            [name] => "interest name"
                                    )
                            )
                    )
            )
    )
);

...where the double-quotes are actual ids and names.

I use it to dynamically create checkboxes in forms, whenever categories/interests are amended.
I would imagine if you're using Mailchimp groups and this API, you would need to generate group checkboxes at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant