You are here

class FacebookCommerce in Facebook Pixel 8

Helper methods for facebook_pixel_commerce module.

@package Drupal\facebook_pixel_commerce

Hierarchy

Expanded class hierarchy of FacebookCommerce

1 string reference to 'FacebookCommerce'
facebook_pixel_commerce.services.yml in modules/facebook_pixel_commerce/facebook_pixel_commerce.services.yml
modules/facebook_pixel_commerce/facebook_pixel_commerce.services.yml
1 service uses FacebookCommerce
facebook_pixel_commerce.facebook_commerce in modules/facebook_pixel_commerce/facebook_pixel_commerce.services.yml
Drupal\facebook_pixel_commerce\FacebookCommerce

File

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

Namespace

Drupal\facebook_pixel_commerce
View source
class FacebookCommerce implements FacebookCommerceInterface {

  /**
   * The rounder service.
   *
   * @var \Drupal\commerce_price\RounderInterface
   */
  protected $rounder;

  /**
   * FacebookCommerce constructor.
   *
   * @param \Drupal\commerce_price\RounderInterface $rounder
   *   The price rounder.
   */
  public function __construct(RounderInterface $rounder) {
    $this->rounder = $rounder;
  }

  /**
   * Build the Facebook object for orders.
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order object.
   *
   * @return array
   *   The data array for an order.
   */
  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;
  }

  /**
   * Build the Facebook object for order items.
   *
   * @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item
   *   The order item object.
   *
   * @return array
   *   The data array for an order item.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacebookCommerce::$rounder protected property The rounder service.
FacebookCommerce::getOrderData public function Build the Facebook object for orders. Overrides FacebookCommerceInterface::getOrderData
FacebookCommerce::getOrderItemData public function Build the Facebook object for order items. Overrides FacebookCommerceInterface::getOrderItemData
FacebookCommerce::__construct public function FacebookCommerce constructor.