From a482be1ebdfabf2e1cc63959041c82a2a59cd979 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 24 Jul 2025 16:25:57 +0300 Subject: [PATCH] fix conflict --- .../global-functions/multisite-functions.php | 118 +++++- .../templates/head-pixel-functions.twig | 369 ++++++++++++++---- .../themes/cosmopet/templates/layout.twig | 18 + 3 files changed, 415 insertions(+), 90 deletions(-) diff --git a/wp-content/themes/cosmopet/global-functions/multisite-functions.php b/wp-content/themes/cosmopet/global-functions/multisite-functions.php index b5d0dc8..ff5b2a5 100644 --- a/wp-content/themes/cosmopet/global-functions/multisite-functions.php +++ b/wp-content/themes/cosmopet/global-functions/multisite-functions.php @@ -268,6 +268,8 @@ if ($site_env->site_mode == 'production' && $site_env->site_region == 'ae') { } +// Функция событий Meta Pixel + add_action('wp_head', function() { global $site_env; $show_pixel = false; @@ -282,12 +284,120 @@ add_action('wp_head', function() { $show_pixel = true; } - if ($show_pixel && !$site_env->is_gp_test_mode) { + // Дополнительные проверки + $skip_tracking = false; + + // Исключаем админов + if (current_user_can('administrator')) { + $skip_tracking = true; + } + + // Исключаем gp-test режим + if ($site_env->is_gp_test_mode) { + $skip_tracking = true; + } + + // Исключаем если есть параметр gp-test=1 в URL + if (isset($_GET['gp-test']) && $_GET['gp-test'] == '1') { + $skip_tracking = true; + } + + if ($show_pixel && !$skip_tracking) { $context = [ - 'pixel_event_type' => 'AddToCart', // или определите логику 'currency' => get_woocommerce_currency(), - // добавьте другие переменные при необходимости ]; + + // Определяем тип страницы и подготавливаем данные + if (is_product()) { + // Страница товара - ViewContent + global $product; + if ($product && $product instanceof WC_Product) { + $context['pixel_event_type'] = 'ViewContent'; + $context['product'] = [ + 'id' => $product->get_id(), + 'name' => $product->get_name(), + 'price' => floatval($product->get_price()), + 'sku' => $product->get_sku(), + 'category' => wp_get_post_terms($product->get_id(), 'product_cat', ['fields' => 'names'])[0] ?? '', + ]; + } + } + elseif (is_checkout() && !is_order_received_page()) { + // Страница чекаута - InitiateCheckout + $context['pixel_event_type'] = 'InitiateCheckout'; + if (WC()->cart && !WC()->cart->is_empty()) { + $context['cart_total'] = floatval(WC()->cart->get_total('edit')); + $context['cart_items'] = []; + $context['content_ids'] = []; + + foreach (WC()->cart->get_cart() as $cart_item) { + $product = $cart_item['data']; + if ($product && $product instanceof WC_Product) { + $item_data = [ + 'item_id' => $product->get_id(), + 'item_name' => $product->get_name(), + 'price' => floatval($product->get_price()), + 'quantity' => intval($cart_item['quantity']), + 'category' => wp_get_post_terms($product->get_id(), 'product_cat', ['fields' => 'names'])[0] ?? '', + ]; + $context['cart_items'][] = $item_data; + $context['content_ids'][] = $product->get_id(); + } + } + } + } + elseif (is_order_received_page()) { + // Страница благодарности - Purchase + $order_id = get_query_var('order-received'); + if ($order_id) { + $order = wc_get_order($order_id); + if ($order && $order instanceof WC_Order) { + $context['pixel_event_type'] = 'Purchase'; + $order_items = []; + $content_ids = []; + + foreach ($order->get_items() as $item) { + if ($item instanceof WC_Order_Item_Product) { + $product = $item->get_product(); + if ($product && $product instanceof WC_Product) { + $order_items[] = [ + 'item_id' => $product->get_id(), + 'item_name' => $product->get_name(), + 'price' => floatval($item->get_total() / $item->get_quantity()), + 'quantity' => intval($item->get_quantity()), + 'category' => wp_get_post_terms($product->get_id(), 'product_cat', ['fields' => 'names'])[0] ?? '', + ]; + $content_ids[] = $product->get_id(); + } + } + } + + $context['order'] = [ + 'id' => $order->get_id(), + 'total' => floatval($order->get_total()), + 'currency' => $order->get_currency(), + 'items' => $order_items, + 'content_ids' => $content_ids, + 'order_key' => $order->get_order_key(), + ]; + } + } + } + else { + // Все остальные страницы - PageView + подготовка для AddToCart + $context['pixel_event_type'] = 'PageView'; + $context['enable_addtocart'] = true; // Включаем обработчик AddToCart + + // Дополнительные данные для PageView + global $wp_query; + $context['page_data'] = [ + 'page_title' => wp_get_document_title(), + 'page_url' => home_url(add_query_arg(array(), $wp_query->request)), + 'page_type' => get_page_type(), + ]; + } + + // Рендерим шаблон \Timber\Timber::render('templates/head-pixel-functions.twig', $context); } -}); +}); \ No newline at end of file diff --git a/wp-content/themes/cosmopet/templates/head-pixel-functions.twig b/wp-content/themes/cosmopet/templates/head-pixel-functions.twig index 555752b..380e8ad 100644 --- a/wp-content/themes/cosmopet/templates/head-pixel-functions.twig +++ b/wp-content/themes/cosmopet/templates/head-pixel-functions.twig @@ -1,135 +1,332 @@ -{# header_scripts_pixel.twig #} +{# templates/head-pixel-functions.twig #} -{# Проверка переменных перед рендерингом #} -{% if pixel_event_type == 'ViewContent' and product %} +{# PageView - просмотр любой страницы #} +{% if pixel_event_type == 'PageView' %} {% endif %} -{% if pixel_event_type == 'InitiateCheckout' %} - -{% endif %} - -{% if pixel_event_type == 'AddToCart' %} +{# ViewContent - просмотр товара #} +{% if pixel_event_type == 'ViewContent' and product %} {% endif %} -{% if pixel_event_type == 'AddPaymentInfo' %} +{# InitiateCheckout - начало оформления заказа #} +{% if pixel_event_type == 'InitiateCheckout' %} {% endif %} +{# Purchase - завершение покупки #} {% if pixel_event_type == 'Purchase' and order %} +{% endif %} +{# AddToCart - добавление в корзину (работает на всех страницах где включен) #} +{% if enable_addtocart or pixel_event_type == 'PageView' %} -{% endif %} +{% endif %} \ No newline at end of file diff --git a/wp-content/themes/cosmopet/templates/layout.twig b/wp-content/themes/cosmopet/templates/layout.twig index 1744fbc..de210ec 100644 --- a/wp-content/themes/cosmopet/templates/layout.twig +++ b/wp-content/themes/cosmopet/templates/layout.twig @@ -10,6 +10,24 @@ {% if site_mode == 'production' %} {{ header_scripts }} {% endif %} + + + + +