如何自定义产品的“添加到购物车”按钮?

时间:2019-01-18 04:24:33

标签: php drupal drupal-modules drupal-8 drupal-commerce

我使用Drupal 8和Commerce 2.11创建了一个自定义模块,以修改产品表的“添加到购物车”按钮。

该模块有效,但是当前已应用于所有产品。

我只想将其应用于ID为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;
        }
      }
    }
  }
}

enter image description here

0 个答案:

没有答案