- 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!