-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·426 lines (366 loc) · 17.5 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
418
419
420
421
422
423
424
425
426
<?php
add_action('wp_head', function () {
//Close PHP tags
?>
<div id="preloader"
style="display: block; position: fixed; left: 0; top: 0; right: 0; bottom: 0;background: #ffffff; z-index: 100000;">
<script>
</script>
<style>#preloader {
transition: .3s background;
}
#preloader.is-active {
background: rgba(255, 255, 255, 0) !important;
}
</style>
</div>
<?php //Open PHP tags
}, 10, 2);
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('child-variable-style', get_stylesheet_directory_uri() . '/css/variables.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-fonts-style', get_stylesheet_directory_uri() . '/css/fonts.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-reset-style', get_stylesheet_directory_uri() . '/css/reset.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-acc-item-style', get_stylesheet_directory_uri() . '/css/widgets/account-item.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-bg-style', get_stylesheet_directory_uri() . '/css/widgets/buttons.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-buttons-style', get_stylesheet_directory_uri() . '/css/widgets/bg.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-cart-style', get_stylesheet_directory_uri() . '/css/widgets/cart.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-controls-style', get_stylesheet_directory_uri() . '/css/widgets/controls.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-chckt-brdcrmbs-style', get_stylesheet_directory_uri() . '/css/widgets/checkout-breadcrumbs.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-mini-cart-style', get_stylesheet_directory_uri() . '/css/widgets/mini-cart.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-forms-style', get_stylesheet_directory_uri() . '/css/widgets/forms.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-lightbox-style', get_stylesheet_directory_uri() . '/css/widgets/lightbox.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-mfp-style', get_stylesheet_directory_uri() . '/css/widgets/mfp.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-offices-style', get_stylesheet_directory_uri() . '/css/widgets/offices.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-reviews-style', get_stylesheet_directory_uri() . '/css/widgets/reviews.css', ['flatsome-main','flatsome-shop'], '1.0', 'all');
wp_enqueue_style('child-sidebar-style', get_stylesheet_directory_uri() . '/css/widgets/sidebar.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-header-style', get_stylesheet_directory_uri() . '/css/header.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-footer-style', get_stylesheet_directory_uri() . '/css/footer.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/css/style.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('slick-carousel', get_stylesheet_directory_uri() . '/css/vendor/slick.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('slick-carousel-theme', get_stylesheet_directory_uri() . '/css/vendor/slick-theme.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-about-style', get_stylesheet_directory_uri() . '/css/pages/about.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-cart-page-style', get_stylesheet_directory_uri() . '/css/pages/cart.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-chckt-page-style', get_stylesheet_directory_uri() . '/css/pages/checkout.css', ['flatsome-main', 'flatsome-shop'], '1.0', 'all');
wp_enqueue_style('child-cntcts-page-style', get_stylesheet_directory_uri() . '/css/pages/contacts.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-dlvr-page-style', get_stylesheet_directory_uri() . '/css/pages/delivery.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-mainpage-style', get_stylesheet_directory_uri() . '/css/pages/mainpage.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-or-compl-style', get_stylesheet_directory_uri() . '/css/pages/order-complete.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-shop-style', get_stylesheet_directory_uri() . '/css/pages/shop.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_style('child-adaptive-style', get_stylesheet_directory_uri() . '/css/adaptive.css', ['flatsome-main'], '1.0', 'all');
wp_enqueue_script('slick-carousel', get_stylesheet_directory_uri() . '/js/slick.min.js', [], 'false', 'all');
wp_enqueue_script('child-script', get_stylesheet_directory_uri() . '/js/script.js', [], 'false', 'all');
wp_enqueue_script('child-product-card-script', get_stylesheet_directory_uri() . '/js/product-card.js', [], 'false', 'all');
});
function prefix_add_footer_styles()
{
wp_enqueue_style('child-product-style', get_stylesheet_directory_uri() . '/css/widgets/product.css', ['flatsome-main'], '1.0', 'all');
}
;
add_action('get_footer', 'prefix_add_footer_styles');
/******************
* Single product page
******************/
/**
* Remove additional information tab
**/
add_filter('woocommerce_product_tabs', function ($tabs) {
unset($tabs['description']);
unset($tabs['additional_information']);
return $tabs;
}, 100, 1);
/**
* Remove share tabs
**/
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50);
/**
* Remove short description
**/
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20);
function woocommerce_template_single_excerpt()
{
return;
}
/**
* Add "additional information" after add to cart
**/
add_action('woocommerce_single_product_summary', function () {
global $product;
wc_display_product_attributes($product);
if (strlen($product->description) > 20 || strlen($product->short_description) > 20)
echo "<p class='product-description-title'>Description</p>
<div class='description-container'>
<div class='term-description product-description'>" . (strlen($product->description) > 20 ? $product->description : $product->short_description) . "</div>
</div>";
woocommerce_template_single_sharing();
}, 135);
add_action('woocommerce_before_single_product', function () {
?>
<h1 class="product-title product_title entry-title">
<?php the_title(); ?>
<?php wc_get_template('loop/sale-flash.php'); ?>
</h1> <?php
}, 135);
/*
* Shop | category page
*/
add_action('init', function () {
add_action('woocommerce_archive_description', function () {
/**
* Show title on category | shop page
**/
$title = strtolower(trim(wp_title('', false)));
echo $title == 'products' ? '<h1>Shop</h1>' : '<h1>' . single_term_title('', false) . '</h1>';
if (is_product_category()) {
/**
* description
**/
remove_action('woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10);
$term = get_queried_object();
$term = $term->term_id ?? 0;
$description = get_term_field('description', $term);
if (strlen($description) > 20) echo "<div class='description-container'> <div class='term-description product-description'>" . $description . "</div>
</div>";
/**
* subcategories list
**/
$taxonomy = 'product_cat';
$parent_terms = get_ancestors(get_queried_object_id(), $taxonomy);
if (sizeof($parent_terms) > 0) {
$term_id = sizeof($parent_terms) == 1 ? get_queried_object_id() : $parent_terms[0];
// Get subcategories of the current category
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $term_id
]);
echo '<ul class="subcategories-list">';
// Loop through product subcategories WP_Term Objects
foreach ($terms as $term) {
$active = $term->term_id == get_queried_object_id() ? 'is-active' : '';
$term_link = get_term_link($term, $taxonomy);
echo '<li class="' . $term->slug . '"><a href="' . $term_link . '" class="' . $active . '">' . $term->name . '</a></li>';
}
echo '</ul>';
} else {
$children_terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => get_queried_object_id()
]);
if (!empty($children_terms)) {
echo '<ul class="subcategories-list">';
// Loop through product subcategories WP_Term Objects
foreach ($children_terms as $term) {
$active = $term->term_id == get_queried_object_id() ? 'is-active' : '';
$term_link = get_term_link($term, $taxonomy);
echo '<li class="' . $term->slug . '"><a href="' . $term_link . '" class="' . $active . '">' . $term->name . '</a></li>';
}
echo '</ul>';
}
}
}
}, 2);
/**
* hide shop title
**/
add_filter('woocommerce_page_title', function () {
return "";
});
/*
// Remove result count.
remove_action( 'flatsome_category_title_alt', 'woocommerce_result_count', 20 );
// Remove ordering dropdown.
remove_action( 'flatsome_category_title_alt', 'woocommerce_catalog_ordering', 30 );
*/
});
/**
* The jQuery code that will handle the event getting the required product data
**/
add_action('wp_footer', 'added_to_cart_js_event');
function added_to_cart_js_event()
{
?>
<script type="text/javascript">
(function ($) {
$(document.body).on('added_to_cart', function (event, fragments, cart_hash, button) {
button.attr('data-total', parseInt(button.data('total')) + 1);
addedToCartNotification();
});
})(jQuery);
</script>
<?php
}
/**
* View cart button
*/
function woocommerce_widget_shopping_cart_button_view_cart()
{
echo '<a href="' . esc_url(wc_get_cart_url()) . '" class="button wc-forward btn-outline">' . esc_html__('View cart', 'woocommerce') . '</a>';
}
/**
* Checkout button
*/
function woocommerce_widget_shopping_cart_proceed_to_checkout()
{
echo '<a href="' . esc_url(wc_get_checkout_url()) . '" class="button checkout wc-forward btn-filled">' . esc_html__('Checkout', 'woocommerce') . '</a>';
}
/**
* Change text strings
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function my_text_strings($translated_text, $text, $domain)
{
switch (strtolower($translated_text)) {
case 'cart' :
$translated_text = __('Shopping Cart', 'woocommerce');
break;
case 'billing details' :
$translated_text = __('Shipping details', 'woocommerce');
break;
}
return $translated_text;
}
add_filter('gettext', 'my_text_strings', 20, 3);
/**
* Added to cart from category
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
add_filter('woocommerce_loop_add_to_cart_link', function ($html, $product) {
if ($product && $product->is_type('simple') && $product->is_purchasable() && $product->is_in_stock() && !$product->is_sold_individually()) {
$product_id = $product->get_id();
$quantity = 0;
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $product_id) $quantity = $cart_item['quantity'];
}
$html = '<div class="add-to-cart-button"><a href="?add-to-cart=' . $product_id . '" data-total="' . $quantity . '" data-quantity="1" class="primary is-small mb-0 button product_type_simple add_to_cart_button ajax_add_to_cart is-outline" data-product_id="' . $product_id . '" data-product_sku="" aria-label="' . $product->get_title() . '" rel="nofollow">Add to cart</a></div>';
}
return $html;
}, 10, 2);
/**
* Manage checkout fields
*
*/
add_filter('woocommerce_checkout_fields', function ($fields) {
$fields['billing']['billing_first_name']['placeholder'] = 'First name*';
$fields['billing']['billing_last_name']['placeholder'] = 'Last name*';
$fields['billing']['billing_company']['placeholder'] = 'Company name (optional)';
$fields['billing']['billing_address_2']['placeholder'] = 'Apartment';
$fields['billing']['billing_city']['placeholder'] = 'Town / City*';
$fields['billing']['billing_state']['placeholder'] = 'State / County (optional)';
$fields['billing']['billing_phone']['placeholder'] = 'Phone*';
$fields['billing']['billing_email']['placeholder'] = 'Email address*';
$fields['order']['order_comments']['placeholder'] = 'Order notes (optional)';
$fields['billing']['billing_postcode']['placeholder'] = 'Postcode / ZIP';
$fields['billing']['billing_first_name']['label'] = $fields['billing']['billing_last_name']['label'] = $fields['billing']['billing_company']['label'] = $fields['billing']['billing_city']['label'] = $fields['billing']['billing_state']['label'] = $fields['billing']['billing_phone']['label'] = $fields['billing']['billing_email']['label'] = $fields['order']['order_comments']['label'] = $fields['billing']['billing_postcode']['label'] = '';
return $fields;
});
function exclude_products_from_child_cats($wp_query)
{
if (!is_admin() && $wp_query->is_main_query()) {
if (isset($wp_query->query_vars['product_cat'])) {
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $wp_query->query_vars['product_cat'],
'include_children' => false
)
);
$wp_query->set('tax_query', $tax_query);
}
}
}
add_filter('pre_get_posts', 'exclude_products_from_child_cats');
/**
* Shop type in category
*/
//Product Cat creation page
function text_domain_taxonomy_add_new_meta_field()
{
?>
<div class="form-field">
<label for="term_meta[shop_category]"><?php _e('Category type', 'text_domain'); ?></label>
<select name="term_meta[shop_category]" id="term_meta[shop_category]">
<option value="">-- None --</option>
<option class="level-0" value="1">Shop</option>
<option class="level-0" value="2">Catering</option>
</select>
<p class="description"><?php _e('Shop or catering', 'text_domain'); ?></p>
</div>
<?php
}
add_action('product_cat_add_form_fields', 'text_domain_taxonomy_add_new_meta_field', 10, 2);
//Product Cat Edit page
function text_domain_taxonomy_edit_meta_field($term)
{
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label
for="term_meta[shop_category]"><?php _e('Category type', 'text_domain'); ?></label></th>
<td>
<?php $shop_category = esc_attr($term_meta['shop_category']) ?? ''; ?>
<select name="term_meta[shop_category]" id="term_meta[shop_category]">
<option value="">-- None --</option>
<option <?php if ($shop_category == 1) echo "selected='selected'"; ?> value="1">Shop</option>
<option <?php if ($shop_category == 2) echo "selected='selected'"; ?> value="2">Catering</option>
</select>
<p class="description"><?php _e('Shop or catering', 'text_domain'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'text_domain_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id)
{
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_id);
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_" . $term_id, $term_meta);
}
}
add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 2);
/**
* change currency symbol to AED
*/
add_filter('woocommerce_currency_symbol', 'wc_change_uae_currency_symbol', 10, 2);
function wc_change_uae_currency_symbol($currency_symbol, $currency)
{
switch ($currency) {
case 'AED':
$currency_symbol = 'AED';
break;
}
return $currency_symbol;
}
/**
* Item quantity in cart
*/
function get_item_qty($product)
{
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// for variable products (product varations)
$product_id = $product->id;
if ($product_id == 0 || empty($product_id))
$product_id = $product->get_id();
if ($product_id == $cart_item['product_id']) {
return [ $cart_item['quantity'], $cart_item_key ];
// break;
}
}
return [false, false];
}
require_once (__DIR__ . '/functions/ajax.php');