You are here

class CartSubscriber in Facebook Pixel 8

Cart subscriber.

Hierarchy

  • class \Drupal\facebook_pixel_commerce\EventSubscriber\CartSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CartSubscriber

1 string reference to 'CartSubscriber'
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 CartSubscriber
facebook_pixel_commerce.cart_subscriber in modules/facebook_pixel_commerce/facebook_pixel_commerce.services.yml
Drupal\facebook_pixel_commerce\EventSubscriber\CartSubscriber

File

modules/facebook_pixel_commerce/src/EventSubscriber/CartSubscriber.php, line 16

Namespace

Drupal\facebook_pixel_commerce\EventSubscriber
View source
class CartSubscriber implements EventSubscriberInterface {

  /**
   * The cart provider.
   *
   * @var \Drupal\commerce_cart\CartProviderInterface
   */
  protected $cartProvider;

  /**
   * The facebook event service.
   *
   * @var \Drupal\facebook_pixel\FacebookEventInterface
   */
  protected $facebookEvent;

  /**
   * The Facebook Commerce service.
   *
   * @var \Drupal\facebook_pixel_commerce\FacebookCommerce
   */
  protected $facebookCommerce;

  /**
   * Constructs a new OrderEventSubscriber object.
   *
   * @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
   *   The cart provider.
   * @param \Drupal\facebook_pixel\FacebookEventInterface $facebook_event
   *   The Facebook Event service.
   * @param \Drupal\facebook_pixel_commerce\FacebookCommerceInterface $facebook_commerce
   *   The Facebook Commerce service.
   */
  public function __construct(CartProviderInterface $cart_provider, FacebookEventInterface $facebook_event, FacebookCommerceInterface $facebook_commerce) {
    $this->cartProvider = $cart_provider;
    $this->facebookEvent = $facebook_event;
    $this->facebookCommerce = $facebook_commerce;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      CartEvents::CART_ENTITY_ADD => 'addToCart',
      'commerce_order.place.pre_transition' => 'finalizeCart',
    ];
  }

  /**
   * Add to cart event.
   *
   * @param \Drupal\commerce_cart\Event\CartEntityAddEvent $event
   *   The cart entity add event.
   */
  public function addToCart(CartEntityAddEvent $event) {
    $data = $this->facebookCommerce
      ->getOrderItemData($event
      ->getOrderItem());

    // Trigger the AddToCart event and force a session to be used.
    $this->facebookEvent
      ->addEvent('AddToCart', $data, TRUE);
  }

  /**
   * Finalize cart event.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The workflow transition event.
   */
  public function finalizeCart(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    $data = $this->facebookCommerce
      ->getOrderData($order);
    $this->facebookEvent
      ->addEvent('Purchase', $data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CartSubscriber::$cartProvider protected property The cart provider.
CartSubscriber::$facebookCommerce protected property The Facebook Commerce service.
CartSubscriber::$facebookEvent protected property The facebook event service.
CartSubscriber::addToCart public function Add to cart event.
CartSubscriber::finalizeCart public function Finalize cart event.
CartSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CartSubscriber::__construct public function Constructs a new OrderEventSubscriber object.