wp-google-analytics-utils.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Jetpack_Google_Analytics_Options provides a single interface to module options
  4. *
  5. * @author allendav
  6. */
  7. /**
  8. * Bail if accessed directly
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. class Jetpack_Google_Analytics_Utils {
  14. /**
  15. * Gets product categories or varation attributes as a formatted concatenated string
  16. * @param WC_Product
  17. * @return string
  18. */
  19. public static function get_product_categories_concatenated( $product ) {
  20. if ( ! class_exists( 'WooCommerce' ) ) {
  21. return '';
  22. }
  23. if ( ! $product ) {
  24. return '';
  25. }
  26. $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
  27. if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
  28. $line = wc_get_formatted_variation( $variation_data, true );
  29. } else {
  30. $out = array();
  31. $categories = get_the_terms( $product->get_id(), 'product_cat' );
  32. if ( $categories ) {
  33. foreach ( $categories as $category ) {
  34. $out[] = $category->name;
  35. }
  36. }
  37. $line = join( "/", $out );
  38. }
  39. return $line;
  40. }
  41. /**
  42. * Gets a product's SKU with fallback to just ID. IDs are prepended with a hash symbol.
  43. * @param WC_Product
  44. * @return string
  45. */
  46. public static function get_product_sku_or_id( $product ) {
  47. if ( ! class_exists( 'WooCommerce' ) ) {
  48. return '';
  49. }
  50. if ( ! $product ) {
  51. return '';
  52. }
  53. return $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
  54. }
  55. }