Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\custom_commerce_recurring;
  4.  
  5. use Drupal\commerce_order\Entity\OrderInterface;
  6. use Drupal\commerce_recurring\RecurringOrderManager as BaseRecurringOrderManager;
  7. use Drupal\commerce_recurring\RecurringOrderManagerInterface;
  8. use Drupal\Component\Datetime\TimeInterface;
  9. use Drupal\Core\Entity\EntityTypeManagerInterface;
  10.  
  11. /**
  12. * Overrides the default recurring order manager.
  13. */
  14. class RecurringOrderManager extends BaseRecurringOrderManager {
  15.  
  16. /**
  17. * The recurring order manager.
  18. *
  19. * @var \Drupal\commerce_recurring\RecurringOrderManagerInterface
  20. */
  21. protected $recurringOrderManager;
  22.  
  23. /**
  24. * Constructs a new RecurringOrderManager object.
  25. *
  26. * @param \Drupal\commerce_recurring\RecurringOrderManagerInterface $recurring_order_manager
  27. * The recurring order manager.
  28. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  29. * The entity type manager.
  30. * @param \Drupal\Component\Datetime\TimeInterface $time
  31. * The time.
  32. */
  33. public function __construct(RecurringOrderManagerInterface $recurring_order_manager, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
  34. $this->recurringOrderManager = $recurring_order_manager;
  35. parent::__construct($entity_type_manager, $time);
  36. }
  37.  
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function closeOrder(OrderInterface $order) {
  42. $subscriptions = $this->recurringOrderManager->collectSubscriptions($order);
  43. /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  44. $subscription = reset($subscriptions);
  45. /** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $subscription_state */
  46. $subscription_state = $subscription ? $subscription->getState() : NULL;
  47. // When a subscription is revoked then on close we cancel it.
  48. if ($subscription_state->value == 'revoked') {
  49. // Cancelling the order.
  50. $transition = $order->getState()->getWorkflow()->getTransition('cancel');
  51. $order->getState()->applyTransition($transition);
  52. $order->save();
  53. // Cancelling the subscription.
  54. $transition = $subscription_state->getWorkflow()->getTransition('cancel');
  55. $subscription->getState()->applyTransition($transition);
  56. $subscription->save();
  57. return;
  58. }
  59.  
  60. $this->recurringOrderManager->closeOrder($order);
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment