Completed
Push — add/8181-enhanced-google-analy... ( fd7aed...e5de7b )
by
unknown
08:24
created

Jetpack_Google_Analytics_Utils   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B get_product_categories_concatenated() 0 20 7
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
		$variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
28
		if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
29
			$line = wc_get_formatted_variation( $variation_data, true );
30
		} else {
31
			$out = array();
32
			$categories = get_the_terms( $product->get_id(), 'product_cat' );
33
			if ( $categories ) {
34
				foreach ( $categories as $category ) {
35
					$out[] = $category->name;
36
				}
37
			}
38
			$line = join( "/", $out );
39
		}
40
		return $line;
41
	}
42
}