Я создал пользовательский модуль с Drupal 8 и Commerce 2.11 для изменения кнопки «Добавить в корзину» на листах продуктов.
Модуль работает, но в настоящее время он применяется ко всем продуктам.
Я хочу применить его только к продукту с идентификатором 50
Как настроить кнопку «Добавить в корзину» товара?
<?php
use Drupal\commerce_store\Entity\StoreType;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
function mymodule_form_commerce_order_item_add_to_cart_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $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 = \Drupal\commerce_product\Entity\ProductVariation::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;
}
}
}
}
}
Я добавил commerce-order-item-add-to-cart-form-commerce-product-50
в свой код, но он не работает:
<?php
use Drupal\commerce_store\Entity\StoreType;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
function mymodule_form_commerce_order_item_add_to_cart_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $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 = \Drupal\commerce_product\Entity\ProductVariation::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;
}
}
}
}
}
введите описание изображения здесь