Completed
Push — try/tracks-store-stats ( ef2a5c...c6bfe5 )
by
unknown
07:38
created

WC_Stats::get_cart_ids()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
class WC_Stats {
8
	/**
9
	 * @var Jetpack
10
	 **/
11
	private $jetpack;
12
13
	/**
14
	 * @var WC_Stats
15
	 **/
16
	private static $instance = null;
17
18
	static function init() {
19
		// Tracking only Site pages
20
		if ( is_admin() ) {
21
			return;
22
		}
23
		// Make sure Jetpack is installed and active
24
		if ( ! Jetpack::is_active() ) {
25
			return;
26
		}
27
		// Make sure WooCommerce is installed and active
28
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
29
			return;
30
		}
31
		if ( is_null( self::$instance ) ) {
32
			self::$instance = new WC_Stats();
33
		}
34
		return self::$instance;
35
	}
36
37
	public function __construct() {
38
		$this->jetpack = Jetpack::init();
39
40
		// event woocommerce_init ??
41
		add_action( 'wp_enqueue_scripts', array( $this, 'track' ) );
42
	}
43
44
	public function get_cart_ids( $item ) {
45
		return $item[ 'product_id' ];
46
	}
47
48
	public function get_cart_quantities( $item ) {
49
		return $item[ 'quantity' ];
50
	}
51
52
	public function get_funnel_step( $post_id, $post_type ) {
53
		switch ( $post_id ) {
54
			case get_option( 'woocommerce_cart_page_id' ):
55
				return 'cart_view';
56
			case get_option( 'woocommerce_checkout_page_id' ):
57
				global $wp;
58
				if ( strpos( $wp->request, 'order-received' ) !== false ) {
59
					return 'checkout_complete';
60
				}
61
				return 'checkout_view';
62
			case get_option( 'woocommerce_view_order_page_id' ):
63
				return 'view_order';
64
			default:
65
				return $post_type === 'product' ? 'product_view' : 'page_view';
66
		}
67
	}
68
69
	public function track() {
70
		// Does tracks take care of this?
71
		$user_id = get_current_user_id();
72
		$cart = WC()->cart->get_cart();
73
		$cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart );
74
		$cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart );
75
		// Is this the right id? Is WooCommerce id different?
76
		$store_id = Jetpack::get_option( 'id' );
77
		$post = get_post();
78
		$post_id = $post->ID;
79
		$post_type = get_post_type( $post_id );
80
		$post_name = $post->post_name;
81
		$funnel_step = $this->get_funnel_step( $post_id, $post_type );
82
83
		echo "<pre style=\"font-size: 12px;\">";
84
		echo "user id: ";
85
		print_r( $user_id );
86
		echo "<br/>";
87
		echo "post type: ";
88
		print_r( $post_type );
89
		echo "<br/>";
90
		echo "post name: ";
91
		print_r( $post_name );
92
		echo "<br/>";
93
		echo "post id: ";
94
		print_r( $post_id );
95
		echo "<br/>";
96
		echo "funnel step: ";
97
		print_r( $funnel_step );
98
		echo "<br/>";
99
		echo "store id: ";
100
		print_r( $store_id );
101
		echo "<br/>";
102
		echo "cart ids: ";
103
		print_r( $cart_ids );
104
		echo "<br/>";
105
		echo "cart quantities: ";
106
		print_r( $cart_quantities );
107
		if ( isset( $_GET[ 'key' ] ) ) {
108
			echo "<br/>";
109
			echo "order number: ";
110
			print_r( $_GET[ 'key' ] );
111
		}
112
		echo "</pre>";
113
	}
114
}
115
116
WC_Stats::init();
117