@@ -69,8 +69,10 @@ private function init( $init ) {
6969 */
7070 add_action ( 'woocommerce_before_add_to_cart_quantity ' , [ $ this , 'add_cart_form_hidden_input ' ] );
7171 add_action ( 'woocommerce_after_add_to_cart_form ' , [ $ this , 'track_add_to_cart_on_product_page ' ] );
72- add_filter ( 'woocommerce_store_api_validate_add_to_cart ' , [ $ this , 'track_add_to_cart ' ], 10 , 2 );
73- add_filter ( 'woocommerce_ajax_added_to_cart ' , [ $ this , 'track_ajax_add_to_cart ' ] );
72+ add_action ( 'woocommerce_store_api_validate_add_to_cart ' , [ $ this , 'track_add_to_cart ' ], 10 , 2 );
73+ add_action ( 'woocommerce_ajax_added_to_cart ' , [ $ this , 'track_ajax_add_to_cart ' ] );
74+ /** @see \WC_Form_Handler::add_to_cart_action() runs on priority 20. */
75+ add_action ( 'wp_loaded ' , [ $ this , 'track_direct_add_to_cart ' ], 21 );
7476 add_action ( 'woocommerce_remove_cart_item ' , [ $ this , 'track_remove_cart_item ' ], 10 , 2 );
7577 add_action ( 'wp_head ' , [ $ this , 'track_entered_checkout ' ] );
7678 add_action ( 'woocommerce_thankyou ' , [ $ this , 'track_purchase ' ] );
@@ -173,22 +175,22 @@ public function track_add_to_cart_on_product_page() {
173175 }
174176
175177 /**
176- * Track (non-Interactivity API, i.e. AJAX) add to cart events.
177- *
178- * @param string|int $product_id ID of the product added to the cart.
178+ * Track add to cart actions by direct link, e.g. ?product_type=download&add-to-cart=1&quantity=1
179179 *
180180 * @return void
181181 *
182- * @codeCoverageIgnore Because we can't test XHR requests here.
182+ * @codeCoverageIgnore Because we can't test XHR here.
183183 */
184- public function track_ajax_add_to_cart ( $ product_id ) {
185- $ product = wc_get_product ( $ product_id );
186- $ add_to_cart_data = [
187- 'id ' => $ product_id ,
188- 'quantity ' => $ _POST [ 'quantity ' ] ?? 1 ,
189- ];
184+ public function track_direct_add_to_cart () {
185+ if ( ! isset ( $ _REQUEST [ 'add-to-cart ' ] ) || ! is_numeric ( wp_unslash ( $ _REQUEST [ 'add-to-cart ' ] ) ) ) {
186+ return ;
187+ }
190188
191- $ this ->track_add_to_cart ( $ product , $ add_to_cart_data );
189+ $ product_id = absint ( wp_unslash ( $ _REQUEST [ 'add-to-cart ' ] ) );
190+ $ product = wc_get_product ( $ product_id );
191+ $ quantity = isset ( $ _REQUEST [ 'quantity ' ] ) ? absint ( wp_unslash ( $ _REQUEST [ 'quantity ' ] ) ) : 1 ;
192+
193+ $ this ->track_add_to_cart ( $ product , [ 'id ' => $ product_id , 'quantity ' => $ quantity ] );
192194 }
193195
194196 /**
@@ -241,6 +243,25 @@ private function clean_data( $product ) {
241243 return $ product ;
242244 }
243245
246+ /**
247+ * Track (non-Interactivity API, i.e. AJAX) add to cart events.
248+ *
249+ * @param string|int $product_id ID of the product added to the cart.
250+ *
251+ * @return void
252+ *
253+ * @codeCoverageIgnore Because we can't test XHR requests here.
254+ */
255+ public function track_ajax_add_to_cart ( $ product_id ) {
256+ $ product = wc_get_product ( $ product_id );
257+ $ add_to_cart_data = [
258+ 'id ' => $ product_id ,
259+ 'quantity ' => $ _POST [ 'quantity ' ] ?? 1 ,
260+ ];
261+
262+ $ this ->track_add_to_cart ( $ product , $ add_to_cart_data );
263+ }
264+
244265 /**
245266 * Track Remove from cart events.
246267 *
0 commit comments