54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?php
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
use Sodino\Repositories\EventRepository;
|
|
use Sodino\Services\TrackingService;
|
|
|
|
function sodino_get_tracking_service() {
|
|
static $service = null;
|
|
|
|
if ($service === null) {
|
|
$eventRepository = new EventRepository();
|
|
$service = new TrackingService($eventRepository);
|
|
}
|
|
|
|
return $service;
|
|
}
|
|
|
|
add_action('template_redirect', 'sodino_track_product_view');
|
|
add_action('woocommerce_add_to_cart', 'sodino_track_add_to_cart', 10, 6);
|
|
add_action('woocommerce_before_checkout_form', 'sodino_track_checkout_start');
|
|
add_action('woocommerce_thankyou', 'sodino_track_purchase', 10, 1);
|
|
|
|
function sodino_track_product_view() {
|
|
if (is_admin() || !is_singular('product')) {
|
|
return;
|
|
}
|
|
|
|
$product_id = get_the_ID();
|
|
if (!$product_id) {
|
|
return;
|
|
}
|
|
|
|
sodino_get_tracking_service()->trackProductView($product_id);
|
|
}
|
|
|
|
function sodino_track_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
|
sodino_get_tracking_service()->trackAddToCart($product_id, $variation_id, $quantity);
|
|
}
|
|
|
|
function sodino_track_checkout_start() {
|
|
if (is_admin()) {
|
|
return;
|
|
}
|
|
|
|
sodino_get_tracking_service()->trackCheckoutStart();
|
|
}
|
|
|
|
function sodino_track_purchase($order_id) {
|
|
sodino_get_tracking_service()->trackPurchase($order_id);
|
|
}
|