You are here

public function ContactSubscription::submitPaneForm in Mailchimp E-Commerce 8

Handles the submission of an pane form.

Parameters

array $pane_form: The pane form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.

array $complete_form: The complete form structure.

Overrides CheckoutPaneBase::submitPaneForm

File

modules/mailchimp_ecommerce_commerce/src/Plugin/Commerce/CheckoutPane/ContactSubscription.php, line 171

Class

ContactSubscription
Provides the subscription information pane.

Namespace

Drupal\mailchimp_ecommerce_commerce\Plugin\Commerce\CheckoutPane

Code

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state
    ->getValue($pane_form['#parents']);
  if ($values['subscription'] == 1) {
    $customer = [];
    $customer['email_address'] = $this->order
      ->getEmail();
    if (!empty($customer['email_address'])) {

      /* @var \Drupal\mailchimp_ecommerce\CustomerHandler $customer_handler */
      $customer_handler = \Drupal::service('mailchimp_ecommerce.customer_handler');
      $billing_profile = $this->order
        ->getBillingProfile();
      $customer = $customer_handler
        ->buildCustomer($customer, $billing_profile);
      $customer_handler
        ->addOrUpdateCustomer($customer);
      module_load_include('module', 'mailchimp', 'mailchimp');
      $list_id = mailchimp_ecommerce_get_list_id();
      $merge_vars = [
        'EMAIL' => $customer['email_address'],
        'FNAME' => $customer['first_name'],
        'LNAME' => $customer['last_name'],
      ];
      mailchimp_subscribe($list_id, $customer['email_address'], $merge_vars);
    }
  }
}