Last active
October 19, 2017 18:14
-
-
Save luksak/c3c6f69fc0dbdb6f053e3d00f2706cbb to your computer and use it in GitHub Desktop.
Display message if order total is below a certain amount such as to get free shipping after adding products to the cart and when viewing the cart page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\MODULE\EventSubscriber; | |
use Drupal\commerce_order\Entity\Order; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Drupal\commerce_cart\Event\CartEntityAddEvent; | |
use Drupal\commerce_cart\Event\CartEvents; | |
use Drupal\Core\StringTranslation\StringTranslationTrait; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
class CartEventSubscriber implements EventSubscriberInterface { | |
use StringTranslationTrait; | |
/** | |
* The amount from which orders get free shipping | |
* | |
* @var int | |
*/ | |
private $freeShippingAmount = 250; | |
/** | |
* Constructs a new CartEventSubscriber object. | |
* | |
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation | |
* The string translation. | |
*/ | |
public function __construct(TranslationInterface $string_translation) { | |
$this->stringTranslation = $string_translation; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() { | |
$events = [ | |
CartEvents::CART_ENTITY_ADD => 'addToCart', | |
KernelEvents::REQUEST => 'processPage', | |
]; | |
return $events; | |
} | |
/** | |
* Displays an add to cart message. | |
* | |
* @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event | |
* The add to cart event. | |
*/ | |
public function addToCart(CartEntityAddEvent $event) { | |
$order = $event->getCart(); | |
$this->displayFreeShippingMessage($order); | |
} | |
/** | |
* Displays an add to cart message. | |
* | |
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event | |
* The add to cart event. | |
*/ | |
public function processPage(GetResponseEvent $event) { | |
$method = $event->getRequest()->getMethod(); | |
$route_name = \Drupal::routeMatch()->getRouteName(); | |
if($route_name === 'commerce_cart.page' && $method === 'GET') { | |
/* @var \Drupal\commerce_cart\CartProvider $cart_provider */ | |
$cart_provider = \Drupal::service('commerce_cart.cart_provider'); | |
$carts = $cart_provider->getCarts(); | |
if(count($carts)) { | |
$order = array_values($carts)[0]; | |
$this->displayFreeShippingMessage($order); | |
} | |
} | |
} | |
/** | |
* Displays an add to cart message. | |
* | |
* @param \Drupal\commerce_order\Entity\Order $order | |
* The add to cart event. | |
*/ | |
public function displayFreeShippingMessage(Order $order) { | |
$total = $order->getTotalPrice(); | |
$price = new \Drupal\commerce_price\Price($this->freeShippingAmount, 'CHF'); | |
if($total && $total->lessThan($price)) { | |
drupal_set_message($this->t('Orders above @amount get free shipping worldwide!', [ | |
'@amount' => $price, | |
])); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
MODULE.cart_subscriber: | |
class: Drupal\MODULE\EventSubscriber\CartEventSubscriber | |
arguments: ['@string_translation'] | |
tags: | |
- { name: event_subscriber } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment