How to customize the “Add to Cart” button of a product ?

I created a custom module with Drupal 8 and Commerce 2.11 to modify the “Add to Cart” button of the product sheets.

The module works, but it is currently applied to all products.

I want to apply it only to the product with ID 50

How to customize the “Add to Cart” button of a product ?

<?php
 
use Drupalcommerce_storeEntityStoreType;
use Drupalcommerce_productEntityProductType;
use Drupalcommerce_productEntityProductVariationType;
use DrupalCoreEntityDisplayEntityViewDisplayInterface;
use DrupalCoreEntityEntityInterface;
use DrupalCoreUrl;
 
function mymodule_form_commerce_order_item_add_to_cart_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
$current_store = Drupal::service('commerce_store.current_store');
$owner = $current_store->getStore()->getOwner();
foreach ($form_state->getFormObject()->getEntity()->getPurchasedEntity()->getProduct()->getStores() as $store) {
$bundle = $store->bundle();
// Product type abonnement.
if ($bundle == 'online') {
if (isset($form["#attributes"]["class"]) && in_array("commerce-order-item-add-to-cart-form", $form["#attributes"]["class"])) {
$selectedVariationId = $form_state->get('selected_variation');
$selectedVariation = Drupalcommerce_productEntityProductVariation::load($selectedVariationId);
$form['actions']['submit']['#value'] = t('Subscribe');
if (!$owner->hasRole('marchand')) {
$form['actions']['submit']['#attributes']['class'] = array('button--add-to-cart', 'button button--primary', 'js-form-submit', 'form-submit', 'is-disabled', 'btn-warning', 'btn');
$form['actions']['submit']['#disabled'] = TRUE;
}
}
}
}
}

I added commerce-order-item-add-to-cart-form-commerce-product-50 to my code but it does not work :

<?php
 
use Drupalcommerce_storeEntityStoreType;
use Drupalcommerce_productEntityProductType;
use Drupalcommerce_productEntityProductVariationType;
use DrupalCoreEntityDisplayEntityViewDisplayInterface;
use DrupalCoreEntityEntityInterface;
use DrupalCoreUrl;
 
function mymodule_form_commerce_order_item_add_to_cart_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
$current_store = Drupal::service('commerce_store.current_store');
$owner = $current_store->getStore()->getOwner();
foreach ($form_state->getFormObject()->getEntity()->getPurchasedEntity()->getProduct()->getStores() as $store) {
$bundle = $store->bundle();
// Product type abonnement.
if ($bundle == 'online') {
if (isset($form["#attributes"]["class"]) && in_array("commerce-order-item-add-to-cart-form-commerce-product-50", $form["#attributes"]["class"])) {
$selectedVariationId = $form_state->get('selected_variation');
$selectedVariation = Drupalcommerce_productEntityProductVariation::load($selectedVariationId);
$form['actions']['submit']['#value'] = t('Subscribe');
if (!$owner->hasRole('marchand')) {
$form['actions']['submit']['#attributes']['class'] = array('button--add-to-cart', 'button button--primary', 'js-form-submit', 'form-submit', 'is-disabled', 'btn-warning', 'btn');
$form['actions']['submit']['#disabled'] = TRUE;
}
}
}
}
}

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed