You are here

function commerce_paypal_form_commerce_order_item_add_to_cart_form_alter in Commerce PayPal 8

Implements hook_form_BASE_FORM_ID_alter().

File

./commerce_paypal.module, line 177
Implements PayPal payment services for use with Drupal Commerce.

Code

function commerce_paypal_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Check to see if PayPal Credit messaging is enabled on Add to Cart forms.
  $enable_messaging = \Drupal::config('commerce_paypal.credit_messaging_settings')
    ->get('add_to_cart');

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$enable_messaging || !$order_item
    ->getUnitPrice()) {
    return;
  }

  // Add Credit Messaging JS to the form.
  // @todo ensure messaging reapplies after an Ajax refresh.
  $form['#attached']['library'][] = 'commerce_paypal/credit_messaging';
  $form['paypal_credit_messaging_product'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'data-pp-message' => '',
      'data-pp-placement' => 'product',
      'data-pp-amount' => Calculator::trim($order_item
        ->getUnitPrice()
        ->getNumber()),
    ],
    '#weight' => 1,
  ];
}