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

“Add and checkout” button with “Add to cart” button

I need two buttons on product page:

  • “Add to cart” – adding product to cart and staying on page
  • “Buy and checkout” – adding product to cart and redirect to checkout page

I knowns about backend option “After Adding a Product Redirect to Shopping Cart”, but if it’s turned on, redirect to checkout will be after click by any of those buttons.

How can i do this?

Get cart details in home page

I have a custom module which will get the cart details in the block and rendering those details in phtml (template) file. Things are working in all pages except home page. In home page its coming as empty values.

Here is the Layout config ,

Root\vendor\namespace\module-xxx\view\frontend\layout\default.xml

<referenceContainer name="main">         <block class="Namespace\Module\Block\Webpayment" name="namespace.web.payment" template="Namespace_Module::webpayment.phtml"/>      </referenceContainer> 

Root\vendor\namespace\module-xxx\Block\Webpayment.php

use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\Template; use Magento\Checkout\Block\Cart\AbstractCart;  class Webpayment extends Template {      private $  _cart;     private $  _helper;      public function __construct(         Template\Context $  context,         AbstractCart $  cart,         Data $  helper,         array $  data = [])     {          $  this->_helper = $  helper;         $  this->_cart = $  cart;         parent::__construct(             $  context,             $  data         );     } ........ 

Root\vendor\namespace\module-xxx\view\frontend\templates\webpayment.phtml

/** @var $  block \Namespace\Module\Block\Webpayment */ $  block->getAllItems() 

I put some logs as well in template file to check whether this block is rendering in home or not. But it’s loading on home page with empty $ block. Any suggestions will be appreciated..