Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\custom_commerce_recurring;
- use Drupal\commerce_order\Entity\OrderInterface;
- use Drupal\commerce_recurring\RecurringOrderManager as BaseRecurringOrderManager;
- use Drupal\commerce_recurring\RecurringOrderManagerInterface;
- use Drupal\Component\Datetime\TimeInterface;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- /**
- * Overrides the default recurring order manager.
- */
- class RecurringOrderManager extends BaseRecurringOrderManager {
- /**
- * The recurring order manager.
- *
- * @var \Drupal\commerce_recurring\RecurringOrderManagerInterface
- */
- protected $recurringOrderManager;
- /**
- * Constructs a new RecurringOrderManager object.
- *
- * @param \Drupal\commerce_recurring\RecurringOrderManagerInterface $recurring_order_manager
- * The recurring order manager.
- * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
- * The entity type manager.
- * @param \Drupal\Component\Datetime\TimeInterface $time
- * The time.
- */
- public function __construct(RecurringOrderManagerInterface $recurring_order_manager, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
- $this->recurringOrderManager = $recurring_order_manager;
- parent::__construct($entity_type_manager, $time);
- }
- /**
- * {@inheritdoc}
- */
- public function closeOrder(OrderInterface $order) {
- $subscriptions = $this->recurringOrderManager->collectSubscriptions($order);
- /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
- $subscription = reset($subscriptions);
- /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $subscription_state */
- $subscription_state = $subscription ? $subscription->getState() : NULL;
- // When a subscription is revoked then on close we cancel it.
- if ($subscription_state->value == 'revoked') {
- // Cancelling the order.
- $transition = $order->getState()->getWorkflow()->getTransition('cancel');
- $order->getState()->applyTransition($transition);
- $order->save();
- // Cancelling the subscription.
- $transition = $subscription_state->getWorkflow()->getTransition('cancel');
- $subscription->getState()->applyTransition($transition);
- $subscription->save();
- return;
- }
- $this->recurringOrderManager->closeOrder($order);
- }
- }
Add Comment
Please, Sign In to add comment