site_mode == 'production' && $site_env->site_region == 'ae') { // Проверка на тестовый режим для метрик // TO_DO: дуюлирует логику is_gp_test_mode в wp-content/themes/cosmopet/global-functions/multisite-functions.php 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() { if (is_gp_test_mode()) return; global $product; // 1. ViewContent if (is_product() && $product && $product->get_price() > 0) { ?> cart && WC()->cart->get_cart_contents_count() > 0) { ?> cart && WC()->cart->get_cart_contents_count() > 0) { $currency = get_woocommerce_currency(); $cart_total = WC()->cart->get_total('edit'); ?> 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() ]; } ?> 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(), 'quantity' => $item->get_quantity() ]; } $currency = $order->get_currency(); ?> site_mode === 'production' && $site_env->site_region === 'ae') { $show_pixel = true; } // Для тестовых стендов AE if ($site_env->site_mode === 'develope' && $site_env->site_region === 'ae') { $show_pixel = true; } // Дополнительные проверки $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 = [ '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); } }); function get_page_type() { if (is_home() || is_front_page()) { return 'home'; } elseif (is_shop()) { return 'shop'; } elseif (is_product_category()) { return 'category'; } elseif (is_product_tag()) { return 'tag'; } elseif (is_search()) { return 'search'; } elseif (is_cart()) { return 'cart'; } elseif (is_account_page()) { return 'account'; } else { return 'other'; } }