Add the ability to change product for existing registration

Created on 10 March 2025, about 1 month ago

Problem/Motivation

The existing functionality in the Registration module provided by ✨ Allow admins to change host entity for existing registration Active allows updating the associated node for a registration but does not permit changing the referenced product (at first glance and trying it out and in registration_change_host_test_entity_base_field_info). This limitation is problematic for sites that do not use product nodes or need to allow modifications to the product selection after registration creation.

Steps to reproduce

  1. Enable the registration_change_host sub module and configure a registration with a referenced product.
  2. Attempt to update the change product associated with an existing registration.
  3. There is nothing we can change to, no way to change the product the registration is associated with

Proposed resolution

TBD

Remaining tasks

User interface changes

  • If a UI change is made, provide a way for users to modify the referenced product in a registration.
πŸ’¬ Support request
Status

Active

Version

3.4

Component

Registration Core

Created by

πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

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

Comments & Activities

  • Issue created by @joelpittet
  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    I set this as a support request instead of feature request because maybe I am overlooking an obvious way to switch out referenced products.

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    We connected on Slack but for anyone else who comes across this issue.

    You need to upgrade Commerce Registration to this release:
    https://www.drupal.org/project/commerce_registration/releases/3.1.5 β†’

    That provides the event subscriber needed for commerce product variations.

  • πŸ‡¨πŸ‡¦Canada franceslui

    The commerce_registration_change_host submodule allows the product variation to be changed after a registration is made. However, our site requires the ability to change the product itself, rather than just the variation. As a result, this submodule does not fully meet our needs.

    To address this, we followed the approach used in ChangeHostSubscriber within this submodule to find and display all products and their variations.

    Here is our code in case others need to implement a similar solution:

    use Drupal\commerce_product\Entity\ProductVariationInterface;
    use Drupal\registration_change_host\Event\RegistrationChangeHostEvents;
    use Drupal\registration_change_host\Event\RegistrationChangeHostPossibleHostsEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    final class ChangeHostSubscriber implements EventSubscriberInterface {
    
      /**
       * Add possible hosts.
       *
       * @param \Drupal\registration_change_host\Event\RegistrationChangeHostPossibleHostsEvent $event
       *   The registration change host event.
       */
      public function getPossibleHosts(RegistrationChangeHostPossibleHostsEvent $event) {
        $registration = $event->getRegistration();
        $set = $event->getPossibleHostsSet();
        $entity = $registration->getHostEntity()->getEntity();
        if ($entity && $entity instanceof ProductVariationInterface) {
          $products = \Drupal::entityTypeManager()->getStorage('commerce_product')->loadMultiple();
          foreach ($products as $product) {
            if ($product) {
              foreach ($product->getVariations() as $variation) {
                $access = $variation->access('view label', NULL, TRUE);
                if ($variation->id() != $entity->id() && $access->isAllowed()) {
                  $possible_host = $set->buildNewPossibleHost($variation);
                  if ($possible_host->getHostEntity()->isConfiguredForRegistration()) {
                    $set->addHost($possible_host);
                  }
                }
                // A variation could become a possible host if it became accessible
                // or configured for registration.
                $set->addCacheableDependency($access);
                $set->addCacheableDependency($variation);
              }
              // Any new variations added to the product could be possible hosts.
              $set->addCacheableDependency($product);
            }
          }
        }
      }
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents(): array {
        return [
          RegistrationChangeHostEvents::REGISTRATION_CHANGE_HOST_POSSIBLE_HOSTS => 'getPossibleHosts',
        ];
      }
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    Thanks @franceslui for posting this example!

Production build 0.71.5 2024