Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\mymodule\Plugin\Block;
- use Drupal\commerce_cart\CartProviderInterface;
- use Drupal\commerce_checkout\CheckoutOrderManagerInterface;
- use Drupal\commerce_checkout\Plugin\Block\CheckoutProgressBlock as BaseBlock;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\Core\Routing\RouteMatchInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Provides a checkout progress block.
- *
- * @Block(
- * id = "mymodule_commerce_checkout_progress",
- * admin_label = @Translation("Desmazieres - Checkout progress"),
- * category = @Translation("Commerce")
- * )
- */
- class CheckoutProgressBlock extends BaseBlock implements ContainerFactoryPluginInterface {
- /**
- * The cart provider.
- *
- * @var \Drupal\commerce_cart\CartProviderInterface
- */
- protected $cartProvider;
- /**
- * {@inheritDoc}
- */
- public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutOrderManagerInterface $checkout_order_manager, RouteMatchInterface $route_match, CartProviderInterface $cart_provider) {
- parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_order_manager, $route_match);
- $this->cartProvider = $cart_provider;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('commerce_checkout.checkout_order_manager'),
- $container->get('current_route_match'),
- $container->get('commerce_cart.cart_provider')
- );
- }
- /**
- * Builds the checkout progress block.
- *
- * @return array
- * A render array.
- *
- * @see \Drupal\commerce_cart\Plugin\Block\CartBlock::build();
- */
- public function build() {
- // Try to build regularly.
- $build = parent::build();
- // If nothing found, try to get the current Cart and rebuild.
- if (empty($build) && $this->routeMatch->getRouteName() == 'commerce_cart.page') {
- $carts = $this->cartProvider->getCarts();
- $carts = array_filter($carts, function ($cart) {
- return $cart->hasItems() && $cart->cart->value;
- });
- if (!empty($carts)) {
- $order = reset($carts);
- $build = parent::render($order);
- }
- }
- return $build;
- }
- }
Add Comment
Please, Sign In to add comment