PromotionSubscriber.php in Commerce Shipping 8.2
File
src/EventSubscriber/PromotionSubscriber.php
View source
<?php
namespace Drupal\commerce_shipping\EventSubscriber;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_promotion\Entity\CouponInterface;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\CombinationOfferInterface;
use Drupal\commerce_promotion\PromotionOfferManager;
use Drupal\commerce_shipping\Event\ShippingEvents;
use Drupal\commerce_shipping\Event\ShippingRatesEvent;
use Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentPromotionOfferInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PromotionSubscriber implements EventSubscriberInterface {
protected $promotionStorage;
protected $offerManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, PromotionOfferManager $offer_manager) {
$this->promotionStorage = $entity_type_manager
->getStorage('commerce_promotion');
$this->offerManager = $offer_manager;
}
public static function getSubscribedEvents() {
return [
ShippingEvents::SHIPPING_RATES => 'onCalculate',
];
}
public function onCalculate(ShippingRatesEvent $event) {
$rates = $event
->getRates();
if (empty($rates)) {
return;
}
$shipping_method = $event
->getShippingMethod();
$shipment = $event
->getShipment();
$order = $shipment
->getOrder();
$promotions = $this
->getPromotions($order);
if (empty($promotions)) {
return;
}
$fake_shipment = clone $shipment;
$fake_order = clone $order;
$fake_order
->set('shipments', [
$fake_shipment,
]);
$fake_shipments = $fake_order
->get('shipments')
->referencedEntities();
$fake_shipment = reset($fake_shipments);
foreach ($rates as $rate) {
$shipping_method
->getPlugin()
->selectRate($fake_shipment, $rate);
$fake_shipment
->clearAdjustments();
foreach ($promotions as $promotion) {
if ($promotion
->applies($fake_order)) {
$promotion
->apply($fake_order);
}
}
$rate
->setAmount($fake_shipment
->getAmount());
}
}
protected function getPromotions(OrderInterface $order) {
$offer_ids = $this
->getOfferIds();
if (!$offer_ids) {
return [];
}
$promotions = $this->promotionStorage
->loadAvailable($order, $offer_ids);
$coupons = $this
->getCoupons($order, $offer_ids);
foreach ($coupons as $coupon) {
$promotion = $coupon
->getPromotion();
$promotions[$promotion
->id()] = $promotion;
}
$promotions = array_filter($promotions, function (PromotionInterface $promotion) {
$offer = $promotion
->getOffer();
if ($offer instanceof CombinationOfferInterface) {
foreach ($offer
->getOffers() as $configured_offer) {
if (!$configured_offer instanceof ShipmentPromotionOfferInterface) {
continue;
}
if ($configured_offer
->isDisplayInclusive()) {
return TRUE;
}
}
return FALSE;
}
assert($offer instanceof ShipmentPromotionOfferInterface);
return $offer
->isDisplayInclusive();
});
return $promotions;
}
protected function getCoupons(OrderInterface $order, array $offer_ids) {
$coupons = $order
->get('coupons')
->referencedEntities();
$coupons = array_filter($coupons, function (CouponInterface $coupon) use ($offer_ids) {
return in_array($coupon
->getPromotion()
->getOffer()
->getPluginId(), $offer_ids);
});
return $coupons;
}
protected function getOfferIds() {
$definitions = $this->offerManager
->getDefinitions();
$definitions = array_filter($definitions, function ($definition) {
return is_subclass_of($definition['class'], ShipmentPromotionOfferInterface::class) || is_subclass_of($definition['class'], CombinationOfferInterface::class);
});
$offer_ids = array_keys($definitions);
return $offer_ids;
}
}