diff --git a/commerce_ajax_atc.module b/commerce_ajax_atc.module index b481e09..142512b 100644 --- a/commerce_ajax_atc.module +++ b/commerce_ajax_atc.module @@ -13,6 +13,7 @@ use Drupal\Core\Ajax\MessageCommand; use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Url; use Drupal\colorbox_load\OpenCommand; +use Drupal\Core\Ajax\ReplaceCommand; /** * Implements commerce_ajax_atc_alter_submit helper function. @@ -211,19 +212,29 @@ function commerce_ajax_atc_form_submit(array $form, FormStateInterface &$form_st 'width' => $config->get('ajax_modal_width'), 'height' => $config->get('ajax_modal_height'), ]; - // Add the Command for the selected pop-up type. - if ($config->get('pop_up_type') === 'non_modal') { + // Check for errors, and display messages. + if ($form_state->hasAnyErrors()) { $variation_id = $form_state->getFormObject()->getEntity()->getPurchasedEntity()->id(); - $text = $config->get('non_modal_message') ?: $success_message; - $response->addCommand(new MessageCommand($text, '#add-to-cart-message-container-' . $variation_id)); + $response->addCommand(new ReplaceCommand('#add-to-cart-message-container-' . $variation_id, [ + '#type' => 'status_messages', + '#weight' => -1000, + ])); } - elseif ($config->get('pop_up_type') === 'modal_dialog') { - $response->addCommand(new OpenModalDialogCommand($title, $content, $options)); - } - elseif ($config->get('pop_up_type') === 'colorbox') { - $rendered_content = \Drupal::service('renderer')->renderRoot($content); - $response->setAttachments($content['#attached']); - $response->addCommand(new OpenCommand($rendered_content)); + // Add the Command for the selected pop-up type. + if (!$form_state->hasAnyErrors()) { + if ($config->get('pop_up_type') === 'non_modal') { + $variation_id = $form_state->getFormObject()->getEntity()->getPurchasedEntity()->id(); + $text = $config->get('non_modal_message') ?: $success_message; + $response->addCommand(new MessageCommand($text, '#add-to-cart-message-container-' . $variation_id)); + } + elseif ($config->get('pop_up_type') === 'modal_dialog') { + $response->addCommand(new OpenModalDialogCommand($title, $content, $options)); + } + elseif ($config->get('pop_up_type') === 'colorbox') { + $rendered_content = \Drupal::service('renderer')->renderRoot($content); + $response->setAttachments($content['#attached']); + $response->addCommand(new OpenCommand($rendered_content)); + } } return $response;