/
functions.php
417 lines (336 loc) · 17.4 KB
/
functions.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* Sage includes
*
* The $sage_includes array determines the code library included in your theme.
* Add or remove files to the array as needed. Supports child theme overrides.
*
* Please note that missing files will produce a fatal error.
*
* @link https://github.com/roots/sage/pull/1042
*/
$sage_includes = [
'lib/utils.php', // Utility functions
'lib/init.php', // Initial theme setup and constants
'lib/wrapper.php', // Theme wrapper class
'lib/config.php', // Configuration
'lib/assets.php', // Scripts and stylesheets
'lib/titles.php', // Page titles
'lib/extras.php', // Custom functions
];
foreach ($sage_includes as $file) {
if (!$filepath = locate_template($file)) {
trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR);
}
require_once $filepath;
}
unset($file, $filepath);
// remove add to cart button from category page
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
// Register Slider Post Type
function slider_post_type() {
$labels = array(
'name' => _x( 'Slides', 'Post Type General Name', 'sage' ),
'singular_name' => _x( 'Slide', 'Post Type Singular Name', 'sage' ),
'menu_name' => __( 'Slider', 'sage' ),
'name_admin_bar' => __( 'Slider', 'sage' ),
'parent_item_colon' => __( '', 'sage' ),
'all_items' => __( 'Slides', 'sage' ),
'add_new_item' => __( 'Add New Item', 'sage' ),
'add_new' => __( 'Add New', 'sage' ),
'new_item' => __( 'New Item', 'sage' ),
'edit_item' => __( 'Edit Item', 'sage' ),
'update_item' => __( 'Update Item', 'sage' ),
'view_item' => __( 'View Item', 'sage' ),
'search_items' => __( 'Search Item', 'sage' ),
'not_found' => __( 'Not found', 'sage' ),
'not_found_in_trash' => __( 'Not found in Trash', 'sage' ),
);
$args = array(
'label' => __( 'Slide', 'sage' ),
'description' => __( 'Slider for Homepage', 'sage' ),
'labels' => $labels,
'supports' => array( 'title', 'excerpt', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-images-alt',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'post',
);
register_post_type( 'slider', $args );
}
add_action( 'init', 'slider_post_type', 0 );
// responcive youtube videos
add_filter( 'embed_oembed_html', 'custom_oembed_filter', 10, 4 ) ;
function custom_oembed_filter($html, $url, $attr, $post_ID) {
$return = '<div class="video-container">'.$html.'</div>';
return $return;
}
// Add new register fields for WooCommerce registration.
function wooc_extra_register_fields() {
?>
<p class="form-row">
<label for="reg_billing_first_name"><?php _e( 'First Name', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row">
<label for="reg_billing_last_name"><?php _e( 'Last Name', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// WooCommerce billing last name.
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
// change appearance of catalog sorting fields
remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);
/** Remove Showing results functionality site-wide */
function woocommerce_result_count() {
return;
}
// Lets create the function to house our form
//remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
function woocommerce_catalog_page_ordering() {
?>
<?php echo '<div class="itemsorder"><h3>Sort:</h3>' ?>
<form action="" method="POST" name="results" class="woocommerce-ordering">
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
<?php
//Get products on page reload
if (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
$numberOfProductsPerPage = $_POST['woocommerce-sort-by-columns'];
} else {
$numberOfProductsPerPage = $_COOKIE['shop_pageResults'];
}
// This is where you can change the amounts per page that the user will use feel free to change the numbers and text as you want, in my case we had 4 products per row so I chose to have multiples of four for the user to select.
$shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
//Add as many of these as you like, -1 shows all products per page
// '' => __('Results per page', 'woocommerce'),
'12' => __('12', 'woocommerce'),
'24' => __('24', 'woocommerce'),
'32' => __('32', 'woocommerce'),
'-1' => __('All', 'woocommerce'),
));
foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
echo '<option value="' . $sort_id . '" ' . selected( $numberOfProductsPerPage, $sort_id, true ) . ' >' . $sort_name . '</option>';
?>
</select>
</form>
<?php echo ' <h3>Products / Page:</h3></div>' ?>
<?php
}
// now we set our cookie if we need to
function dl_sort_by_page($count) {
if (isset($_COOKIE['shop_pageResults'])) { // if normal page load with cookie
$count = $_COOKIE['shop_pageResults'];
}
if (isset($_POST['woocommerce-sort-by-columns'])) { //if form submitted
setcookie('shop_pageResults', $_POST['woocommerce-sort-by-columns'], time()+1209600, '/', 'www.your-domain-goes-here.com', false); //this will fail if any part of page has been output- hope this works!
$count = $_POST['woocommerce-sort-by-columns'];
}
// else normal page load and no cookie
return $count;
}
add_filter('loop_shop_per_page','dl_sort_by_page');
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 40 );
// change pagination .prev & .next labels
add_filter( 'woocommerce_pagination_args' , 'custom_override_pagination_args' );
function custom_override_pagination_args( $args ) {
$args['prev_text'] = __( 'Previous Page' );
$args['next_text'] = __( 'Next Page' );
return $args;
}
// change place for "my contributions"
if( function_exists('wc_product_reviews_pro') && wc_product_reviews_pro()) {
remove_action( 'woocommerce_before_my_account', array( wc_product_reviews_pro()->frontend, 'render_my_account_contributions' ), 11 );
add_action( 'woocommerce_after_my_account', array( wc_product_reviews_pro()->frontend, 'render_my_account_contributions' ), 5 );
}
// change place for wishlists
add_filter('woocommerce_wishlists_account_location', 'change_woocommerce_wishlists_account_location', 10, 1);
function change_woocommerce_wishlists_account_location($location) {
return 'before';
}
// Remove quantity in all product type
function wc_remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
// change place for price
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10, 2);
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 25, 2);
// change place for product_meta
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40, 2);
//add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 10, 2);
// change place for rating
//remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10, 2);
//tabs
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['reviews']['title'] = __( 'Reviews' );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['additional_information'] );
return $tabs;
}
// change my orders title
add_filter( 'woocommerce_my_account_my_orders_title', 'override_my_account_my_orders_title', 10, 1 );
function override_my_account_my_orders_title( $message ) {
return __( 'Order History', 'woocommerce' );
}
// add go to shop link on cart page
add_action('woocommerce_before_cart', 'woo_go_to_shop_link', 10);
function woo_go_to_shop_link() {
$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
echo '<a href="'. $shop_page_url .'" class="gotoshop">Continue Shopping</a>';
}
// login-logout links in menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if ( $args-> theme_location == 'primary_navigation') {
$items .= '<li id="" class="menu-item menu-item-type-custom menu-item-object-custom"><a id="gw-header" href="#" onclick="GrooveWidget.toggle(); return false;">Support</a></li>';
}
if (is_user_logged_in() && $args-> theme_location == 'primary_navigation') {
$memberlink = '<a href="'. site_url('/account/') .'">My Account</a>';
$items .= '<li class="marg"><a href="'. wp_logout_url( home_url() ) .'">Logout</a></li> <li class="span"><span>|</span></li> <li>'. $memberlink .'</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary_navigation') {
$items .= '<li class="marg"><a href="'. site_url('/account/#member_login') .'">Login</a></li> <li class="span"><span>|</span></li> <li><a href="'. site_url('/account/#member_register') .'">Signup</a></li>';
}
return $items;
}
// change add to cart text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
return __( 'Add to Cart', 'woocommerce' );
}
// change catalog ordering function
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
if ( ! function_exists( 'woocommerce_new_catalog_ordering' ) ) {
function woocommerce_new_catalog_ordering() {
global $wp_query;
if ( ! woocommerce_products_will_display() ) {
return;
}
$orderby = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( ! $show_default_orderby ) {
unset( $catalog_orderby_options['menu_order'] );
}
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
unset( $catalog_orderby_options['rating'] );
}
wc_get_template( 'loop/orderby.php', array( 'catalog_orderby_options' => $catalog_orderby_options, 'orderby' => $orderby, 'show_default_orderby' => $show_default_orderby ) );
}
}
add_action( 'woocommerce_before_shop_loop', 'woocommerce_new_catalog_ordering', 30 );
// remove cross-sell from cart
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' , 10 );
/* CHECKOUT CHANGES */
// customize checkout fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name';
$fields['billing']['billing_first_name']['label'] = '';
$fields['billing']['billing_last_name']['placeholder'] = 'Last Name';
$fields['billing']['billing_last_name']['label'] = '';
$fields['billing']['billing_company']['placeholder'] = 'Company (optional)';
$fields['billing']['billing_company']['label'] = '';
$fields['billing']['billing_email']['placeholder'] = 'Email Address';
$fields['billing']['billing_email']['label'] = '';
$fields['billing']['billing_phone']['placeholder'] = 'Phone';
$fields['billing']['billing_phone']['label'] = '';
$fields['billing']['billing_country']['placeholder'] = 'Country';
$fields['billing']['billing_country']['label'] = '';
$fields['billing']['billing_country']['class'] = array('form-row-last address-field update_totals_on_change woocommerce-validated');
$fields['billing']['billing_address_1']['placeholder'] = 'Address';
$fields['billing']['billing_address_1']['label'] = '';
$fields['billing']['billing_address_1']['class'] = array('form-row-first address-field validate-required woocommerce-validated');
$fields['billing']['billing_address_2']['class'] = array('form-row-last address-field woocommerce-validated');
$fields['billing']['billing_city']['placeholder'] = 'Town/City';
$fields['billing']['billing_city']['label'] = '';
$fields['billing']['billing_city']['class'] = array('form-row-first address-field woocommerce-validated');
$fields['billing']['billing_state']['placeholder'] = 'State';
$fields['billing']['billing_state']['label'] = '';
$fields['billing']['billing_state']['class'] = array('form-row-last address-field validate-required update_totals_on_change woocommerce-validated');
$fields['billing']['billing_postcode']['placeholder'] = 'Postcode/Zip';
$fields['billing']['billing_postcode']['label'] = '';
$fields['billing']['billing_postcode']['class'] = array('form-row-first address-field validate-required');
$order = array(
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_email",
"billing_phone",
"billing_address_1",
"billing_address_2",
"billing_city",
"billing_country",
"billing_postcode",
"billing_state"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
// change place for payment option
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_new_payment_place', 'woocommerce_checkout_payment', 20 );
// change Order Button text
add_filter( 'woocommerce_order_button_text', create_function( '', 'return "Place Order";' ) );
// change credit card fields
add_filter( 'woocommerce_credit_card_form_fields' , 'custom_override_cards_fields' );
function custom_override_cards_fields( $fields ) {
$fields['card-number-field'] = '<p class="form-row form-row-wide"><input id="stripe-card-number" class="input-text wc-credit-card-form-card-number" type="text" maxlength="20" autocomplete="off" placeholder="Card Number"/></p>';
$fields['card-expiry-field'] = '<p class="form-row form-row-first"><input id="stripe-card-expiry" class="input-text wc-credit-card-form-card-expiry" type="text" autocomplete="off" placeholder="Expiration Date (MM/YY)"/></p>';
$fields['card-cvc-field'] = '<p class="form-row form-row-last"><input id="stripe-card-cvc" class="input-text wc-credit-card-form-card-cvc" type="text" autocomplete="off" placeholder="CVC Code"/></p>';
return $fields;
}
// remove Order Again button from thank you page
remove_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button' );