- πΊπΈUnited States joshuasosa
Is there an example that could be provided for making this request a possibility? Just posting a link to the event subscriber source and long documentation to how event subscribers work isn't very helpful.
Going further, I believe it would be better if the number field could automatically multiply by the price of the field instead of making people custom code a solution that should be an obvious inclusion. Quantity and multiplying cost by that quantity is a very common scenario for checkout systems.
- πΊπΈUnited States joshuasosa
For anyone interested, I think I was able to get things situated:
mymodule/mymodule.services.yml
services: # Subscriber to the event we dispatch in hook_order_item. mymodule_order_item: class: '\Drupal\mymodule\EventSubscriber\OrderItemSubscriber' tags: - { name: 'event_subscriber' }
mymodule/src/EventSubscriber/OrderItemSubscriber.php
<?php namespace Drupal\mymodule\EventSubscriber; use Drupal\commerce_order\Entity\OrderItem; use Drupal\webform_product\Event\OrderItemEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Class OrderItemSubscriber. * * @package Drupal\mymodule\EventSubscriber */ class OrderItemSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} */ public static function getSubscribedEvents() { return [ // Static class constant => method on this class. OrderItemEvent::EVENT_NAME => 'onOrderItem', ]; } /** * Subscribe to the order item event dispatched. * * @param \Drupal\webform_product\Event\OrderItemEvent $event * Our custom event object. */ public function onOrderItem(OrderItemEvent $event) { $webform = $event->webformSubmission->toArray(); if ($webform['webform_id'][0]['target_id'] == 'mywebform_id') { $dinner_count = $event->webformSubmission->getElementData('dinner_count'); if ($dinner_count > 0) { $event->orderItems[] = OrderItem::create([ 'type' => 'webform', 'title' => 'Dinner guest count (' . $dinner_count . ' entries at $20 ea)', 'quantity' => $dinner_count, 'unit_price' => [ 'number' => 20, 'currency_code' => 'USD', ], ]); } } } }