class OrderItemSubscriber in Commerce Shipping 8.2
Hierarchy
- class \Drupal\commerce_shipping\EventSubscriber\OrderItemSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of OrderItemSubscriber
1 string reference to 'OrderItemSubscriber'
1 service uses OrderItemSubscriber
File
- src/
EventSubscriber/ OrderItemSubscriber.php, line 10
Namespace
Drupal\commerce_shipping\EventSubscriberView source
class OrderItemSubscriber implements EventSubscriberInterface {
/**
* The shipping order manager.
*
* @var \Drupal\commerce_shipping\ShippingOrderManagerInterface
*/
protected $shippingOrderManager;
/**
* Constructs a new OrderSubscriber object.
*
* @param \Drupal\commerce_shipping\ShippingOrderManagerInterface $shipping_order_manager
* The shipping order manager.
*/
public function __construct(ShippingOrderManagerInterface $shipping_order_manager) {
$this->shippingOrderManager = $shipping_order_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
'commerce_order.commerce_order_item.update' => [
'onOrderItemUpdate',
],
'commerce_order.commerce_order_item.delete' => [
'onOrderItemDelete',
],
];
}
/**
* Force repack/rates recalculation when quantity is updated.
*
* @param \Drupal\commerce_order\Event\OrderItemEvent $order_item_event
* Order item event.
*/
public function onOrderItemUpdate(OrderItemEvent $order_item_event) {
$order_item = $order_item_event
->getOrderItem();
$order = $order_item
->getOrder();
if (!$order || !$this
->shouldRefresh($order)) {
return;
}
if ($order_item
->getQuantity() !== $order_item->original
->getQuantity()) {
$order
->setData(ShippingOrderManagerInterface::FORCE_REFRESH, TRUE);
}
}
/**
* Force repack/rates recalculation when an order item is removed.
*
* @param \Drupal\commerce_order\Event\OrderItemEvent $order_item_event
* Order item event.
*/
public function onOrderItemDelete(OrderItemEvent $order_item_event) {
$order_item = $order_item_event
->getOrderItem();
$order = $order_item
->getOrder();
if (!$order || !$this
->shouldRefresh($order)) {
return;
}
$order
->setData(ShippingOrderManagerInterface::FORCE_REFRESH, TRUE);
}
/**
* Checks whether we should force a shipping refresh.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* Order entity.
*
* @return bool
* Whether we should force a shipping refresh.
*/
protected function shouldRefresh(OrderInterface $order) {
return $order
->getState()
->getId() == 'draft' && $this->shippingOrderManager
->hasShipments($order);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderItemSubscriber:: |
protected | property | The shipping order manager. | |
OrderItemSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
OrderItemSubscriber:: |
public | function | Force repack/rates recalculation when an order item is removed. | |
OrderItemSubscriber:: |
public | function | Force repack/rates recalculation when quantity is updated. | |
OrderItemSubscriber:: |
protected | function | Checks whether we should force a shipping refresh. | |
OrderItemSubscriber:: |
public | function | Constructs a new OrderSubscriber object. |