AI Verified

Name

Add Discount Selection to WordPress User

About

This code add a discount field to the user profile in Wordpress

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Add to your functions.php file.

Codevault

WTD01

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

20/08/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Add Discount Selection to WordPress User

 
                    
1/**
2 * Adds a custom field (dropdown) for discounts in user profiles.
3 */
4function cwpai_add_user_discount_field($user) {
5 // Check if $user is an object before trying to access its properties
6 $user_id = is_object($user) ? $user->ID : get_current_user_id();
7 
8 // Get the current selected value
9 $selected = get_user_meta($user_id, 'cwpai_user_discount', true);
10 
11 echo '<h3>' . __('Discount Settings', 'codewp') . '</h3>';
12 echo '<table class="form-table"><tr>';
13 echo '<th><label for="cwpai_user_discount">' . __('Discount Rate', 'codewp') . '</label></th>';
14 echo '<td>';
15 echo '<select id="cwpai_user_discount" name="cwpai_user_discount" class="regular-text">';
16 
17 // Define discount options
18 $discounts = array('40%', '30%', '20%', '10%');
19 foreach ($discounts as $discount) {
20 echo '<option value="' . esc_attr($discount) . '"' . selected($selected, $discount, false) . '>' . esc_html($discount) . '</option>';
21 }
22 
23 echo '</select>';
24 echo '</td></tr></table>';
25}
26add_action('show_user_profile', 'cwpai_add_user_discount_field');
27add_action('edit_user_profile', 'cwpai_add_user_discount_field');
28add_action('woocommerce_edit_account_form', 'cwpai_add_user_discount_field');
29 
30/**
31 * Saves the selected discount rate when the profile is updated.
32 */
33function cwpai_save_user_discount_field($user_id) {
34 if (!current_user_can('edit_user', $user_id)) {
35 return false;
36 }
37 update_user_meta($user_id, 'cwpai_user_discount', $_POST['cwpai_user_discount']);
38 return true;
39}
40add_action('personal_options_update', 'cwpai_save_user_discount_field');
41add_action('edit_user_profile_update', 'cwpai_save_user_discount_field');
42add_action('woocommerce_save_account_details', 'cwpai_save_user_discount_field');
43 
44/**
45 * Applies the selected discount for the user viewing WooCommerce products.
46 */
47function cwpai_apply_user_discount_to_products($price, $product) {
48 $user_id = get_current_user_id();
49 $user_discount = get_user_meta($user_id, 'cwpai_user_discount', true);
50 
51 if (!empty($user_discount)) {
52 $discount_decimal = rtrim($user_discount, '%') / 100;
53 $price -= ($price * $discount_decimal);
54 }
55 
56 return $price;
57}
58add_filter('woocommerce_product_get_price', 'cwpai_apply_user_discount_to_products', 10, 2);
59add_filter('woocommerce_product_variation_get_price', 'cwpai_apply_user_discount_to_products', 10, 2);

0

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

1

Hide WP Admin Bar For Non-Admins

Added: 1 year ago

Last Updated: 1 year ago

Hide WP Admin Bar For Non-Admins

WordPress Admin

AI Verified

0

CLOUD Meister: Hide PlugIns from List

Added: 1 year ago

Last Updated: 1 year ago

WordPress Admin

AI Verified

0

Hide ‘Screen Options’ Tab

Added: 1 year ago

Last Updated: 1 year ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

0

Add Discount Selection to WordPress User

Added: 4 weeks ago

Last Updated: 4 weeks ago

This code add a discount field to the user profile in Wordpress