You are here

function commerce_cart_redirection_form_alter in Commerce Cart Redirection 3.0.x

Same name and namespace in other branches
  1. 8 commerce_cart_redirection.module \commerce_cart_redirection_form_alter()

Implements hook_form_alter().

File

./commerce_cart_redirection.module, line 29
Contains commerce_cart_redirection.module.

Code

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

  /** @var \Drupal\commerce_product\Entity\Product $product */
  if ($product = $form_state
    ->get('product')) {

    // Load module config and see if there is text in the
    // 'Modify Add to Cart button text' field. Skip this whole thing if
    // there is not.
    $config = \Drupal::configFactory()
      ->get('commerce_cart_redirection.settings');
    if (!empty($config
      ->get('add_to_cart_replacement_text'))) {

      // Load the Product Variation types this module is redirecting.
      // If the current variation is one of them then go ahead and
      // change the button text.
      $active_bundles = $config
        ->get('product_bundles');
      $negate = $config
        ->get('negate_product_bundles');
      $purchaseable_bundle = $product
        ->getDefaultVariation()
        ->bundle();
      $redirect = FALSE;
      if (isset($active_bundles[$purchaseable_bundle]) && $active_bundles[$purchaseable_bundle] !== 0) {
        if (!$negate) {
          $redirect = TRUE;
        }
      }
      else {
        if ($negate) {
          $redirect = TRUE;
        }
      }
      if ($redirect) {
        $form['actions']['submit']['#value'] = t($config
          ->get('add_to_cart_replacement_text'));
      }
    }
  }
}