PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Ecommerce / EasyDigitalDownloads.php
matomo / classes / WpMatomo / Ecommerce Last commit date
Base.php 6 years ago EasyDigitalDownloads.php 6 years ago MemberPress.php 6 years ago Woocommerce.php 6 years ago
EasyDigitalDownloads.php
227 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Ecommerce;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // if accessed directly
14 }
15
16 class EasyDigitalDownloads extends Base {
17
18 public function register_hooks() {
19 if ( ! is_admin() ) {
20 add_action( 'template_redirect', array( $this, 'on_product_view' ), 99999, 0 );
21 }
22
23 parent::register_hooks();
24
25 // these actions may be triggered in admin when ajax is used
26 add_action( 'edd_payment_receipt_after_table', array( $this, 'on_order' ), 99999, 2 );
27 add_action( 'edd_post_remove_from_cart', array( $this, 'on_cart_update' ), 99999, 0 );
28 add_action( 'edd_post_add_to_cart', array( $this, 'on_cart_update' ), 99999, 0 );
29 add_action( 'edd_cart_discounts_removed', array( $this, 'on_cart_update' ), 99999, 0 );
30 add_action( 'edd_after_set_cart_item_quantity', array( $this, 'on_cart_update' ), 99999, 0 );
31 add_action( 'edd_cart_discount_set', array( $this, 'on_cart_update' ), 99999, 0 );
32 }
33
34 public function on_cart_update() {
35 if ( ! function_exists( 'EDD' )
36 || ! class_exists( '\EDD_Download' ) ) {
37 return;
38 }
39
40 $cart = EDD()->cart;
41
42 $contents = $cart->get_contents();
43
44 $tracking_code = '';
45 foreach ( $contents as $key => $item ) {
46 $download = new \EDD_Download( $item['id'] );
47
48 // If the item is not a download or it's status has changed since it was added to the cart.
49 if ( empty( $download->ID ) || ! $download->can_purchase() ) {
50 unset( $cart[ $key ] );
51 }
52
53 $name = $download->get_name();
54
55 $price_id = edd_get_cart_item_price_id( $item );
56 $price = $download->get_price();
57
58 if ( isset( $price_id ) ) {
59 // variation
60 $name .= ' - ' . edd_get_price_option_name( $item['id'], $price_id );
61 $price = edd_get_price_option_amount( $download->ID, $price_id );
62 }
63 $sku = $this->get_sku( $download, $item['id'] );
64 $categories = $this->get_product_categories( $download->ID );
65 $quantity = isset( $item['quantity'] ) ? $item['quantity'] : 0;
66
67 $params = array( 'addEcommerceItem', $sku, $name, $categories, $price, $quantity );
68 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
69 }
70
71 $total = 0;
72 if ( ! empty( $cart->get_total_fees() ) ) {
73 $total = $cart->get_total_fees();
74 } elseif ( ! empty( $cart->get_total() ) ) {
75 $total = $cart->get_total();
76 }
77
78 $tracking_code .= $this->make_matomo_js_tracker_call( array( 'trackEcommerceCartUpdate', $total ) );
79
80 // we can't echo directly as we wouldn't know where in the template rendering stage we are and whether
81 // we're supposed to print or not etc
82 $this->cart_update_queue = $this->wrap_script( $tracking_code );
83 $this->logger->log( 'Tracked ecommerce cart update: ' );
84 }
85
86 private function get_product_categories( $download_id ) {
87 $categories = (array) get_the_terms( $download_id, 'download_category' );
88
89 return array_values( array_filter( wp_list_pluck( $categories, 'name' ) ) );
90 }
91
92 /**
93 * @param \EDD_Download $download
94 *
95 * @return mixed
96 */
97 private function get_sku( $download, $download_id ) {
98 $sku = $download->get_sku();
99 if ( ! edd_use_skus() || empty( $sku ) || '-' === $sku ) {
100 $sku = $download_id;
101 }
102
103 return '' . $sku;
104 }
105
106 public function on_product_view() {
107 if ( ! is_singular( 'download' ) ) {
108 return;
109 }
110
111 $download_id = get_the_ID();
112
113 if ( empty( $download_id ) ) {
114 return;
115 }
116
117 if ( ! class_exists( '\EDD_Download' ) ) {
118 return;
119 }
120
121 $download = new \EDD_Download( $download_id );
122
123 $sku = $this->get_sku( $download, $download_id );
124
125 $params = array(
126 'setEcommerceView',
127 $sku,
128 $download->get_name(),
129 $this->get_product_categories( $download_id ),
130 $download->get_price(),
131 );
132
133 echo $this->wrap_script( $this->make_matomo_js_tracker_call( $params ) );
134 }
135
136 public function on_order( $payment, $edd_receipt_args ) {
137 if ( $edd_receipt_args['payment_id'] ) {
138 if ( 'publish' !== $payment->post_status
139 && 'complete' !== $payment->post_status
140 && 'edd_subscription' !== $payment->post_status ) {
141 return;
142 }
143 // Use a meta value so we only send the beacon once.
144 if ( $this->has_order_been_tracked_already( $payment->ID ) ) {
145 return;
146 }
147
148 if ( ! class_exists( '\EDD_Download' ) ) {
149 return;
150 }
151
152 if ( ! function_exists( 'edd_get_payment_meta_cart_details' ) ) {
153 return;
154 }
155
156 $this->set_order_been_tracked( $payment->ID );
157
158 if ( function_exists( 'edd_get_payment_number' ) ) {
159 $order_id_to_track = edd_get_payment_number( $payment->ID );
160 } else {
161 $order_id_to_track = $payment->ID;
162 }
163
164 $tracking_code = '';
165
166 if ( ! empty( $edd_receipt_args['products'] ) ) {
167 $cart = edd_get_payment_meta_cart_details( $payment->ID, true );
168 if ( $cart ) {
169 foreach ( $cart as $key => $item ) {
170 if ( empty( $item['in_bundle'] ) ) {
171 $price_id = edd_get_cart_item_price_id( $item );
172 $name = $item['name'];
173 if ( isset( $price_id ) ) {
174 // variation
175 $name .= ' - ' . edd_get_price_option_name( $item['id'], $price_id );
176 }
177
178 $download = new \EDD_Download( $item['id'] );
179 $sku = $this->get_sku( $download, $item['id'] );
180
181 $price = 0;
182 if ( isset( $item['item_price'] ) && is_numeric( $item['item_price'] ) ) {
183 $price = $item['item_price'];
184 }
185
186 $params = array(
187 'addEcommerceItem',
188 $sku,
189 $name,
190 $this->get_product_categories( $item['id'] ),
191 $price,
192 $item['quantity'],
193 );
194 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
195 }
196 }
197 }
198 }
199
200 $grand_total = edd_get_payment_amount( $payment->ID );
201
202 $payment_meta = edd_get_payment_meta( $payment->ID );
203 $discount = 0;
204 if ( ! empty( $payment_meta['user_info']['discount'] )
205 && 'none' !== $payment_meta['user_info']['discount'] ) {
206 $discount = $payment_meta['user_info']['discount'];
207 $discount = explode( ',', $discount );
208 $discount = reset( $discount );
209 }
210
211 $params = array(
212 'trackEcommerceOrder',
213 '' . $order_id_to_track,
214 $grand_total ? $grand_total : 0,
215 edd_payment_subtotal( $payment->ID ),
216 edd_use_taxes() ? edd_get_payment_tax( $payment->ID, $payment_meta ) : '0',
217 $shipping = 0,
218 $discount,
219 );
220 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
221
222 echo $this->wrap_script( $tracking_code );
223 }
224 }
225
226 }
227