You are here

function commerce_add_to_cart_link_commerce_product_view in Commerce Add To Cart Link 8

Same name and namespace in other branches
  1. 2.x commerce_add_to_cart_link.module \commerce_add_to_cart_link_commerce_product_view()

Implements hook_ENTITY_TYPE_view() for commerce_product.

File

./commerce_add_to_cart_link.module, line 79
Hook implementations of commerce_add_to_cart_link module.

Code

function commerce_add_to_cart_link_commerce_product_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  /** @var \Drupal\commerce_product\Entity\ProductInterface $entity */
  if ($display
    ->getComponent('add_to_cart_link')) {
    $variation = $entity
      ->getDefaultVariation();
    if (empty($variation)) {

      // Avoid errors, if the product has no (active) variations.
      return;
    }
    $build['add_to_cart_link'] = (new AddToCartLink($variation))
      ->build();
  }
}