private function EventTrackerService::buildProductFromProductVariation in Commerce Google Tag Manager 8.2
Same name and namespace in other branches
- 8 src/EventTrackerService.php \Drupal\commerce_google_tag_manager\EventTrackerService::buildProductFromProductVariation()
Build Enhanced Ecommerce product from a given commerce product variation.
Parameters
\Drupal\commerce_product\Entity\ProductVariationInterface $product_variation: A commerce product variation.
Return value
\Drupal\commerce_google_tag_manager\Product The Enhanced Ecommerce product.
1 call to EventTrackerService::buildProductFromProductVariation()
- EventTrackerService::buildProductFromOrderItem in src/
EventTrackerService.php - Build the Enhanced Ecommerce product from a given commerce order item.
File
- src/
EventTrackerService.php, line 336
Class
- EventTrackerService
- Track different events from Google's Enhanced Ecommerce.
Namespace
Drupal\commerce_google_tag_managerCode
private function buildProductFromProductVariation(ProductVariationInterface $product_variation) {
$context = new Context($this->currentUser, $this->currentStore
->getStore());
$product = new Product();
$product
->setName($product_variation
->getProduct()
->getTitle())
->setId($product_variation
->getProduct()
->id())
->setVariant($product_variation
->getTitle());
// Get price based on resolver(s).
/** @var \Drupal\commerce_price\Price $calculated_price */
$calculated_price = $this->priceCalculator
->calculate($product_variation, 1, $context)
->getCalculatedPrice();
if ($calculated_price) {
$product
->setPrice(self::formatPrice((double) $calculated_price
->getNumber()));
}
$event = new AlterProductEvent($product, $product_variation);
$this->eventDispatcher
->dispatch(EnhancedEcommerceEvents::ALTER_PRODUCT, $event);
return $product;
}