WooCommerce: Add a WordPress Customizer Setting

If you’ve taken the ConfigureWoo online course, you’ll know that WooCommerce adds its own “WordPress Customizer” section called “WooCommerce”, indeed. From there you can manage some settings such as the store notice, product images, checkout fields and so on.

What if you want to add your own settings, and let yourself or your client toggle options or type input values, so that this can be applied to your current WooCommerce website? Well, that’s quite doable, so in this post we’ll see how they do it. Enjoy!

If you go to WordPress Dashboard > Appearance > Customize you will see the “WordPress Customizer”. By default, WooCommerce plugin adds its own “WooCommerce” section to the Customizer, and from there you can manage some settings.

PHP Snippet: Add a WordPress Customizer WooCommerce Custom Setting

In this example, we will be adding a checkbox to the “WooCommerce” > “Checkout” WordPress Customizer existing section, which will switch between 2 columns checkout layout (default for some WooCommerce themes such as Storefront, 2021, etc.) and a 1 column checkout layout (already the normal behavior for mobiles, so billing is followed by shipping and finally by order review, but we want here to get the same on desktops as well).

As you can see inside the snippet there are 3 parts:

  1. add_setting(): this WordPress function creates the option
  2. add_control(): this WordPress function controls the option, tells WP it’s a checkbox and in which section should show (woocommerce_checkout in the example below)
  3. conditional CSS: this is CSS that only triggers if the option is enabled (checkbox checked in this case)
/**
 * @snippet       WordPress Customizer Setting - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'customize_register', 'bbloomer_woocommerce_wordpress_customizer_css_option' );

function bbloomer_woocommerce_wordpress_customizer_css_option( $wp_customize ) {
	$wp_customize->add_setting( 'woocommerce_checkout_switch_col_layout', array(
		'default' => 'no',
		'type' => 'option',
		'capability' => 'manage_woocommerce',
		'sanitize_callback' => 'wc_bool_to_string',
		'sanitize_js_callback' => 'wc_string_to_bool',
	));
	$wp_customize->add_control( 'woocommerce_checkout_switch_col_layout', array(
		'label' => 'Switch layout',
		'description' => 'Optionally switch to a 1-column checkout on desktops',
		'section' => 'woocommerce_checkout',
		'settings' => 'woocommerce_checkout_switch_col_layout',
		'type' => 'checkbox',
	));
}

add_action( 'wp_head', 'bbloomer_switch_checkout_cols_css' );

function bbloomer_switch_checkout_cols_css() {
	if ( true === wc_string_to_bool( get_option( 'woocommerce_checkout_switch_col_layout', 'yes' ) ) ) {
	?>
	<style type="text/css">
		.woocommerce-checkout form .col2-set, .woocommerce-checkout form #order_review, .woocommerce-checkout form #order_review_heading {
			width: 100%;
			float: none;
			clear: none;
			padding-left: 0;
			padding-right: 0;
		}
	</style>
	<?php
	}
}


Before & After Screenshots

Before: on desktop my theme shows a 2 columns checkout.
After: once my custom setting (a checkbox) is checked, my checkout layout on desktop goes onto 1 column.

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *