|
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 track() { |
|
53
|
|
|
$cart = WC()->cart->get_cart(); |
|
54
|
|
|
$cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart ); |
|
55
|
|
|
$cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart ); |
|
56
|
|
|
$store_id = Jetpack::get_option( 'id' ); |
|
57
|
|
|
$post_id = get_the_ID(); |
|
58
|
|
|
$post_type = get_post_type( $post_id ); |
|
59
|
|
|
|
|
60
|
|
|
echo "<pre>"; |
|
61
|
|
|
echo "post type: "; |
|
62
|
|
|
print_r( $post_type ); |
|
63
|
|
|
echo "<br/>"; |
|
64
|
|
|
echo "post id: "; |
|
65
|
|
|
print_r( $post_id ); |
|
66
|
|
|
echo "<br/>"; |
|
67
|
|
|
echo "store id: "; |
|
68
|
|
|
print_r( $store_id ); |
|
69
|
|
|
echo "<br/>"; |
|
70
|
|
|
echo "cart ids: "; |
|
71
|
|
|
print_r( $cart_ids ); |
|
72
|
|
|
echo "<br/>"; |
|
73
|
|
|
echo "cart quantities: "; |
|
74
|
|
|
print_r( $cart_quantities ); |
|
75
|
|
|
echo "</pre>"; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
WC_Stats::init(); |
|
80
|
|
|
|