Last active
March 8, 2019 21:06
-
-
Save wanze/3d1d7f86b87b3f3bacfa409aaa5b148c to your computer and use it in GitHub Desktop.
commerce_google_tag_manager: Example of an AlterEventData 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\AlterEventDataEvent; | |
class AlterShippingData implements EventSubscriberInterface { | |
public static function getSubscribedEvents() { | |
return [ | |
'commerce_gtm_enhanced_ecommerce.alter_event_data' => 'addShipping' | |
]; | |
} | |
public function addShipping(AlterEventDataEvent $event) { | |
$data = $event->getEventData(); | |
// Only alter the "purchase" event. | |
if ($data['event'] !== 'purchase') { | |
return; | |
} | |
// Set the shipping cost. | |
$shipping_fee = OrderProcessor::SHIPPING_FEE; | |
$data['ecommerce']['purchase']['actionField']['shipping'] = $shipping_fee; | |
$event->setEventData($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment