parent
c8108c2234
commit
81962b283a
@ -1,22 +1,46 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
// add_filter('woocommerce_get_script_data', 'add_custom_woocommerce_params', 10, 2); |
// add_filter('woocommerce_get_script_data', 'add_custom_woocommerce_params', 10, 2); |
||||||
// function add_custom_woocommerce_params($params, $handle) { |
// function add_custom_woocommerce_params($params, $handle) { |
||||||
// // Добавляем только для скрипта корзины |
// // Добавляем только для скрипта корзины |
||||||
// $params['i18n_restore_item'] = pll__('Восстановить'); |
// $params['i18n_restore_item'] = pll__('Восстановить'); |
||||||
// return $params; |
// return $params; |
||||||
// } |
// } |
||||||
|
|
||||||
add_filter('timber/context', function($context) { |
|
||||||
$context['cart_count'] = WC()->cart->get_cart_contents_count(); |
if (!class_exists('WooCommerce')) { |
||||||
return $context; |
return; // Прерываем, если WooCommerce не активен |
||||||
}); |
} |
||||||
|
|
||||||
|
// Откладываем выполнение фильтра timber/context |
||||||
add_action('wp_enqueue_scripts', 'enable_ajax_add_to_cart_single'); |
add_action('wp', function() { |
||||||
function enable_ajax_add_to_cart_single() { |
add_filter('timber/context', function($context) { |
||||||
if (is_product()) { |
$context['cart_count'] = 0; |
||||||
wp_enqueue_script('wc-add-to-cart-variation'); // для вариативных товаров |
if (class_exists('WooCommerce') && !is_null(WC()->cart)) { |
||||||
wp_enqueue_script('wc-add-to-cart'); // основной скрипт для добавления в корзину |
$context['cart_count'] = WC()->cart->get_cart_contents_count(); |
||||||
} |
} |
||||||
|
if (defined('WP_IMPORTING') && WP_IMPORTING) { |
||||||
|
$context['cart_count'] = 0; |
||||||
|
} |
||||||
|
return $context; |
||||||
|
}); |
||||||
|
}, 20); |
||||||
|
|
||||||
|
// Подключение скриптов для AJAX-добавления в корзину |
||||||
|
add_action('wp_enqueue_scripts', 'enable_ajax_add_to_cart_single'); |
||||||
|
function enable_ajax_add_to_cart_single() { |
||||||
|
if (is_product()) { |
||||||
|
wp_enqueue_script('wc-add-to-cart-variation'); |
||||||
|
wp_enqueue_script('wc-add-to-cart'); |
||||||
|
error_log('WooCommerce scripts enqueued on product page'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Добавление перевода для WooCommerce скриптов |
||||||
|
add_filter('woocommerce_get_script_data', 'add_custom_woocommerce_params', 10, 2); |
||||||
|
function add_custom_woocommerce_params($params, $handle) { |
||||||
|
if (in_array($handle, ['wc-cart', 'wc-add-to-cart'])) { |
||||||
|
$params['i18n_restore_item'] = function_exists('pll__') ? pll__('Восстановить') : __('Восстановить', 'cosmopet'); |
||||||
|
} |
||||||
|
return $params; |
||||||
} |
} |
Loading…
Reference in new issue