Drupal 8 - Commerce - Billing same as shipping

Submitted by root on Sat, 12/02/2017 - 05:52

Added checkbox which pull the shipping info and fill billing form.  But after checked, Shipping information not is visible mode.

 

function code_monkey_preprocess_commerce_order(array &$variables) {
  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['elements']['#commerce_order'];
  $orderId = $order->order_id->value;
  $orders = \Drupal\commerce_order\Entity\Order::load($orderId);
  $orderItems = $orders->getItems()[0]->toArray();
  $completed = $orders->toArray()['completed'][0]['value'];
  $unit_price = [
    '#type' => 'inline_template',
    '#template' => '{{ price|commerce_price_format }}',
    '#context' => [
      'price' => new \Drupal\commerce_price\Price($orderItems['total_price'][0]['number'], $orderItems['total_price'][0]['currency_code']),
    ],
  ];
  $variables['total_price'] = $unit_price;
  $variables['order_id'] = $orderId;
  $variables['order_email_id'] = $orders->getEmail();
  $variables['completed'] = date("F j, Y, G:i", $completed);
  $variables['ring_title'] = $orderItems['title'][0]['value'];
  $variables['diamond_carat'] = $orderItems['field_item_diamond_carat'][0]['value'];
  $variables['diamond_clarity'] = \Drupal\taxonomy\Entity\Term::load($orderItems['field_item_diamond_clarity'][0]['target_id'])->name->value;
  $variables['diamond_colors'] = \Drupal\taxonomy\Entity\Term::load($orderItems['field_item_diamond_colors'][0]['target_id'])->name->value;
  $variables['diamond_cut'] = \Drupal\taxonomy\Entity\Term::load($orderItems['field_item_diamond_cut'][0]['target_id'])->name->value;
  $variables['ring_size'] = \Drupal\taxonomy\Entity\Term::load($orderItems['field_item_ring_size'][0]['target_id'])->name->value;
}

function code_monkey_form_commerce_checkout_flow_alter(array &$form, FormStateInterface $form_state) {
  if (isset($form['payment_information']['add_payment_method'])) {
    $form['payment_information']['copy_from_shipping'] = [
      '#type' => 'checkbox',
      '#title' => t('My billing address is the same as my shipping address'),
      '#default_value' => TRUE,
      '#weight' => 0,
    ];
    $form['payment_information']['add_payment_method']['#weight'] = 5;
    $form['payment_information']['add_payment_method']['#after_build'][] = 'code_monkey_process_payment_method_element';

    array_unshift($form['actions']['next']['#submit'], 'code_monkey_submit_payment_method');
  }
}

function code_monkey_process_payment_method_element(array $element, FormStateInterface $form_state) {
  $trigger_element = ':input[name="payment_information[copy_from_shipping]"]';

  if (isset($element['billing_information'])) {
    $element['billing_information']['#states']['visible'] = [
      $trigger_element => ['checked' => FALSE,],
    ];
  }

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = $element['billing_information']['#profile'];

  foreach ($profile->getFieldDefinitions() as $field_definition) {
    if ($field_definition->isRequired() && isset($element['billing_information'][$field_definition->getName()])) {
      $element['billing_information'][$field_definition->getName()]['#states']['required'] = [
        $trigger_element => ['checked' => FALSE,],
      ];
    }
  }

  return $element;
}

function code_monkey_submit_payment_method(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValue('payment_information');

  if (!empty($values['copy_from_shipping']) && isset($form['shipping_information']['shipping_profile']['#profile'])) {
    $profile = $form['shipping_information']['shipping_profile']['#profile'];
    $form['payment_information']['add_payment_method']['billing_information']['#profile'] = $profile;
  }
}

 

 

Code Belongs To