new0
Neues Wissen einreichen

Mailchimp Connection

// Mailchimp
    if($status == 'success') {
      try {
        $api_key = get_field('kp_supplier_api_key', 'option');
        $email = $email;
        $status = 'subscribed'; // subscribed, cleaned, pending
        $list_id = get_field('kp_supplier_list_id', 'option');
        
        $args = array(
          'method' => 'PUT',
          'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
          ),
          'body' => json_encode(array(
            'email_address' => $email,
            'status'        => $status,
            'merge_fields' => $merge_fields,
          ))
        );
        $response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)), $args );
        $body = json_decode( $response['body'] );
        
        if ( $response['response']['code'] == 200 && $body->status == $status ) {
          //          $message =  'The user has been successfully ' . $status . '.';
          $status = 'success';
        } else {
          $message =  '' . $response['response']['code'] . $body->title . ': ' . $body->detail;
          $message =  'hello';
          $status = 'error';
          //$error = 'mailchimp';
        }
      } catch (exception $e){
        $text = 'fail: '.$e;
        $status = 'error';
      }
    }