Task 6591 | fix: Макс пофиксил проверку вывода скриптов, чтоб они в тестовом режиме не выводились, так же почистил лишний код

dev_10_fixbugs
parent 287dc111c7
commit e1542909fa
  1. 234
      wp-content/themes/cosmopet/global-functions/multisite-functions.php
  2. 44
      wp-content/themes/cosmopet/temp-functions/blog-logic.php
  3. 20
      wp-content/themes/cosmopet/temp-functions/filters-logic.php
  4. 4
      wp-content/themes/cosmopet/templates/layout.twig

@ -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'); add_action('woocommerce_thankyou', 'send_purchase_to_metrika');
function send_purchase_to_metrika($order_id) { function send_purchase_to_metrika($order_id) {
if (!$order_id) return; // Проверка, что заказ существует if (!$order_id) return; // Проверка, что заказ существует
@ -161,120 +137,126 @@ function send_purchase_to_metrika($order_id) {
<?php <?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 || []; * Добавление событий контрибуции для FP Pixel
window.dataLayer.push({ * только на боевом сайте АЕ
'event': 'add_to_cart', * */
'ecommerce': { if($site_env->site_mode == 'production' and $site_env->site_region == 'ae') {
'currency': currency, add_action('wp_footer', 'add_facebook_pixel_events');
'value': parseFloat(priceElement) * quantity, function add_facebook_pixel_events() {
'items': [{ // 1. Событие AddToCart (Добавление в корзину)
'item_id': productId, if (is_product() || is_shop() || is_cart()) {
'item_name': productName, ?>
'price': parseFloat(priceElement), <script>
'quantity': quantity 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>
</script> <?php
<?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'
});
window.dataLayer = window.dataLayer || []; // 2. Событие Purchase (Покупка)
window.dataLayer.push({ if (is_wc_endpoint_url('order-received')) {
'event': 'purchase', $order_id = absint(get_query_var('order-received'));
'ecommerce': { if (!$order_id) return;
'currency': '<?php echo $order->get_currency(); ?>',
'value': <?php echo $order->get_total(); ?>, $order = wc_get_order($order_id);
'items': <?php echo json_encode($items); ?> if (!$order || ($order->get_status() !== 'processing' && $order->get_status() !== 'completed')) return;
}
}); $items = [];
}); foreach ($order->get_items() as $item) {
</script> $product = $item->get_product();
<?php $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 window.dataLayer = window.dataLayer || [];
if (is_checkout() && !is_wc_endpoint_url('order-received')) { window.dataLayer.push({
$currency = get_woocommerce_currency(); 'event': 'purchase',
$cart_total = WC()->cart ? WC()->cart->get_total('edit') : 0; 'ecommerce': {
?> 'currency': '<?php echo $order->get_currency(); ?>',
<script> 'value': <?php echo $order->get_total(); ?>,
document.addEventListener('DOMContentLoaded', function() { 'items': <?php echo json_encode($items); ?>
fbq('track', 'AddPaymentInfo', { }
value: <?php echo $cart_total; ?>, });
currency: '<?php echo $currency; ?>'
}); });
</script>
<?php
}
window.dataLayer = window.dataLayer || []; // 3. Событие AddPaymentInfo
window.dataLayer.push({ if (is_checkout() && !is_wc_endpoint_url('order-received')) {
'event': 'add_payment_info', $currency = get_woocommerce_currency();
'ecommerce': { $cart_total = WC()->cart ? WC()->cart->get_total('edit') : 0;
'currency': '<?php echo $currency; ?>', ?>
'value': <?php echo $cart_total; ?> <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>
</script> <?php
<?php }
} }
} }
// Отключаем кэширование для страниц товаров // Отключаем кэширование для страниц товаров
add_action('template_redirect', function() { add_action('template_redirect', function() {
if (is_product()) { if (is_product()) {

@ -142,32 +142,32 @@ function get_comment_likes_count($comment_id) {
} }
function is_user_liked_post($post_id) { function is_user_liked_post($post_id) {
if (!is_user_logged_in()) { if (!is_user_logged_in()) {
return false; return false;
} }
global $wpdb; global $wpdb;
$table_name = $wpdb->prefix . 'cosmopet_likes'; $table_name = $wpdb->prefix . 'cosmopet_likes';
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$result = $wpdb->get_var($wpdb->prepare( $result = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND user_id = %d", "SELECT COUNT(*) FROM $table_name WHERE post_id = %d AND user_id = %d",
$post_id, $user_id $post_id, $user_id
)); ));
return $result > 0; return $result > 0;
} }
function is_user_liked_comment($comment_id) { function is_user_liked_comment($comment_id) {
if (!is_user_logged_in()) { if (!is_user_logged_in()) {
return false; return false;
} }
global $wpdb; global $wpdb;
$table_name = $wpdb->prefix . 'cosmopet_likes'; $table_name = $wpdb->prefix . 'cosmopet_likes';
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$result = $wpdb->get_var($wpdb->prepare( $result = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE comment_id = %d AND user_id = %d", "SELECT COUNT(*) FROM $table_name WHERE comment_id = %d AND user_id = %d",
$comment_id, $user_id $comment_id, $user_id
)); ));
return $result > 0; return $result > 0;
} }

@ -9,14 +9,14 @@ register_sidebar( array(
'after_title' => '</h3>', 'after_title' => '</h3>',
) ); ) );
// Для кнопки "Применить фильтр" // // Для кнопки "Применить фильтр"
add_filter('wbw_filter_submit_button_text', 'change_wbw_filter_button_text'); // add_filter('wbw_filter_submit_button_text', 'change_wbw_filter_button_text');
function change_wbw_filter_button_text($text) { // function change_wbw_filter_button_text($text) {
return 'Ваш текст'; // Например, "Фильтровать" или "Поиск" // return 'Ваш текст'; // Например, "Фильтровать" или "Поиск"
} // }
// Для кнопки сброса (если есть) // // Для кнопки сброса (если есть)
add_filter('wbw_filter_reset_button_text', 'change_wbw_reset_button_text'); // add_filter('wbw_filter_reset_button_text', 'change_wbw_reset_button_text');
function change_wbw_reset_button_text($text) { // function change_wbw_reset_button_text($text) {
return 'Сбросить'; // return 'Сбросить';
} // }

@ -7,7 +7,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
{{ header_scripts }} {% if site_mode == 'production' %}
{{ header_scripts }}
{% endif %}
</head> </head>
<body class="{{bodyClass}}"> <body class="{{bodyClass}}">

Loading…
Cancel
Save