woocommerce.php
5.62 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Add WooCommerce support
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
add_action( 'after_setup_theme', 'understrap_woocommerce_support' );
if ( ! function_exists( 'understrap_woocommerce_support' ) ) {
/**
* Declares WooCommerce theme support.
*/
function understrap_woocommerce_support() {
add_theme_support( 'woocommerce' );
// Add Product Gallery support.
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-slider' );
// Add Bootstrap classes to form fields.
add_filter( 'woocommerce_form_field_args', 'understrap_wc_form_field_args', 10, 3 );
add_filter( 'woocommerce_quantity_input_classes', 'understrap_quantity_input_classes' );
}
}
// First unhook the WooCommerce content wrappers.
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
// Then hook in your own functions to display the wrappers your theme requires.
add_action( 'woocommerce_before_main_content', 'understrap_woocommerce_wrapper_start', 10 );
add_action( 'woocommerce_after_main_content', 'understrap_woocommerce_wrapper_end', 10 );
if ( ! function_exists( 'understrap_woocommerce_wrapper_start' ) ) {
/**
* Display the theme specific start of the page wrapper.
*/
function understrap_woocommerce_wrapper_start() {
$container = get_theme_mod( 'understrap_container_type' );
echo '<div class="wrapper" id="woocommerce-wrapper">';
echo '<div class="' . esc_attr( $container ) . '" id="content" tabindex="-1">';
echo '<div class="row">';
get_template_part( 'global-templates/left-sidebar-check' );
echo '<main class="site-main" id="main">';
}
}
if ( ! function_exists( 'understrap_woocommerce_wrapper_end' ) ) {
/**
* Display the theme specific end of the page wrapper.
*/
function understrap_woocommerce_wrapper_end() {
echo '</main><!-- #main -->';
get_template_part( 'global-templates/right-sidebar-check' );
echo '</div><!-- .row -->';
echo '</div><!-- Container end -->';
echo '</div><!-- Wrapper end -->';
}
}
if ( ! function_exists( 'understrap_wc_form_field_args' ) ) {
/**
* Filter hook function monkey patching form classes
* Author: Adriano Monecchi http://stackoverflow.com/a/36724593/307826
*
* @param string $args Form attributes.
* @param string $key Not in use.
* @param null $value Not in use.
*
* @return mixed
*/
function understrap_wc_form_field_args( $args, $key, $value = null ) {
// Start field type switch case.
switch ( $args['type'] ) {
// Targets all select input type elements, except the country and state select input types.
case 'select':
/*
* Add a class to the field's html element wrapper - woocommerce
* input types (fields) are often wrapped within a <p></p> tag.
*/
$args['class'][] = 'form-group mb-3';
// Add a class to the form input itself.
$args['input_class'][] = 'form-control';
// Add custom data attributes to the form input itself.
$args['custom_attributes'] = array(
'data-plugin' => 'select2',
'data-allow-clear' => 'true',
'aria-hidden' => 'true',
);
break;
/*
* By default WooCommerce will populate a select with the country names - $args
* defined for this specific input type targets only the country select element.
*/
case 'country':
$args['class'][] = 'form-group mb-3 single-country';
break;
/*
* By default WooCommerce will populate a select with state names - $args defined
* for this specific input type targets only the country select element.
*/
case 'state':
$args['class'][] = 'form-group mb-3';
$args['custom_attributes'] = array(
'data-plugin' => 'select2',
'data-allow-clear' => 'true',
'aria-hidden' => 'true',
);
break;
case 'textarea':
$args['input_class'][] = 'form-control';
break;
case 'checkbox':
$args['class'][] = 'form-group mb-3';
// Wrap the label in <span> tag.
$args['label'] = isset( $args['label'] ) ? '<span class="custom-control-label">' . $args['label'] . '<span>' : '';
// Add a class to the form input's <label> tag.
$args['label_class'][] = 'custom-control custom-checkbox';
$args['input_class'][] = 'custom-control-input';
break;
case 'radio':
$args['label_class'][] = 'custom-control custom-radio';
$args['input_class'][] = 'custom-control-input';
break;
default:
$args['class'][] = 'form-group mb-3';
$args['input_class'][] = 'form-control';
break;
} // End of switch ( $args ).
return $args;
}
}
if ( ! is_admin() && ! function_exists( 'wc_review_ratings_enabled' ) ) {
/**
* Check if reviews are enabled.
*
* Function introduced in WooCommerce 3.6.0., include it for backward compatibility.
*
* @return bool
*/
function wc_reviews_enabled() {
return 'yes' === get_option( 'woocommerce_enable_reviews' );
}
/**
* Check if reviews ratings are enabled.
*
* Function introduced in WooCommerce 3.6.0., include it for backward compatibility.
*
* @return bool
*/
function wc_review_ratings_enabled() {
return wc_reviews_enabled() && 'yes' === get_option( 'woocommerce_enable_review_rating' );
}
}
if ( ! function_exists( 'understrap_quantity_input_classes' ) ) {
/**
* Add Bootstrap class to quantity input field.
*
* @param array $classes Array of quantity input classes.
* @return array
*/
function understrap_quantity_input_classes( $classes ) {
$classes[] = 'form-control';
return $classes;
}
}