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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 4.6
 * @community     https://businessbloomer.com/club/
 */

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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

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. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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