You are here

public function Product::toArray in Commerce Google Tag Manager 8.2

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

Build the product data as array in the requested format by Google.

Return value

array Formated Product data as requested by Google.

File

src/Product.php, line 72

Class

Product
Represents a product in the domain of Google's Enhanced Ecommerce.

Namespace

Drupal\commerce_google_tag_manager

Code

public function toArray() {
  $data = [];
  foreach ($this as $property => $value) {
    if (is_array($value)) {
      foreach ($value as $i => $v) {
        $data[rtrim($property, 's') . ($i + 1)] = $v;
      }
    }
    elseif ($value !== NULL) {
      $data[$property] = $value;
    }
  }
  return $data;
}