|
|
|
@ -82,30 +82,6 @@ function custom_canonical_url() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
add_action('wp_head', 'add_facebook_pixel'); |
|
|
|
|
function add_facebook_pixel() { |
|
|
|
|
?> |
|
|
|
|
<!-- Meta Pixel Code --> |
|
|
|
|
<script> |
|
|
|
|
!function(f,b,e,v,n,t,s) |
|
|
|
|
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
|
|
|
|
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; |
|
|
|
|
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; |
|
|
|
|
n.queue=[];t=b.createElement(e);t.async=!0; |
|
|
|
|
t.src=v;s=b.getElementsByTagName(e)[0]; |
|
|
|
|
s.parentNode.insertBefore(t,s)}(window, document,'script', |
|
|
|
|
'https://connect.facebook.net/en_US/fbevents.js'); |
|
|
|
|
fbq('init', '1791804684725971'); |
|
|
|
|
fbq('track', 'PageView'); |
|
|
|
|
</script> |
|
|
|
|
<noscript> |
|
|
|
|
<img height="1" width="1" style="display:none" |
|
|
|
|
src="https://www.facebook.com/tr?id=1791804684725971&ev=PageView&noscript=1"/> |
|
|
|
|
</noscript> |
|
|
|
|
<!-- End Meta Pixel Code --> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
add_action('woocommerce_thankyou', 'send_purchase_to_metrika'); |
|
|
|
|
function send_purchase_to_metrika($order_id) { |
|
|
|
|
if (!$order_id) return; // Проверка, что заказ существует |
|
|
|
@ -161,120 +137,126 @@ function send_purchase_to_metrika($order_id) { |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
add_action('wp_footer', 'add_facebook_pixel_events'); |
|
|
|
|
function add_facebook_pixel_events() { |
|
|
|
|
// 1. Событие AddToCart (Добавление в корзину) |
|
|
|
|
if (is_product() || is_shop() || is_cart()) { |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
jQuery(function($) { |
|
|
|
|
$(document.body).on('added_to_cart', function(event, fragments, cart_hash, $button) { |
|
|
|
|
var productId = $button.data('product_id') || ''; |
|
|
|
|
var quantity = $button.data('quantity') || 1; |
|
|
|
|
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(); ?>'; // Динамическая валюта
|
|
|
|
|
|
|
|
|
|
// Событие для Facebook Pixel |
|
|
|
|
fbq('track', 'AddToCart', { |
|
|
|
|
content_ids: [productId], |
|
|
|
|
content_type: 'product', |
|
|
|
|
value: parseFloat(priceElement) * quantity, |
|
|
|
|
currency: currency |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Событие для Google Analytics |
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_to_cart', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': currency, |
|
|
|
|
'value': parseFloat(priceElement) * quantity, |
|
|
|
|
'items': [{ |
|
|
|
|
'item_id': productId, |
|
|
|
|
'item_name': productName, |
|
|
|
|
'price': parseFloat(priceElement), |
|
|
|
|
'quantity': quantity |
|
|
|
|
}] |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* Добавление событий контрибуции для FP Pixel |
|
|
|
|
* только на боевом сайте АЕ |
|
|
|
|
* */ |
|
|
|
|
if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') { |
|
|
|
|
add_action('wp_footer', 'add_facebook_pixel_events'); |
|
|
|
|
function add_facebook_pixel_events() { |
|
|
|
|
// 1. Событие AddToCart (Добавление в корзину) |
|
|
|
|
if (is_product() || is_shop() || is_cart()) { |
|
|
|
|
?> |
|
|
|
|
<script> |
|
|
|
|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
|
jQuery(function($) { |
|
|
|
|
$(document.body).on('added_to_cart', function(event, fragments, cart_hash, $button) { |
|
|
|
|
var productId = $button.data('product_id') || ''; |
|
|
|
|
var quantity = $button.data('quantity') || 1; |
|
|
|
|
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(); ?>'; // Динамическая валюта
|
|
|
|
|
|
|
|
|
|
// Событие для Facebook Pixel |
|
|
|
|
fbq('track', 'AddToCart', { |
|
|
|
|
content_ids: [productId], |
|
|
|
|
content_type: 'product', |
|
|
|
|
value: parseFloat(priceElement) * quantity, |
|
|
|
|
currency: currency |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Событие для Google Analytics |
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_to_cart', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': currency, |
|
|
|
|
'value': parseFloat(priceElement) * quantity, |
|
|
|
|
'items': [{ |
|
|
|
|
'item_id': productId, |
|
|
|
|
'item_name': productName, |
|
|
|
|
'price': parseFloat(priceElement), |
|
|
|
|
'quantity': quantity |
|
|
|
|
}] |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 2. Событие Purchase (Покупка) |
|
|
|
|
if (is_wc_endpoint_url('order-received')) { |
|
|
|
|
$order_id = absint(get_query_var('order-received')); |
|
|
|
|
if (!$order_id) return; |
|
|
|
|
|
|
|
|
|
$order = wc_get_order($order_id); |
|
|
|
|
if (!$order || ($order->get_status() !== 'processing' && $order->get_status() !== 'completed')) return; |
|
|
|
|
|
|
|
|
|
$items = []; |
|
|
|
|
foreach ($order->get_items() as $item) { |
|
|
|
|
$product = $item->get_product(); |
|
|
|
|
$items[] = [ |
|
|
|
|
'item_id' => $product->get_id(), |
|
|
|
|
'item_name' => $product->get_name(), |
|
|
|
|
'price' => $product->get_price(), |
|
|
|
|
'quantity' => $item->get_quantity() |
|
|
|
|
]; |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
?> |
|
|
|
|
<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' |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
// 2. Событие Purchase (Покупка) |
|
|
|
|
if (is_wc_endpoint_url('order-received')) { |
|
|
|
|
$order_id = absint(get_query_var('order-received')); |
|
|
|
|
if (!$order_id) return; |
|
|
|
|
|
|
|
|
|
$order = wc_get_order($order_id); |
|
|
|
|
if (!$order || ($order->get_status() !== 'processing' && $order->get_status() !== 'completed')) return; |
|
|
|
|
|
|
|
|
|
$items = []; |
|
|
|
|
foreach ($order->get_items() as $item) { |
|
|
|
|
$product = $item->get_product(); |
|
|
|
|
$items[] = [ |
|
|
|
|
'item_id' => $product->get_id(), |
|
|
|
|
'item_name' => $product->get_name(), |
|
|
|
|
'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' |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 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': 'purchase', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $order->get_currency(); ?>',
|
|
|
|
|
'value': <?php echo $order->get_total(); ?>,
|
|
|
|
|
'items': <?php echo json_encode($items); ?> |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
window.dataLayer = window.dataLayer || []; |
|
|
|
|
window.dataLayer.push({ |
|
|
|
|
'event': 'add_payment_info', |
|
|
|
|
'ecommerce': { |
|
|
|
|
'currency': '<?php echo $currency; ?>',
|
|
|
|
|
'value': <?php echo $cart_total; ?> |
|
|
|
|
} |
|
|
|
|
// 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> |
|
|
|
|
<?php |
|
|
|
|
</script> |
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Отключаем кэширование для страниц товаров |
|
|
|
|
add_action('template_redirect', function() { |
|
|
|
|
if (is_product()) { |
|
|
|
|