PHP code example of betacie / mailchimp-bundle

1. Go to this page and download the library: Download betacie/mailchimp-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

betacie / mailchimp-bundle example snippets

 
$bundles = [
    // ...
    new Betacie\MailChimpBundle\BetacieMailChimpBundle(),
];



namespace YourApp\App\Newsletter;

use Betacie\MailchimpBundle\Provider\ProviderInterface;
use Betacie\MailchimpBundle\Subscriber\Subscriber;
use YourApp\Model\User\UserRepository;
use YourApp\Model\User\User;

class SubscriberProvider implements ProviderInterface
{
    // these tags should match the one you added in MailChimp's backend
    const TAG_NICKNAME =           'NICKNAME';
    const TAG_GENDER =             'GENDER';
    const TAG_BIRTHDATE =          'BIRTHDATE';
    const TAG_LAST_ACTIVITY_DATE = 'LASTACTIVI';
    const TAG_REGISTRATION_DATE =  'REGISTRATI';
    const TAG_CITY =               'CITY';

    protected $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function getSubscribers()
    {
        $users = $this->userRepository->findSubscribers();

        $subscribers = array_map(function(User $user) {
            $subscriber = new Subscriber($user->getEmail(), [
                self::TAG_NICKNAME => $user->getNickname(),
                self::TAG_GENDER => $user->getGender(),
                self::TAG_BIRTHDATE => $user->getBirthdate() ? $user->getBirthdate()->format('Y-m-d') : null,
                self::TAG_CITY => $user->getCity(),
                self::TAG_LAST_ACTIVITY_DATE => $user->getLastActivityDate() ? $user->getLastActivityDate()->format('Y-m-d') : null,
                self::TAG_REGISTRATION_DATE => $user->getRegistrationDate() ? $user->getRegistrationDate()->format('Y-m-d') : null,
                // you don't need to specify "mc_language" tag if you added it in your config
                // you can also use all MailChimp configuration tags here as well
            ]);

            return $subscriber;
        }, $users);

        return $subscribers;
    }
}



use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function newUser(User $user)
{
    // ...
	
    $subscriber = new Subscriber($user->getEmail(), [
		'FIRSTNAME' => $user->getFirstname(),
		'mc_language' => 'fr',
		// Important note : mc_language defined in config.yml will not be used, be sure to set it here if needed
		// as well as any other MailChimp tag you need.
	]);

	$this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_SUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}



use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function changedEmail($previousMail, $newEmail)
{
    // ...
	
    $subscriber = new Subscriber($previousEmail, [
		 'new-email' => $newEmail
    ]);

    $this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_SUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}



use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function deletedUser(User $user)
{
    // ...
	
    $subscriber = new Subscriber($user->getEmail());

    $this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_UNSUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}