FacebookCommerce.php in Facebook Pixel 8
File
modules/facebook_pixel_commerce/src/FacebookCommerce.php
View source
<?php
namespace Drupal\facebook_pixel_commerce;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_price\RounderInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
class FacebookCommerce implements FacebookCommerceInterface {
protected $rounder;
public function __construct(RounderInterface $rounder) {
$this->rounder = $rounder;
}
public function getOrderData(OrderInterface $order) {
$contents = [];
$content_ids = [];
$data = [
'value' => $this->rounder
->round($order
->getTotalPrice())
->getNumber(),
'currency' => $order
->getTotalPrice()
->getCurrencyCode(),
'num_items' => count($order
->getItems()),
'content_name' => 'order',
'content_type' => 'product',
];
foreach ($order
->getItems() as $order_item) {
$item_data = $this
->getOrderItemData($order_item);
if (!empty($item_data['contents'][0])) {
$contents[] = $item_data['contents'][0];
$content_ids[] = $item_data['contents'][0]['id'];
}
}
if (!empty($contents)) {
$data['contents'] = $contents;
$data['content_ids'] = $content_ids;
}
return $data;
}
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(),
],
],
];
if ($entity instanceof ProductVariationInterface) {
$data['content_ids'] = [
$entity
->getSku(),
];
$data['content_name'] = $entity
->getTitle();
$data['contents'][0]['id'] = $entity
->getSku();
}
return $data;
}
}