You are here

class CommerceGoogleTagManagerActionAddToCart in Commerce Google Tag Manager 7.2

@file This file contains the "Send Add To Cart event to GTM" action definition.

Hierarchy

Expanded class hierarchy of CommerceGoogleTagManagerActionAddToCart

File

includes/rules/CommerceGoogleTagManagerActionAddToCart.php, line 7
This file contains the "Send Add To Cart event to GTM" action definition.

View source
class CommerceGoogleTagManagerActionAddToCart extends CommerceGoogleTagManagerBaseAction {

  /**
   * Defines the action.
   *
   * @return array
   */
  public static function getInfo() {
    return self::getDefaultsInfo() + array(
      'label' => t('Send "Add to Cart" event to google analytics via GTM'),
      'parameter' => array(
        'item' => array(
          'type' => 'commerce_line_item',
          'label' => t('Line Item in the Order'),
        ),
        'count' => array(
          'type' => 'text',
          'label' => t('The count of items added to the Cart'),
          'optional' => TRUE,
        ),
      ),
    );
  }

  /**
   * @return string
   */
  public function getCommerceEventName() {
    return 'addToCart';
  }

  /**
   * Executes the action.
   *
   * @param array $item The Line Item of the order
   * @param string $count Number of items added to the order
   */
  public function execute($item, $count) {
    $item = CommerceGoogleTagManagerHelper::getWrappedLineItem($item);
    $productData = CommerceGoogleTagManagerHelper::getLineItemData($item, $this
      ->getCommerceEventName());
    $order = $item->order;
    $currencyCode = $order->commerce_order_total->currency_code
      ->value();

    // Override the product quantity, if set:
    if ($count) {
      $productData['quantity'] = $count;
    }
    $data = array(
      'currencyCode' => $currencyCode,
      'add' => array(
        'products' => array(
          $productData,
        ),
      ),
    );
    $this
      ->pushCommerceData($data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceGoogleTagManagerActionAddToCart::execute public function Executes the action.
CommerceGoogleTagManagerActionAddToCart::getCommerceEventName public function Overrides CommerceGoogleTagManagerBaseAction::getCommerceEventName
CommerceGoogleTagManagerActionAddToCart::getInfo public static function Defines the action.
CommerceGoogleTagManagerBaseAction::getDefaultsInfo protected static function Get the default Rule's info.
CommerceGoogleTagManagerBaseAction::pushCommerceData protected function Builds and pushes the current commerce data.
RulesPluginHandlerBase::$element protected property
RulesPluginHandlerBase::access public function Implements RulesPluginImplInterface::access(). Overrides RulesPluginHandlerInterface::access
RulesPluginHandlerBase::assertions public function Returns an array of info assertions for the specified parameters. Overrides RulesPluginHandlerInterface::assertions 1
RulesPluginHandlerBase::dependencies public function Returns an array of required modules. Overrides RulesPluginHandlerInterface::dependencies
RulesPluginHandlerBase::form_alter public function Alters the generated configuration form of the element. Overrides RulesPluginHandlerInterface::form_alter
RulesPluginHandlerBase::info_alter public function Allows altering of the element's action/condition info. Overrides RulesPluginHandlerInterface::info_alter
RulesPluginHandlerBase::process public function Processes settings independent from a form submission. Overrides RulesPluginHandlerInterface::process
RulesPluginHandlerBase::validate public function Validates $settings independent from a form submission. Overrides RulesPluginHandlerInterface::validate
RulesPluginHandlerBase::__construct public function Overridden to provide $this->element to make the code more meaningful.