|
|
|
@ -75,15 +75,58 @@ function custom_canonical_url() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Добавление событий контрибуции для FP Pixel |
|
|
|
|
* только на боевом сайте АЕ |
|
|
|
|
* */ |
|
|
|
|
if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
/* Передача просто по url */ |
|
|
|
|
* Добавление событий контрибуции для FP Pixel |
|
|
|
|
* только на боевом сайте АЕ |
|
|
|
|
*/ |
|
|
|
|
if ($site_env->site_mode == 'production' && $site_env->site_region == 'ae') { |
|
|
|
|
|
|
|
|
|
// Проверка на тестовый режим для метрик |
|
|
|
|
function is_gp_test_mode() { |
|
|
|
|
if (isset($_GET['gp-test']) && $_GET['gp-test'] == '1') { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
if (is_user_logged_in() && current_user_can('administrator')) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
add_action('wp_footer', 'add_facebook_pixel_events'); |
|
|
|
|
function add_facebook_pixel_events() { |
|
|
|
|
// 1. Событие AddToCart (Добавление в корзину) |
|
|
|
|
if (is_gp_test_mode()) return; |
|
|
|
|
|
|
|
|
|
global $product; |
|
|
|
|
|
|
|
|
|
// 1. ViewContent |
|
|
|
|
if (is_product() && $product && $product->get_price() > 0) { |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'ViewContent', { |
|
|
|
|
content_ids: ['<?php echo $product->get_id(); ?>'],
|
|
|
|
|
content_type: 'product', |
|
|
|
|
value: <?php echo $product->get_price(); ?>,
|
|
|
|
|
currency: '<?php echo get_woocommerce_currency(); ?>'
|
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 2. InitiateCheckout |
|
|
|
|
if (is_checkout() && !is_wc_endpoint_url('order-received') && WC()->cart && WC()->cart->get_cart_contents_count() > 0) { |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'InitiateCheckout'); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3. AddToCart |
|
|
|
|
if (is_product() || is_shop() || is_cart()) { |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
@ -95,9 +138,8 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
var productName = $button.data('product_sku') || |
|
|
|
|
$button.closest('.product').find('.woocommerce-loop-product__title').text().trim() || 'Unknown'; |
|
|
|
|
var priceElement = $button.closest('.product').find('.price .amount').text().replace(/[^0-9.]/g, '') || '0.00'; |
|
|
|
|
var currency = '<?php echo get_woocommerce_currency(); ?>'; // Динамическая валюта
|
|
|
|
|
var currency = '<?php echo get_woocommerce_currency(); ?>';
|
|
|
|
|
|
|
|
|
|
// Событие для Facebook Pixel |
|
|
|
|
fbq('track', 'AddToCart', { |
|
|
|
|
content_ids: [productId], |
|
|
|
|
content_type: 'product', |
|
|
|
@ -105,7 +147,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
currency: currency |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Событие для Google Analytics |
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_to_cart', |
|
|
|
@ -124,9 +165,35 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<? } |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 2. Событие Purchase (Покупка) |
|
|
|
|
// 4. AddPaymentInfo |
|
|
|
|
if (is_checkout() && !is_wc_endpoint_url('order-received') && WC()->cart && WC()->cart->get_cart_contents_count() > 0) { |
|
|
|
|
$currency = get_woocommerce_currency(); |
|
|
|
|
$cart_total = WC()->cart->get_total('edit'); |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'AddPaymentInfo', { |
|
|
|
|
value: <?php echo $cart_total; ?>,
|
|
|
|
|
currency: '<?php echo $currency; ?>'
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_payment_info', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $currency; ?>',
|
|
|
|
|
'value': <?php echo $cart_total; ?> |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 5. Purchase |
|
|
|
|
if (is_wc_endpoint_url('order-received')) { |
|
|
|
|
$order_id = absint(get_query_var('order-received')); |
|
|
|
|
if (!$order_id) return; |
|
|
|
@ -138,84 +205,57 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
foreach ($order->get_items() as $item) { |
|
|
|
|
$product = $item->get_product(); |
|
|
|
|
$items[] = [ |
|
|
|
|
'item_id' => $product->get_id(), |
|
|
|
|
'item_id' => $product->get_id(), |
|
|
|
|
'item_name' => $product->get_name(), |
|
|
|
|
'price' => $product->get_price(), |
|
|
|
|
'price' => $product->get_price(), |
|
|
|
|
'quantity' => $item->get_quantity() |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'Purchase', { |
|
|
|
|
value: <?php echo $order->get_total(); ?>,
|
|
|
|
|
currency: '<?php echo $order->get_currency(); ?>',
|
|
|
|
|
content_ids: [<?php echo implode(',', array_column($items, 'item_id')); ?>],
|
|
|
|
|
content_type: 'product' |
|
|
|
|
}); |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'Purchase', { |
|
|
|
|
value: <?php echo $order->get_total(); ?>,
|
|
|
|
|
currency: '<?php echo $order->get_currency(); ?>',
|
|
|
|
|
content_ids: [<?php echo implode(',', array_column($items, 'item_id')); ?>],
|
|
|
|
|
content_type: 'product' |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'purchase', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $order->get_currency(); ?>',
|
|
|
|
|
'value': <?php echo $order->get_total(); ?>,
|
|
|
|
|
'items': <?php echo json_encode($items); ?> |
|
|
|
|
} |
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'purchase', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $order->get_currency(); ?>',
|
|
|
|
|
'value': <?php echo $order->get_total(); ?>,
|
|
|
|
|
'items': <?php echo json_encode($items); ?> |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3. Событие AddPaymentInfo |
|
|
|
|
if (is_checkout() && !is_wc_endpoint_url('order-received')) { |
|
|
|
|
$currency = get_woocommerce_currency(); |
|
|
|
|
$cart_total = WC()->cart ? WC()->cart->get_total('edit') : 0; |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
fbq('track', 'AddPaymentInfo', { |
|
|
|
|
value: <?php echo $cart_total; ?>,
|
|
|
|
|
currency: '<?php echo $currency; ?>'
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_payment_info', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $currency; ?>',
|
|
|
|
|
'value': <?php echo $cart_total; ?> |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<? } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Передача контрибуции со страницы "Спасибо" */ |
|
|
|
|
add_action('woocommerce_thankyou', 'send_purchase_to_metrika'); |
|
|
|
|
function send_purchase_to_metrika($order_id) |
|
|
|
|
{ |
|
|
|
|
if (!$order_id) |
|
|
|
|
return; // Проверка, что заказ существует |
|
|
|
|
function send_purchase_to_metrika($order_id) { |
|
|
|
|
if (is_gp_test_mode()) return; |
|
|
|
|
|
|
|
|
|
if (!$order_id) return; |
|
|
|
|
|
|
|
|
|
$order = wc_get_order($order_id); |
|
|
|
|
if ($order->get_status() !== 'processing' && $order->get_status() !== 'completed') |
|
|
|
|
return; // Отправляем только для оплаченных заказов |
|
|
|
|
if ($order->get_status() !== 'processing' && $order->get_status() !== 'completed') return; |
|
|
|
|
|
|
|
|
|
$items = []; |
|
|
|
|
foreach ($order->get_items() as $item) { |
|
|
|
|
$product = $item->get_product(); |
|
|
|
|
$items[] = [ |
|
|
|
|
'id' => $product->get_id(), |
|
|
|
|
'name' => $product->get_name(), |
|
|
|
|
'price' => $product->get_price(), |
|
|
|
|
'id' => $product->get_id(), |
|
|
|
|
'name' => $product->get_name(), |
|
|
|
|
'price' => $product->get_price(), |
|
|
|
|
'quantity' => $item->get_quantity() |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Получаем валюту заказа |
|
|
|
|
$currency = $order->get_currency(); |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
@ -233,7 +273,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Яндекс.Метрика |
|
|
|
|
yaCounter96481053.reachGoal('purchase', { |
|
|
|
|
'order_id': '<?php echo $order_id; ?>',
|
|
|
|
|
'order_price': '<?php echo $order->get_total(); ?>',
|
|
|
|
@ -241,7 +280,6 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
'items': <?php echo json_encode($items); ?> |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Facebook Pixel |
|
|
|
|
fbq('track', 'Purchase', { |
|
|
|
|
value: <?php echo $order->get_total(); ?>,
|
|
|
|
|
currency: '<?php echo $currency; ?>',
|
|
|
|
@ -253,6 +291,7 @@ if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TO_DO: удалить, если не нужно |
|
|
|
|
// Отключаем кэширование для страниц товаров |
|
|
|
|
add_action('template_redirect', function() { |
|
|
|
|