private function EventTrackerService::buildProductFromOrderItem in Commerce Google Tag Manager 8
Same name and namespace in other branches
- 8.2 src/EventTrackerService.php \Drupal\commerce_google_tag_manager\EventTrackerService::buildProductFromOrderItem()
Build the Enhanced Ecommerce product from a given commerce order item.
Parameters
\Drupal\commerce_order\Entity\OrderItemInterface $order_item: A commerce order item.
Return value
\Drupal\commerce_google_tag_manager\Product The Enhanced Ecommerce product.
2 calls to EventTrackerService::buildProductFromOrderItem()
- EventTrackerService::addToCart in src/
EventTrackerService.php - Track the "addToCart" event.
- EventTrackerService::removeFromCart in src/
EventTrackerService.php - Track the "removeFromCart" event.
File
- src/
EventTrackerService.php, line 307
Class
- EventTrackerService
- Track different events from Google's Enhanced Ecommerce.
Namespace
Drupal\commerce_google_tag_managerCode
private function buildProductFromOrderItem(OrderItemInterface $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if ($purchased_entity instanceof ProductVariationInterface) {
$product = $this
->buildProductFromProductVariation($purchased_entity);
}
else {
// The purchased entity is not a product variation.
$product = (new Product())
->setName($order_item
->getTitle())
->setId($order_item
->getPurchasedEntityId())
->setPrice(self::formatPrice((double) $order_item
->getTotalPrice()
->getNumber()));
$event = new AlterProductPurchasedEntityEvent($product, $order_item, $purchased_entity);
$this->eventDispatcher
->dispatch(EnhancedEcommerceEvents::ALTER_PRODUCT_PURCHASED_ENTITY, $event);
}
return $product;
}