0

Drupal 8 and Drupal Commerce 2.10

I have several types of store on my site and I want to modify the button Add to cart of all the products associated with the store type online (online is the machine name of my store type).

How to do this ?

The custom module below works and changes the Add to Cart button for products in all store types.

How to limit it to the type of store online ?

This must be applied to the products of the current store.

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;

/**
 * Store online.
 */
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');
    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');
    }
}

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy