-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
123 lines (107 loc) · 4.47 KB
/
Copy pathfunctions.php
File metadata and controls
123 lines (107 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require get_template_directory() . '/inc/class-wh-nav-walker.php';
require get_template_directory() . '/inc/post-types.php';
require get_template_directory() . '/inc/subscribe.php';
require get_template_directory() . '/inc/catalog-filters.php';
require get_template_directory() . '/inc/last-viewed.php';
add_action(
'after_setup_theme',
function () {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
// Без поддержки штатной галереи (zoom/lightbox/slider) — на странице
// товара свой переключатель миниатюр (product-detail.js), а не
// woocommerce_show_product_images().
add_theme_support( 'woocommerce' );
register_nav_menus(
array(
'menu-desktop' => __( 'Desktop menu', 'wh' ),
'menu-mobile' => __( 'Mobile menu', 'wh' ),
'footer-info' => __( 'Footer: Info', 'wh' ),
'footer-service' => __( 'Footer: Customer service', 'wh' ),
'footer-useful' => __( 'Footer: Useful information', 'wh' ),
)
);
// Тема отдаёт свою разметку везде — штатный CSS WooCommerce (float-раскладка
// woocommerce-general/layout/smallscreen) только мешает, накладываясь на BEM.
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
// "Showing all N results" и дропдаун сортировки дублируют наш собственный
// .filters__count в панели фильтров — их float ещё и поджимал .catalog__wrapper
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
}
);
/**
* Читает build/.vite/manifest.json (собирается `npm run build:theme` /
* `npm run watch:theme` из vite.theme.config.js) и подключает CSS/JS по их
* настоящим хешированным именам вместо жёстко прописанных путей.
*/
function wh_enqueue_assets() {
// global.scss задаёт font-family: "Lato" — без её реального подключения
// браузер молча падает на sans-serif, и это незаметно, пока не сравнишь
// с макетом: шрифт просто выглядит "обычным"
wp_enqueue_style(
'wh-google-fonts',
'https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700;900&display=swap',
array(),
null
);
$manifest_path = get_template_directory() . '/build/.vite/manifest.json';
if ( ! file_exists( $manifest_path ) ) {
return;
}
$manifest = json_decode( file_get_contents( $manifest_path ), true );
$entry = $manifest['src/main.js'] ?? null;
if ( ! $entry ) {
return;
}
$build_uri = get_template_directory_uri() . '/build/';
if ( ! empty( $entry['file'] ) ) {
wp_enqueue_script( 'wh-main', $build_uri . $entry['file'], array(), null, true );
wp_localize_script(
'wh-main',
'whSubscription',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wh_subscribe' ),
)
);
}
foreach ( $entry['css'] ?? array() as $css_file ) {
wp_enqueue_style( 'wh-main', $build_uri . $css_file, array(), null );
}
}
add_action( 'wp_enqueue_scripts', 'wh_enqueue_assets' );
function wh_cart_contents_count() {
if ( ! function_exists( 'WC' ) || ! WC()->cart ) {
return 0;
}
return WC()->cart->get_cart_contents_count();
}
/**
* " (N)" при непустой корзине, иначе пусто — тот же формат, что рисовал
* прежний renderCartCount() в cart.js до перехода на серверный рендер.
*/
function wh_cart_count_label() {
$count = wh_cart_contents_count();
return $count > 0 ? ' (' . $count . ')' : '';
}
/**
* Оба .cart__count (в шапке и в мобильном меню) обновляются этим же
* фрагментом: jQuery(selector).replaceWith() у wc-cart-fragments заменяет
* все элементы, подходящие под селектор, а не только первый.
*/
add_filter(
'woocommerce_add_to_cart_fragments',
function ( $fragments ) {
ob_start();
?>
<span class="cart__count" data-cart-count aria-hidden="true"><?php echo esc_html( wh_cart_count_label() ); ?></span>
<?php
$fragments['.cart__count'] = trim( ob_get_clean() );
return $fragments;
}
);