You are here

private function AjaxCartHelper::getCartBlockId in Ajax Add to Cart 8

Gets the machine name (id) of a commerce cart block visible on the current page. Returns only the first cart found

Parameters

none:

Return value

mixed or FALSE Return id of the first commerce cart block found on current page. Returns FALSE if no commerce cart block is visible.

1 call to AjaxCartHelper::getCartBlockId()
AjaxCartHelper::getCartBlock in src/Helper/AjaxCartHelper.php
Get cart block.

File

src/Helper/AjaxCartHelper.php, line 93

Class

AjaxCartHelper
Class AjaxCartHelper.

Namespace

Drupal\ajax_add_to_cart\Helper

Code

private function getCartBlockId() {
  $blockRepo = Drupal::service('block.repository');

  //Returns an array of regions each with an array of blocks
  $regions = $blockRepo
    ->getVisibleBlocksPerRegion();

  //Iterate all visible blocks and regions
  foreach ($regions as $region) {
    foreach ($region as $block) {
      $idPlugin = $block
        ->get('plugin');

      //check if this is a commerce cart block
      if ($idPlugin == 'commerce_cart') {
        $cartBlockId = $block
          ->get('id');
        return $cartBlockId;
      }
    }
  }
  return FALSE;
}