Number field Price multiply from user input?

Created on 26 December 2021, almost 3 years ago
Updated 7 June 2024, 5 months ago

Problem/Motivation

I used a number field and set the price to $10. In frontend when a user opens form and in this number field inputs 2, 3, or 4.. or any number. When submitting form can the price be multiplied by the number input which the user submit? Is this the intended use of number field for this module?

Steps to reproduce

Install webform, commerce, webform_product, and depdencies. Create a webform. Add number field to webform. Go to the form page and submit it.

Proposed resolution

Allow number field price to be altered or multiplied from user input of number upon checkout.

✨ Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Tankeroo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡Έ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',
              ],
            ]);
          }
        }
      }
    }
    
Production build 0.71.5 2024