You are here

class DefaultVariationSubscriber in Commerce Core 8.2

Hierarchy

  • class \Drupal\commerce_product_test\EventSubscriber\DefaultVariationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of DefaultVariationSubscriber

1 string reference to 'DefaultVariationSubscriber'
commerce_product_test.services.yml in modules/product/tests/modules/commerce_product_test/commerce_product_test.services.yml
modules/product/tests/modules/commerce_product_test/commerce_product_test.services.yml
1 service uses DefaultVariationSubscriber
commerce_product_test.default_variation_subscriber in modules/product/tests/modules/commerce_product_test/commerce_product_test.services.yml
Drupal\commerce_product_test\EventSubscriber\DefaultVariationSubscriber

File

modules/product/tests/modules/commerce_product_test/src/EventSubscriber/DefaultVariationSubscriber.php, line 9

Namespace

Drupal\commerce_product_test\EventSubscriber
View source
class DefaultVariationSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ProductEvents::PRODUCT_DEFAULT_VARIATION => 'onDefaultVariation',
    ];
  }

  /**
   * Handle the default variation event.
   *
   * @param \Drupal\commerce_product\Event\ProductDefaultVariationEvent $event
   *   The event.
   */
  public function onDefaultVariation(ProductDefaultVariationEvent $event) {
    if ($event
      ->getDefaultVariation()
      ->getSku() === 'TEST_DEFAULT_VARIATION_EVENT') {
      $variations = $event
        ->getProduct()
        ->getVariations();
      $new_default = end($variations);
      $event
        ->setDefaultVariation($new_default);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultVariationSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
DefaultVariationSubscriber::onDefaultVariation public function Handle the default variation event.