You are here

public static function EventTrackerService::formatPrice in Commerce Google Tag Manager 8.2

Same name and namespace in other branches
  1. 8 src/EventTrackerService.php \Drupal\commerce_google_tag_manager\EventTrackerService::formatPrice()

Format the given price into a compliant Google's Enhanced Ecommerce.

The given price will be truncate to contain only 2 decimals. No round up are operate, so 11,999 will become 11,99.

Parameters

float $price: The price to format.

Return value

string The formatted price.

4 calls to EventTrackerService::formatPrice()
EventTrackerService::buildProductFromOrderItem in src/EventTrackerService.php
Build the Enhanced Ecommerce product from a given commerce order item.
EventTrackerService::buildProductFromProductVariation in src/EventTrackerService.php
Build Enhanced Ecommerce product from a given commerce product variation.
EventTrackerService::purchase in src/EventTrackerService.php
Track a purchase of the given order entity.
FormatPriceTest::testFormatPrice in tests/src/Unit/FormatPriceTest.php
@covers ::formatPrice

File

src/EventTrackerService.php, line 405

Class

EventTrackerService
Track different events from Google's Enhanced Ecommerce.

Namespace

Drupal\commerce_google_tag_manager

Code

public static function formatPrice($price) {
  if ($price == 0) {
    return '0';
  }

  // Truncate decimals without rounding.
  $number = bcdiv((double) $price, 1, 2);

  // Format the number as requested by Google's Enhanced Ecommerce.
  return number_format($number, 2, '.', '');
}