You are here

public function FacebookCommerce::getOrderItemData in Facebook Pixel 8

Build the Facebook object for order items.

Parameters

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item object.

Return value

array The data array for an order item.

Overrides FacebookCommerceInterface::getOrderItemData

1 call to FacebookCommerce::getOrderItemData()
FacebookCommerce::getOrderData in modules/facebook_pixel_commerce/src/FacebookCommerce.php
Build the Facebook object for orders.

File

modules/facebook_pixel_commerce/src/FacebookCommerce.php, line 80

Class

FacebookCommerce
Helper methods for facebook_pixel_commerce module.

Namespace

Drupal\facebook_pixel_commerce

Code

public function getOrderItemData(OrderItemInterface $order_item) {
  $entity = $order_item
    ->getPurchasedEntity();
  $data = [
    'value' => $this->rounder
      ->round($order_item
      ->getUnitPrice())
      ->getNumber(),
    'currency' => $order_item
      ->getTotalPrice()
      ->getCurrencyCode(),
    'order_id' => $order_item
      ->getOrderId(),
    'content_ids' => [
      $entity
        ->id(),
    ],
    'content_name' => $entity
      ->getOrderItemTitle(),
    'content_type' => 'product',
    'contents' => [
      [
        'id' => $entity
          ->id(),
        'quantity' => $order_item
          ->getQuantity(),
      ],
    ],
  ];

  // Use the SKU and title for product variations.
  if ($entity instanceof ProductVariationInterface) {
    $data['content_ids'] = [
      $entity
        ->getSku(),
    ];
    $data['content_name'] = $entity
      ->getTitle();
    $data['contents'][0]['id'] = $entity
      ->getSku();
  }
  return $data;
}