MaterioCommerceOrderEventsSubscriber.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // https://docs.drupalcommerce.org/commerce2/developer-guide/orders/workflows/react-to-workflow-transitions
  3. namespace Drupal\materio_commerce\EventSubscriber;
  4. use Drupal\commerce_order\Event\OrderEvent;
  5. use Drupal\commerce_order\Event\OrderEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8. * Class EntityTypeSubscriber.
  9. *
  10. * @package Drupal\custom_events\EventSubscriber
  11. */
  12. class MaterioCommerceOrderEventsSubscriber implements EventSubscriberInterface {
  13. /**
  14. * {@inheritdoc}
  15. *
  16. * @return array
  17. * The event names to listen for, and the methods that should be executed.
  18. */
  19. public static function getSubscribedEvents() {
  20. return [
  21. OrderEvents::ORDER_PAID => 'onOrderPaid'
  22. // OrderEvents::ORDER_CREATE => 'onOrderCreate'
  23. // 'commerce_order.order.paid' => ['onOrderPaid', -100]
  24. ];
  25. }
  26. public function onOrderPaid(OrderEvent $event) {
  27. $order = $event->getOrder();
  28. $customer = $order->getCustomer();
  29. foreach ($order->getItems() as $order_item) {
  30. // Skip order items that do not have a license reference field.
  31. // We check order items rather than the purchased entity to allow products
  32. // with licenses to be purchased without the checkout flow triggering
  33. // our synchronization. This is for cases such as recurring orders, where
  34. // the license entity should not be put through the normal workflow.
  35. // Checking the order item's bundle for our entity trait is expensive, as
  36. // it requires loading the bundle entity to call hasTrait() on it.
  37. // For now, just check whether the order item has our trait's field on it.
  38. // @see https://www.drupal.org/node/2894805
  39. if (!$order_item->hasField('license')) {
  40. continue;
  41. }
  42. $purchased_entity = $order_item->getPurchasedEntity();
  43. // This order item isn't "licensable" if the purchased entity it
  44. // references isn't properly configured.
  45. if (!$purchased_entity->hasField('license_type') || $purchased_entity->get('license_type')->isEmpty()) {
  46. continue;
  47. }
  48. $sku = $purchased_entity->getSku();
  49. switch ($sku) {
  50. case 'web-monthly':
  51. case 'web-annual':
  52. $member_type = 0;
  53. break;
  54. case 'web-showroom-monthly':
  55. case 'web-showroom-annual':
  56. $member_type = 1;
  57. break;
  58. }
  59. }
  60. if(isset($member_type)){
  61. $customer->set('field_member_type', $member_type);
  62. $customer->save();
  63. }
  64. // \Drupal::messenger()->addStatus('HAHA Order '.$order->getOrderNumber().' paid: ' . $customer->getEmail());
  65. }
  66. // public function onOrderCreate(OrderEvent $event) {
  67. // $order = $event->getOrder();
  68. // $customer = $order->getCustomer();
  69. // \Drupal::messenger()->addStatus('HAHA Order '.$order->getOrderNumber().' created: ' . $customer->getEmail());
  70. // }
  71. }