XquantityCartBlock.php in Commerce Extended Quantity 8
File
src/Plugin/Block/XquantityCartBlock.php
View source
<?php
namespace Drupal\commerce_xquantity\Plugin\Block;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;
use Drupal\commerce_cart\Plugin\Block\CartBlock;
class XquantityCartBlock extends CartBlock {
public function build() {
$cachable_metadata = new CacheableMetadata();
$cachable_metadata
->addCacheContexts([
'user',
'session',
]);
$carts = $this->cartProvider
->getCarts();
$carts = array_filter($carts, function ($cart) {
return $cart
->hasItems() && $cart->cart->value;
});
$count = 0;
$cart_views = [];
if (!empty($carts)) {
$cart_views = $this
->getCartViews($carts);
foreach ($carts as $cart_id => $cart) {
foreach ($cart
->getItems() as $order_item) {
$count += $order_item
->getItemsQuantity();
}
$cachable_metadata
->addCacheableDependency($cart);
}
}
$links = [];
$links[] = [
'#type' => 'link',
'#title' => $this
->t('Cart'),
'#url' => Url::fromRoute('commerce_cart.page'),
];
return [
'#attached' => [
'library' => [
'commerce_cart/cart_block',
],
],
'#theme' => 'commerce_cart_block',
'#icon' => [
'#theme' => 'image',
'#uri' => drupal_get_path('module', 'commerce') . '/icons/ffffff/cart.png',
'#alt' => $this
->t('Shopping cart'),
],
'#count' => $count,
'#count_text' => $this
->formatPlural($count, '@count item', '@count items'),
'#url' => Url::fromRoute('commerce_cart.page')
->toString(),
'#content' => $cart_views,
'#links' => $links,
'#cache' => [
'contexts' => [
'cart',
],
],
];
}
}