douyin2435 2019-01-18 04:24
浏览 31

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

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 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;
        }
      }
    }
  }
}

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

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探