You are here

protected function PageContextService::buildCommerceProductData in Simple Facebook Pixel 8

Builds commerce product view content event data.

1 call to PageContextService::buildCommerceProductData()
PageContextService::build in src/PageContextService.php
Builds events data.

File

src/PageContextService.php, line 159

Class

PageContextService
Class PageContextService.

Namespace

Drupal\simple_facebook_pixel

Code

protected function buildCommerceProductData() {
  if (!class_exists('Drupal\\commerce_product\\Entity\\Product')) {
    return;
  }
  $commerce_product = $this->request->attributes
    ->get('commerce_product');
  if ($commerce_product instanceof \Drupal\commerce_product\Entity\ProductInterface) {
    $view_content_entities = array_values($this->configFactory
      ->get('view_content_entities'));
    if (in_array('commerce_product:' . $commerce_product
      ->bundle(), $view_content_entities) && $commerce_product
      ->getDefaultVariation()) {
      $product_variation = $commerce_product
        ->getDefaultVariation();
      $context = new Context($this->currentUser, $this->currentStore
        ->getStore());
      $resolved_price = $this->chainPriceResolver
        ->resolve($product_variation, 1, $context);
      $data = [
        'content_name' => $commerce_product
          ->getTitle(),
        'content_type' => 'product',
        'content_ids' => [
          $product_variation
            ->getSku(),
        ],
        'value' => $resolved_price
          ->getNumber(),
        'currency' => $resolved_price
          ->getCurrencyCode(),
      ];
      $this->pixelBuilder
        ->addEvent('ViewContent', $data);
    }
  }
}