Last active
March 8, 2019 21:06
-
-
Save wanze/9996444e7f5808b4c7a12ca128dedd16 to your computer and use it in GitHub Desktop.
commerce_google_tag_manager: Example implementation of an AlterProduct event subscriber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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