AI Verified

Name

Deactivate Some Woo Checkout Fields

About

An admin interface for deactivating checkout fields. Sub Menu item can be found in your Woo menu :)

Language

PHP

Rating

Voted: 1 by 1 user(s)

Codevault

Super-Snippet-Storage

Scroll down to see more snippets from this codevault.

Wordpress Compatability

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

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:

17/01/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:

Potential vulnerability found : Cross Site Scripting
Found on line : 125
Code : echo("<h2>" . ucfirst($section)
Vulnerable line : 51
Code : $fields = WC()

Found 1 vulnerabilities

Deactivate Some Woo Checkout Fields

 
                    
1/**
2 * Plugin Name: Checkout Fields Customizer
3 * Author: Mark Harris & His AI Friends
4 * Author URI: https://www.christchurchwebsolutions.co.uk/
5 * Description: This plugin allows customization of WooCommerce checkout fields directly from the WordPress admin area. It introduces a submenu under the WooCommerce menu for managing the visibility of checkout fields. The plugin offers the following functionalities:
6 *
7 * 1. Admin Menu Integration: Adds a new submenu under 'WooCommerce' for 'Checkout Fields'. This submenu provides an interface to manage the checkout fields.
8 *
9 * 2. Settings Registration: Registers settings for the 'Checkout Fields' options, allowing administrators to configure which fields to display at checkout.
10 *
11 * 3. Settings Sanitization: Ensures all input data is sanitized before being saved, maintaining the security and integrity of the data.
12 *
13 * 4. Dynamic Settings Page: Generates a dynamic settings page in the WordPress admin, listing all available checkout fields with checkboxes to enable or disable them.
14 *
15 * 5. Checkout Fields Customization: Based on the saved settings, modifies the checkout fields on the front-end, hiding or showing them as configured.
16 *
17 * 6. Admin-Area Exclusion: Ensures that the checkout fields remain unchanged in the admin area, applying changes only on the front-end.
18 *
19 * Note: This plugin requires WooCommerce to be installed and activated as it directly interacts with WooCommerce's checkout fields.
20 */
21 
22// Register the sub-menu under WooCommerce menu
23add_action("admin_menu", "wpturbo_register_submenu_page");
24function wpturbo_register_submenu_page()
25{
26 add_submenu_page(
27 "woocommerce",
28 "Checkout Fields",
29 "Checkout Fields",
30 "manage_options",
31 "wpturbo-checkout-fields",
32 "wpturbo_checkout_fields_options_page"
33 );
34}
35 
36// Register settings
37add_action("admin_init", "wpturbo_register_settings");
38function wpturbo_register_settings()
39{
40 register_setting(
41 "wpturbo_checkout_fields",
42 "wpturbo_checkout_fields_options",
43 "wpturbo_checkout_fields_sanitize"
44 );
45}
46 
47// Sanitize the input before saving
48function wpturbo_checkout_fields_sanitize($input)
49{
50 // Ensure all expected options are present in the array by checking against the default fields
51 $fields = WC()
52 ->checkout()
53 ->get_checkout_fields();
54 $new_input = [];
55 foreach ($fields as $section => $field_group) {
56 foreach ($field_group as $key => $field) {
57 // If the key exists in our submitted input, use that value, otherwise, this field is not hidden
58 $new_input[$key] = isset($input[$key]) ? "1" : "0";
59 }
60 }
61 return $new_input;
62}
63 
64// The settings page content
65function wpturbo_checkout_fields_options_page()
66{
67 if (!current_user_can("manage_options")) {
68 return;
69 }
70 
71 if (isset($_GET["settings-updated"])) {
72 add_settings_error(
73 "wpturbo_messages",
74 "wpturbo_message",
75 __("Settings Saved", "wpturbo"),
76 "updated"
77 );
78 }
79 
80 settings_errors("wpturbo_messages");
81 $options = get_option("wpturbo_checkout_fields_options", []);
82 ?>
83 <div class="wrap">
84 <h1><?php esc_html_e("Checkout Fields", "wpturbo"); ?></h1>
85 <form action="options.php" method="post">
86 <?php
87 settings_fields("wpturbo_checkout_fields");
88 do_settings_sections("wpturbo_checkout_fields");
89 $fields = WC()
90 ->checkout()
91 ->get_checkout_fields();
92 
93 // Inline CSS for styling the settings page
94 echo '<style>
95 .wpturbo-checkout-fields-wrapper {
96 background-color: #f7f7f7;
97 padding: 20px;
98 border-radius: 5px;
99 box-shadow: 0 1px 3px rgba(0,0,0,0.1);
100 }
101 .wpturbo-checkout-fields-wrapper h2 {
102 color: #333;
103 border-bottom: 1px solid #ddd;
104 padding-bottom: 5px;
105 }
106 .wpturbo-checkout-fields-wrapper label {
107 display: block;
108 margin: 10px 0;
109 line-height: 1.6;
110 }
111 .wpturbo-checkout-fields-wrapper input[type="checkbox"] {
112 margin-right: 10px;
113 }
114 .form-table th {
115 width: auto;
116 padding-right: 20px;
117 }
118 </style>';
119 
120 echo '<div class="wpturbo-checkout-fields-wrapper">';
121 foreach ($fields as $section => $field_group) {
122 // Skip the account fields section
123 if ($section === "account") {
124 continue;
125 }
126 echo "<h2>" . ucfirst($section) . " Fields</h2>";
127 foreach ($field_group as $key => $field) {
128 // The checkbox is checked if the option is set to "1", meaning the field is disabled.
129 $is_disabled =
130 isset($options[$key]) && $options[$key] === "1";
131 $checked = checked($is_disabled, true, false);
132 echo "<label>";
133 echo '<input type="checkbox" name="wpturbo_checkout_fields_options[' .
134 esc_attr($key) .
135 ']" ' .
136 $checked .
137 ">";
138 echo esc_html($field["label"]);
139 echo "</label>";
140 }
141 }
142 echo "</div>";
143 
144 submit_button();?>
145 </form>
146 </div>
147 <?php
148}
149 
150// Override checkout fields based on saved options
151add_filter(
152 "woocommerce_checkout_fields",
153 "wpturbo_custom_override_checkout_fields"
154);
155function wpturbo_custom_override_checkout_fields($fields)
156{
157 if (is_admin()) {
158 // If we are in the admin area, do not modify the fields
159 return $fields;
160 }
161 
162 $options = get_option("wpturbo_checkout_fields_options", []);
163 
164 foreach ($fields as $section => $field_group) {
165 foreach ($field_group as $key => $field) {
166 // If the option is set to "1", the field should be unset (hidden) on the front-end.
167 if (!empty($options[$key])) {
168 unset($fields[$section][$key]);
169 }
170 }
171 }
172 return $fields;
173}

1

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Migrate BasePress -> BetterDocs: 3. Feedback

Added: 11 months ago

Last Updated: 11 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...

General

AI Verified

0

Hide Categories

Added: 9 months ago

Last Updated: 9 months ago

General

Unverified

0

GA 4 - Header

Added: 1 year ago

Last Updated: 1 year ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

21

Convert To WebP

Added: 9 months ago

Last Updated: 4 days ago

<p>Snippet to convert JPG / PNG / Gif to WebP automatically on upload. Used GD or ImageMagick</p>

WordPress Admin

AI Verified

5

Really Simple Duplications

Added: 9 months ago

Last Updated: 7 months ago

A snippet to duplicate posts and pages and CPTS.

General

AI Verified

2

WP-Admin ChatGPT

Added: 6 months ago

Last Updated: 4 months ago