<?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class TestForm extends FormBase {

  public function getFormId() {
        return 'mymodule_test_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state, $product = NULL) {

    $variations = $product->getVariations();
    foreach ($variations as $variation) {
      $var = $variation->getAttributeValueIds();
      $text = $text . $var["attribute_size"] . ' ';
    }

    $form['title'] = [
      '#type' => 'item',
      '#markup' => $product->getTitle() . $text,
    ];

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Click'),
      '#button_type' => 'primary',
      '#ajax' => [
        'callback' => [$this, 'alert'],
        'event' => 'click',
      ],
    );
    return $form;
  }

  public function SubmitForm(array &$form, FormStateInterface $form_state) {

  }

  public function alert($form, $form_state) {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand('Hello'));
    return $response;
  }
}
