You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
7.3 KiB
185 lines
7.3 KiB
<?php
|
|
|
|
add_filter('woocommerce_currency_symbol', 'change_aed_currency_symbol', 10, 2);
|
|
|
|
function change_aed_currency_symbol($currency_symbol, $currency) {
|
|
if ($currency == 'AED') {
|
|
$currency_symbol = 'AED';
|
|
}
|
|
return $currency_symbol;
|
|
}
|
|
|
|
add_filter('timber/context', function($context) {
|
|
// Передаем все нужные константы в контекст Twig
|
|
$context['CONSTANTS'] = [
|
|
'DOMAIN' => defined('SITE_DOMAIN') ? SITE_DOMAIN : null,
|
|
];
|
|
|
|
return $context;
|
|
});
|
|
|
|
|
|
// Отключаем канонические ссылки и hreflang от Yoast SEO
|
|
add_filter('wpseo_canonical', '__return_false');
|
|
add_filter('wpseo_opengraph_url', '__return_false'); // Отключаем OG URL
|
|
add_filter('wpseo_add_x_default_hreflang', '__return_false'); // Отключаем hreflang от Yoast
|
|
add_filter('wpseo_disable_adjacent_rel_links', '__return_true'); // Отключаем соседние rel-ссылки
|
|
|
|
// Добавляем каноническую ссылку
|
|
add_action('wp_head', 'custom_canonical_url', 5);
|
|
function custom_canonical_url() {
|
|
if (!is_admin()) {
|
|
// Защищаем от дублирования
|
|
static $canonical_added = false;
|
|
if ($canonical_added) {
|
|
return;
|
|
}
|
|
$canonical_added = true;
|
|
|
|
// Формируем текущий URL без лишних параметров
|
|
$current_url = trailingslashit(home_url($_SERVER['REQUEST_URI']));
|
|
// Удаляем возможные параметры запроса, если они не нужны
|
|
$current_url = strtok($current_url, '?');
|
|
echo '<link rel="canonical" href="' . esc_url($current_url) . '" />' . "\n";
|
|
}
|
|
}
|
|
|
|
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; // Проверка, что заказ существует
|
|
|
|
$order = wc_get_order($order_id);
|
|
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(),
|
|
'quantity' => $item->get_quantity()
|
|
];
|
|
}
|
|
|
|
// Получаем валюту заказа
|
|
$currency = $order->get_currency();
|
|
?>
|
|
<script>
|
|
window.dataLayer = window.dataLayer || [];
|
|
dataLayer.push({
|
|
'ecommerce': {
|
|
'purchase': {
|
|
'actionField': {
|
|
'id': '<?php echo $order_id; ?>',
|
|
'revenue': '<?php echo $order->get_total(); ?>',
|
|
'currency': '<?php echo $currency; ?>'
|
|
},
|
|
'products': <?php echo json_encode($items); ?>
|
|
}
|
|
}
|
|
});
|
|
|
|
// Яндекс.Метрика
|
|
yaCounter96481053.reachGoal('purchase', {
|
|
'order_id': '<?php echo $order_id; ?>',
|
|
'order_price': '<?php echo $order->get_total(); ?>',
|
|
'currency': '<?php echo $currency; ?>',
|
|
'items': <?php echo json_encode($items); ?>
|
|
});
|
|
|
|
// Facebook Pixel
|
|
fbq('track', 'Purchase', {
|
|
value: <?php echo $order->get_total(); ?>,
|
|
currency: '<?php echo $currency; ?>',
|
|
content_ids: [<?php echo implode(',', array_column($items, 'id')); ?>],
|
|
content_type: 'product'
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
// Отключаем кэширование для страниц товаров
|
|
add_action('template_redirect', function() {
|
|
if (is_product()) {
|
|
header('Cache-Control: no-cache, no-store, must-revalidate');
|
|
header('Pragma: no-cache');
|
|
header('Expires: 0');
|
|
}
|
|
});
|
|
|
|
// Обработчик AJAX
|
|
add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
|
|
add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
|
|
|
|
function woocommerce_ajax_add_to_cart() {
|
|
error_log('AJAX handler called'); // Отладка: логируем вызов
|
|
|
|
$product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
|
|
$quantity = isset($_POST['quantity']) ? absint($_POST['quantity']) : 1;
|
|
|
|
error_log('Received product_id: ' . $product_id . ', quantity: ' . $quantity); // Отладка
|
|
|
|
if (!$product_id) {
|
|
error_log('Invalid product_id received'); // Отладка
|
|
wp_send_json_error(array('message' => 'Неверный ID товара'));
|
|
wp_die();
|
|
}
|
|
|
|
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
|
|
|
|
if ($passed_validation) {
|
|
$added = WC()->cart->add_to_cart($product_id, $quantity);
|
|
if ($added) {
|
|
error_log('Product added to cart: ' . $product_id); // Отладка
|
|
// Подготавливаем фрагменты корзины
|
|
ob_start();
|
|
woocommerce_mini_cart();
|
|
$mini_cart = ob_get_clean();
|
|
|
|
// Фрагменты для стандартной корзины и кастомного счетчика
|
|
$fragments = array(
|
|
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>',
|
|
'.mini-profile__button--counter' => '<div class="mini-profile__button--counter">' . WC()->cart->get_cart_contents_count() . '</div>'
|
|
);
|
|
|
|
wp_send_json_success(array(
|
|
'message' => 'Товар успешно добавлен в корзину',
|
|
'fragments' => apply_filters('woocommerce_add_to_cart_fragments', $fragments),
|
|
'cart_hash' => apply_filters('woocommerce_add_to_cart_hash', WC()->cart->get_cart_hash(), array())
|
|
));
|
|
} else {
|
|
error_log('Failed to add product to cart: ' . $product_id); // Отладка
|
|
wp_send_json_error(array('message' => 'Не удалось добавить товар в корзину'));
|
|
}
|
|
} else {
|
|
error_log('Validation failed for product: ' . $product_id); // Отладка
|
|
wp_send_json_error(array('message' => 'Ошибка валидации товара'));
|
|
}
|
|
|
|
wp_die();
|
|
}
|