Skip to content

Instantly share code, notes, and snippets.

@wanze
Last active March 8, 2019 21:06
Show Gist options
  • Save wanze/9996444e7f5808b4c7a12ca128dedd16 to your computer and use it in GitHub Desktop.
Save wanze/9996444e7f5808b4c7a12ca128dedd16 to your computer and use it in GitHub Desktop.
commerce_google_tag_manager: Example implementation of an AlterProduct event subscriber
<?php
use Drupal\commerce_google_tag_manager\Event\AlterProductEvent;
class AlterProductSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'commerce_gtm_enhanced_ecommerce.alter_product' => 'alterProduct'
];
}
public function alterProduct(AlterProductEvent $event) {
// The Commerce product variation entity.
$productVariation = $event->getProductVariation();
// The product represented in Enhanced Ecommerce.
$product = $event->getProduct();
// Set the ID to the title of the product.
// Set the name to the value of the field "field_title".
$product
->setId($productVariation->getProduct()->getTitle())
->setName($productVariation->getProduct()->get('field_title')->value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment