-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
629 lines (472 loc) · 21.2 KB
/
functions.php
File metadata and controls
629 lines (472 loc) · 21.2 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* WooCommerce Functionality Customizations and Extensions
*
* Description:
* This file is a dynamic script designed to customize and extend the
* functionality of WooCommerce. Its purpose is to continuously improve
* and adapt features based on project requirements.
*
* Key Features:
* - The file will be frequently updated with new features and improvements.
* - Designed to provide flexibility and scalability for WooCommerce installations.
* - Includes well-documented code for easy maintenance and understanding.
*
* Usage:
* - Place this file in your WordPress theme's directory (`wp-content/themes/your-theme/functions.php`).
* - Ensure WooCommerce is installed and activated on your WordPress site.
* - Customize the code as per your requirements.
*
* Author: Anastasios Lamprianidis
* Site: https://lamprian.github.io/
*
* License: GPL-3.0+
*
* Copyright (C) 2026
*
* This program is distributed under the GNU General Public License, version 3 (GPL-3.0+).
* See the full license at https://www.gnu.org/licenses/gpl-3.0.html
*/
/*
Start: This function is used to reorder the menu items on the "My Account" page of WooCommerce.
It defines a new order for the tabs displayed to users when they visit their account page.
*/
// Apply the function through the 'woocommerce_account_menu_items' filter
add_filter( 'woocommerce_account_menu_items', 'custom_reorder_account_menu' );
/*
End: The function returns the newly ordered menu items, improving user experience on the account page.
*/
// Apply the function through the 'woocommerce_account_menu_items' filter
add_filter( 'woocommerce_account_menu_items', 'custom_reorder_account_menu' );
/*
End: The function returns the newly ordered menu items, improving user experience on the account page.
*/
/*
Start: This function adds the payment method used by the customer to the order email sent to the store administrators.
Using a WooCommerce hook, the function displays the payment method to the admin each time a new order is created.
*/
if ( !function_exists( 'evolution_add_payment_method_to_admin_new_order' ) ) :
/**
* Adds the payment method to the admin's email
*
* @hooked woocommerce_email_after_order_table()
*/
function evolution_add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
// Check if the email is being sent to the admin
if ( $is_admin_email && is_a( $order, 'WC_Order' ) ) {
// Display the payment method in the admin email
echo '<p><strong>Used Payment Method:</strong> ' . esc_html( $order->get_payment_method_title() ) . '</p>';
}
}
// Attach the function to WooCommerce via the 'woocommerce_email_after_order_table' hook
add_action( 'woocommerce_email_after_order_table', 'evolution_add_payment_method_to_admin_new_order', 15, 2 );
endif;
/*
End: The function uses the 'woocommerce_email_after_order_table' hook to add the payment method
to the admin's email. This allows administrators to easily see which payment method
the customer used for their order.
*/
/*
Start: This function hides the prices and the "Add to Cart" buttons for users who are not logged in.
Only logged-in users can see the prices and add products to the cart. For non-logged-in users,
a message with a link to log in is displayed.
*/
if ( !function_exists( 'evolution_hide_price_add_cart_not_logged_in' ) ) :
/**
* Hides the price and "Add to Cart" buttons for non-logged-in users
*/
@@ -158,179 +158,226 @@ function activate_gutenberg_product( $can_edit, $post_type ) {
$can_edit = true; // Enables the use of the Gutenberg editor
}
return $can_edit; // Returns the value of the $can_edit variable
}
add_filter( 'use_block_editor_for_post_type', 'activate_gutenberg_product', 10, 2 );
/*
End: This function is hooked to the 'use_block_editor_for_post_type' filter, which determines whether
the Gutenberg editor is used for a specific post type. By adding this filter, the use of Gutenberg
is enabled for the "product" post type in WooCommerce.
*/
/*
Start: This function automatically deletes the images associated with a WooCommerce product
when the product is deleted. Both the featured image and the gallery images are deleted.
This is useful for keeping the file system clean and avoiding the storage of unnecessary images.
*/
add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
function delete_product_images( $post_id )
{
// Continue only for WooCommerce products
if ( 'product' !== get_post_type( $post_id ) ) {
return;
}
// Retrieve the product from WooCommerce using the post ID
$product = wc_get_product( $post_id );
// If the product is not found, terminate the function
if ( !$product ) {
return;
}
// Get the ID of the product's featured image
$featured_image_id = $product->get_image_id();
// Get the IDs of the product's gallery images
$image_galleries_id = $product->get_gallery_image_ids();
// Delete the featured image if it exists
if( !empty( $featured_image_id ) ) {
wp_delete_post( $featured_image_id, true ); // Delete the image from WordPress
}
// Delete the gallery images if they exist
if( !empty( $image_galleries_id ) ) {
foreach( $image_galleries_id as $single_image_id ) {
wp_delete_post( $single_image_id, true ); // Delete each image from the gallery
}
}
}
/*
End: This function is hooked to 'before_delete_post', so it executes before a post is deleted.
The use of this function ensures that the images associated with products are automatically deleted when
the product is deleted, keeping the system free of unnecessary images.
*/
/*
Start: This function adds a new column with product thumbnails in the "My Orders" table on the "My Account" page in WooCommerce.
It moves the "Order Number" column and adds the new column before it, so the product thumbnails appear next to the order number.
*/
add_filter( 'woocommerce_my_account_my_orders_columns', 'filter_woocommerce_my_account_my_orders_columns', 10, 1 );
function filter_woocommerce_my_account_my_orders_columns( $columns ) {
// Retain the "Order Number" column in the new layout
$new_column = array( 'order-number' => $columns['order-number']);
// Remove the original "Order Number" column
unset($columns['order-number']);
// Add a new column for product thumbnails
$new_column['order-thumbnails'] = '';
// Return the new column layout
return array_merge($new_column, $columns);
}
add_action( 'woocommerce_my_account_my_orders_column_order-thumbnails', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
/*
This function displays the product thumbnails in the "order-thumbnails" column we created.
For each product in the order, the function retrieves the product object and its thumbnail,
which is then displayed in the column.
*/
// Loop through each product in the order
foreach( $order->get_items() as $item ) {
// Get the product object (WC_Product) from the order item
$product = $item->get_product();
// Continue only when a valid product exists
if ( !$product ) {
continue;
}
// Get the product's thumbnail with dimensions 36x36
$thumbnail = $product->get_image(array( 36, 36));
// Check if the product has an image and display the thumbnail
if( $product->get_image_id() > 0 ) {
echo $thumbnail . ' ' ; // Display the thumbnail
}
}
}
/*
End: The first function creates a new column for product thumbnails in the "My Orders" table on the "My Account" page,
while the second function fills this column with product images.
With this change, users can see thumbnails of the products they have ordered next to their order number.
*/
/*
Start: This function automates the completion of orders that contain only virtual products.
Typically, orders go through the "processing" stage first, but for virtual products, which do not require physical shipping,
the order can be automatically marked as "Completed".
*/
function auto_complete_virtual_orders( $payment_complete_status, $order_id, $order ) {
$current_status = $order->get_status(); // Retrieve the current status of the order
// We want to update the status only if the order is in one of the allowed statuses
$allowed_current_statuses = array( 'on-hold', 'pending', 'failed' );
// Check if the payment status is "processing" and the current status is allowed
if ( 'processing' === $payment_complete_status && in_array( $current_status, $allowed_current_statuses ) ) {
$order_items = $order->get_items(); // Retrieve the items in the order
// Create an array of the products in the order
$order_products = array_filter( array_map( function( $item ) {
// Retrieve the product associated with each order item
return $item->get_product();
}, $order_items ), function( $product ) {
// Filter out anything that is not a product
return !! $product;
} );
// Check if the order contains products
if ( count( $order_products ) > 0 ) {
// Check if all products in the order are "virtual"
$is_virtual_order = array_reduce( $order_products, function( $virtual_order_so_far, $product ) {
// If the product is virtual, continue checking
return $virtual_order_so_far && $product->is_virtual();
}, true );
// If all products are virtual, update the order status to "Completed"
if ( $is_virtual_order ) {
$payment_complete_status = 'completed';
}
}
}
return $payment_complete_status; // Return the updated payment status
}
add_filter( 'woocommerce_payment_complete_order_status', 'auto_complete_virtual_orders', 10, 3 );
function evolution_add_product_sku_to_order_items( $item_name, $item, $is_visible ) {
$product = $item->get_product();
if ( !$product ) {
return $item_name;
}
$sku = $product->get_sku();
if ( !empty( $sku ) ) {
$item_name .= sprintf( ' <small>(SKU: %s)</small>', esc_html( $sku ) );
}
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'evolution_add_product_sku_to_order_items', 10, 3 );
function evolution_add_low_stock_badge_to_loop() {
global $product;
if ( !$product || !$product->managing_stock() || !$product->is_in_stock() ) {
return;
}
$stock_quantity = $product->get_stock_quantity();
if ( null !== $stock_quantity && $stock_quantity <= 3 ) {
echo '<span class="woocommerce-badge woocommerce-badge-low-stock">' . esc_html__( 'Limited stock', 'woocommerce' ) . '</span>';
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'evolution_add_low_stock_badge_to_loop', 11 );
/*
End: This function simplifies the management of orders with virtual products by automating the completion process
when the conditions are met. This allows the order to be marked as "Completed" without manual intervention,
as long as the products are virtual and do not require shipping or physical delivery.
*/
/*
Start: This function optimizes the loading of WooCommerce scripts and styles.
It removes the WooCommerce styles and scripts from pages that are not related to WooCommerce
(such as product, cart, checkout, or account pages), thus improving the performance of the website.
*/
if ( !function_exists( 'evolution_manage_woocommerce_styles' ) ) :
/**
* Optimizes WooCommerce scripts
* Removes WooCommerce styles and scripts from pages not related to WooCommerce
*/
function evolution_manage_woocommerce_styles() {
// First, check if WooCommerce is installed to avoid errors
if ( function_exists( 'is_woocommerce' ) ) {
// Removes the styles and scripts if the page is not a WooCommerce page (not product, cart, checkout, or account)
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {
@@ -395,26 +442,304 @@ function evolution_hide_price_add_cart_not_logged_in() {
}
}
// Add function to init hook
add_action('init', 'evolution_hide_price_add_cart_not_logged_in');
/**
* Displays a login link with a message
*/
function evolution_print_login_to_see() {
echo '<p>' .
__('Please log in to see the prices.', 'theme_name') .
' <a href="' . esc_url( get_permalink(wc_get_page_id('myaccount')) ) . '">' .
__('Log in here', 'theme_name') .
'</a></p>';
}
endif;
/*
End: This function helps to hide product prices and the "Add to Cart" button for non-logged-in users.
It ensures that only logged-in users can view the prices and add products to their cart. If a user is not logged in,
a message with a link to the login page is shown. This can improve user experience by prompting users to log in
to access the store's features.
*/
/*
Start: This function displays a free-shipping progress notice in the cart.
It calculates the remaining amount needed to reach the free-shipping minimum for the active shipping zone.
*/
function evolution_add_remaining_amount_for_free_shipping_notice() {
if ( !function_exists( 'WC' ) || !WC()->cart ) {
return;
}
$packages = WC()->cart->get_shipping_packages();
if ( empty( $packages ) ) {
return;
}
$package = reset( $packages );
$zone = wc_get_shipping_zone( $package );
$methods = $zone ? $zone->get_shipping_methods( true ) : array();
$minimum = 0;
foreach ( $methods as $method ) {
if ( 'free_shipping' === $method->id && 'yes' === $method->enabled ) {
$minimum = (float) $method->min_amount;
break;
}
}
if ( $minimum <= 0 ) {
return;
}
$subtotal = (float) WC()->cart->get_displayed_subtotal();
$remaining = $minimum - $subtotal;
if ( $remaining > 0 ) {
$message = sprintf( __( 'Add %s more for free shipping.', 'woocommerce' ), wc_price( $remaining ) );
if ( !wc_has_notice( $message, 'notice' ) ) {
wc_print_notice( $message, 'notice' );
}
}
}
add_action( 'woocommerce_before_cart', 'evolution_add_remaining_amount_for_free_shipping_notice' );
/*
End: This function helps customers understand how much more they need to spend
to unlock free shipping directly from the cart page.
*/
/*
Start: This function validates checkout requirements for heavy-item orders.
If heavy products exist in the cart, billing phone becomes mandatory.
*/
function evolution_block_checkout_for_heavy_items_without_phone( $data, $errors ) {
if ( !is_checkout() || !function_exists( 'WC' ) || !WC()->cart ) {
return;
}
$has_heavy_item = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( !empty( $cart_item['data'] ) && $cart_item['data']->has_weight() && (float) $cart_item['data']->get_weight() >= 20 ) {
$has_heavy_item = true;
break;
}
}
if ( $has_heavy_item && empty( $data['billing_phone'] ) && is_a( $errors, 'WP_Error' ) ) {
$errors->add( 'billing_phone_required_for_heavy_items', __( 'A phone number is required when your order includes heavy items.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_after_checkout_validation', 'evolution_block_checkout_for_heavy_items_without_phone', 10, 2 );
/*
End: This function improves delivery reliability by forcing phone collection
when the order includes heavy products.
*/
/*
Start: This function stores the customer order sequence number into order meta.
Example: first order = 1, second order = 2, etc.
*/
function evolution_add_customer_order_sequence_meta( $order_id ) {
$order = wc_get_order( $order_id );
if ( !$order ) {
return;
}
$customer_id = $order->get_customer_id();
if ( !$customer_id ) {
return;
}
$customer_orders_count = wc_get_customer_order_count( $customer_id );
if ( $customer_orders_count > 0 ) {
$order->update_meta_data( '_customer_order_sequence', (int) $customer_orders_count );
$order->save();
}
}
add_action( 'woocommerce_checkout_order_processed', 'evolution_add_customer_order_sequence_meta', 20, 1 );
/*
End: This function enriches order metadata with the customer's purchase sequence,
useful for loyalty workflows and admin insights.
*/
/*
Start: This function displays the stored customer order sequence
in the WooCommerce admin order details screen.
*/
function evolution_show_customer_order_sequence_in_admin( $order ) {
if ( !is_a( $order, 'WC_Order' ) ) {
return;
}
$sequence = $order->get_meta( '_customer_order_sequence' );
if ( !empty( $sequence ) ) {
echo '<p><strong>' . esc_html__( 'Customer Order Number:', 'woocommerce' ) . '</strong> #' . esc_html( $sequence ) . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'evolution_show_customer_order_sequence_in_admin', 20 );
/*
End: This function helps administrators quickly identify whether
an order belongs to a customer's first, second, or later purchase.
*/
/*
Start: This function adds a backorder handling fee based on quantity.
It applies extra operational cost when items are purchased on backorder.
*/
function evolution_add_backorder_risk_fee( $cart ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
return;
}
if ( !is_a( $cart, 'WC_Cart' ) ) {
return;
}
$backorder_count = 0;
foreach ( $cart->get_cart() as $cart_item ) {
if ( !empty( $cart_item['data'] ) && $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$backorder_count += (int) $cart_item['quantity'];
}
}
if ( $backorder_count > 0 ) {
$cart->add_fee( __( 'Backorder handling fee', 'woocommerce' ), 2.5 * $backorder_count, false );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'evolution_add_backorder_risk_fee', 20, 1 );
/*
End: This function introduces dynamic fee control to reflect backorder risk
and fulfillment overhead in the cart totals.
*/
/*
Start: This function sorts related products by stock status.
In-stock products are prioritized while preserving original order when equal.
*/
function evolution_sort_related_products_by_stock_status( $related_posts, $product_id, $args ) {
if ( empty( $related_posts ) ) {
return $related_posts;
}
$indexed_posts = array_values( $related_posts );
usort( $indexed_posts, function( $a, $b ) use ( $related_posts ) {
$product_a = wc_get_product( $a );
$product_b = wc_get_product( $b );
$score_a = $product_a && $product_a->is_in_stock() ? 1 : 0;
$score_b = $product_b && $product_b->is_in_stock() ? 1 : 0;
if ( $score_a === $score_b ) {
return array_search( $a, $related_posts, true ) <=> array_search( $b, $related_posts, true );
}
return $score_b <=> $score_a;
} );
return $indexed_posts;
}
add_filter( 'woocommerce_related_products', 'evolution_sort_related_products_by_stock_status', 10, 3 );
/*
End: This function improves product recommendation quality by showing
available related items first.
*/
/*
Start: This function calculates and displays total shipment weight in order emails.
It helps both store admins and customers understand shipping weight context.
*/
function evolution_add_order_weight_to_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( !is_a( $order, 'WC_Order' ) ) {
return;
}
$total_weight = 0;
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
if ( $product && $product->has_weight() ) {
$total_weight += (float) $product->get_weight() * (int) $item->get_quantity();
}
}
if ( $total_weight > 0 ) {
echo '<p><strong>' . esc_html__( 'Estimated Shipment Weight:', 'woocommerce' ) . '</strong> ' . esc_html( wc_format_weight( $total_weight ) ) . '</p>';
}
}
add_action( 'woocommerce_email_after_order_table', 'evolution_add_order_weight_to_emails', 20, 4 );
/*
End: This function improves communication around shipment characteristics
by exposing the computed order weight in transactional emails.
*/
/*
Start: This function auto-tags high-value customers using user meta.
The tag is assigned when total customer spend reaches the defined threshold.
*/
function evolution_auto_tag_high_value_customers( $order_id ) {
$order = wc_get_order( $order_id );
if ( !$order || !$order->get_customer_id() ) {
return;
}
$customer_id = $order->get_customer_id();
$total_spent = (float) wc_get_customer_total_spent( $customer_id );
if ( $total_spent >= 1500 ) {
update_user_meta( $customer_id, '_is_high_value_customer', 'yes' );
}
}
add_action( 'woocommerce_order_status_completed', 'evolution_auto_tag_high_value_customers', 20 );
/*
End: This function supports customer segmentation by automatically marking
top-spending users for VIP workflows and targeted campaigns.
*/
?>