class PromotionSubscriber in Commerce Shipping 8.2
Applies display-inclusive promotions to the calculated shipping rates.
Hierarchy
- class \Drupal\commerce_shipping\EventSubscriber\PromotionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of PromotionSubscriber
1 file declares its use of PromotionSubscriber
File
- src/
EventSubscriber/ PromotionSubscriber.php, line 19
Namespace
Drupal\commerce_shipping\EventSubscriberView source
class PromotionSubscriber implements EventSubscriberInterface {
/**
* The promotion storage.
*
* @var \Drupal\commerce_promotion\PromotionStorageInterface
*/
protected $promotionStorage;
/**
* THe offer manager.
*
* @var \Drupal\commerce_promotion\PromotionOfferManager
*/
protected $offerManager;
/**
* Constructs a new PromotionSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_promotion\PromotionOfferManager $offer_manager
* The offer manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, PromotionOfferManager $offer_manager) {
$this->promotionStorage = $entity_type_manager
->getStorage('commerce_promotion');
$this->offerManager = $offer_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ShippingEvents::SHIPPING_RATES => 'onCalculate',
];
}
/**
* Applies the promotions.
*
* @param \Drupal\commerce_shipping\Event\ShippingRatesEvent $event
* The event.
*/
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;
}
// Clone the entities to avoid modifying the original data.
$fake_shipment = clone $shipment;
$fake_order = clone $order;
$fake_order
->set('shipments', [
$fake_shipment,
]);
// Re-fetch the shipment to acquire a reference.
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $fake_shipments */
$fake_shipments = $fake_order
->get('shipments')
->referencedEntities();
$fake_shipment = reset($fake_shipments);
// Calculate the discounted amounts.
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());
}
}
/**
* Gets the display-inclusive shipping promotions for the given order.
*
* This includes both automatic and coupon-based promotions.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return \Drupal\commerce_promotion\Entity\PromotionInterface[]
* The promotions.
*/
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 this is a combination offer, check if there is a display inclusive
// shipment promotion offer configured.
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;
}
/**
* Gets the shipping coupons for the given order.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
* @param string[] $offer_ids
* The shipping offer IDs.
*
* @return \Drupal\commerce_promotion\Entity\CouponInterface[]
* The coupons.
*/
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;
}
/**
* Gets the shipping offer IDs.
*
* @return string[]
* The offer IDs.
*/
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;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PromotionSubscriber:: |
protected | property | THe offer manager. | |
PromotionSubscriber:: |
protected | property | The promotion storage. | |
PromotionSubscriber:: |
protected | function | Gets the shipping coupons for the given order. | |
PromotionSubscriber:: |
protected | function | Gets the shipping offer IDs. | |
PromotionSubscriber:: |
protected | function | Gets the display-inclusive shipping promotions for the given order. | |
PromotionSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
PromotionSubscriber:: |
public | function | Applies the promotions. | |
PromotionSubscriber:: |
public | function | Constructs a new PromotionSubscriber object. |