<?php

namespace Drupal\custom_commerce\Plugin\Block;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\mymodule\Form\TestForm;

/**
 * Provides
 *
 * @Block(
 *   id = "custom_commerce_product_suggest",
 *   admin_label = @Translation("Products suggest"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node", label = @Translation("Node")),
 *   }
 * )
 */

class ProductSuggest extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = $this->getContextValue('node');
    $node_id = $node->id();

    $products = \Drupal::entityTypeManager()
      ->getStorage('commerce_product')
      ->loadByProperties(/*['field_node' => $node_id]*/);
    foreach ($products as $product) {
      $build[] = \Drupal::entityTypeManager()->getViewBuilder('commerce_product')->view($product, 'cart');
    }

    empty($products) ? $build = ['#markup' => 'Ничего не найдено'] : '';
    return $build;
  }
}
