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