Posted on

How to Edit and Disable Checkout Fields in Woocommerce

This is just a quick post that shows you how to edit or disable the checkout fields in Woothemes WordPress shopping cart Woocommerce. It may make all the Magento developers jealous to see how nearly trivial it is – but I couldn’t see a good blog post explaining it, so here goes.

Disabling and Editing Checkout

To use the WordPress API, I made a simple little plugin that listens for the filter event and then makes some changes to the arrays.

The anatomy of a plugin is really just a class definition, in the constructor you listen for events and bind them to functions of that class. In those functions, you do the real grunt work. In this case, that’s where I weed out disabled fields, and swap in my edited field definitions.

Hopefully the way the code is laid out makes the actual editing/disabling is performed self-evident. To see the full range of available fields and options, please see the underlying array declaration in the core file woocommerce/classes/checkout.class.php.

My class looks like this:

class woocommerce_disable_checkout_fields {
 
	var $update_billing;
	var $disabled_billing;
	var $disabled_shipping;
	var $update_shipping;
 
	public function __construct() { 
 
		// If you do not have shipping on checkout, then only billing will have an effect
		$this->disabled_shipping = array('shipping_last_name');
		$this->update_shipping = array();
 
		$this->disabled_billing = array('billing_last_name', 'billing_address_1', 'billing_address_2', 'billing_city', 
						'billing_postcode', 'billing_country', 'billing_state');
		$this->update_billing = array(
			'billing_first_name' 	=> array(
				'name'=>	'billing_first_name',
				'label'                 => __('Name','wc_disable_checkout_fields'),
				'placeholder'  		=> __('Name','wc_disable_checkout_fields'),
				'required'              => true,
				'class'                 => array('form-row-first')
				),
			'billing_company'         => array(
				'label'                 => __('Company','wc_disable_checkout_fields'),
				'placeholder'  		=> __('Company','wc_disable_checkout_fields'),
				'required'              => false,
				'class'                 => array('form-row-first')
				),
			'billing_email' 	=> array(
				'label'                 => __('Email','wc_disable_checkout_fields'),
				'placeholder'   	=> __('you@yourdomain.com','wc_disable_checkout_fields'),
				'required'              => true,
				'class'                 => array('form-row-first')
				),
			'billing_phone'         => array(
				'label'                 => __('Phone','wc_disable_checkout_fields'),
				'placeholder'   	=> __('Phone number','wc_disable_checkout_fields'),
				'required'              => true,
				'class'                 => array('form-row-first')
				)
			);
 
		// Filters for checkout actions
		add_filter( 'woocommerce_shipping_fields', array(&$this, 'filter_shipping'), 10, 1 );
		add_filter( 'woocommerce_billing_fields', array(&$this, 'filter_billing'), 10, 1 );
	} 
 
 
	// array_flip is a somewhat smelly way to make a normal array into an associative one
	function filter_shipping( $fields_array ) {
		$fields_array = array_replace($fields_array, $this->update_shipping);
		return array_diff_key($fields_array, array_flip($this->disabled_shipping));
	}
 
	function filter_billing( $fields_array ) {
		$fields_array = array_replace($fields_array, $this->update_billing);
		return array_diff_key($fields_array, array_flip($this->disabled_billing));
	}
 
 
}

This disables most of the billing fields, and edits a couple of others. It doesn’t touch shipping because on the site I’m building this for, the products are all digitally delivered. It looks like this (with a few CSS tweaks):

To complete the plugin, at the end of your class definition, you can just instantiate your plugin class to kick things off.

$woocommerce_disable_checkout_fields = new woocommerce_disable_checkout_fields();

That’s all there is to it.

Download

You can download the file here and drop it into your wordpress install. It’s provided as is with no promise of any maintenance!

Update 0.2: Moved the variable initialization to the constructor so that inline translation can be used if desired.

Ease of Modification

Note how much easier the learning curve for WordPress+Woocommerce is, compared to Magento. Within an hour I have managed to edit the checkout fields, and publish some code that I’m pretty sure will make it really easy to do this in a maintainable and upgradeable way.

One of the reasons it’s so easy is the filter API for WordPress. This innocuous little comment in the Woocommerce code was perfect:

// Define shipping fields in an array. This can be hooked into and filtered if you wish to change/add anything.

It gives all the hinting required to lead you to make changes the right way – although it’s tempting just to hack the core code, my experience has been that’s a very bad idea.

Admittedly my code is not a proper UI-based plugin yet, but if there’s enough interest, I’ll turn it into one (so comment below if you’d like a plugin for this).

Lastly, this is my first public foray into Woocommerce, so if I’m doing it wrong, let me know, I’ll save further embarrassment!

79 thoughts on “How to Edit and Disable Checkout Fields in Woocommerce

  1. Hy!
    Great, just what I was looking for.
    I tried to add a translation for the fields but I get this error:
    Parse error: syntax error, unexpected ‘(‘, expecting ‘)’ … wp-content\plugins\disable-checkout-fields.php on line 39:
    ‘label’ => __(‘Name’, ‘woothemes’),

    Please any idea!
    Thank you!

  2. Hi Razvan.

    The __() is a function call, so if you want to do any inline translation you’ll have to move it to the constructor, rather than as part of the variable declaration. I’ve updated the code to show this, as using translations is a cleaner way of doing it.

  3. Thank you very much..it works like a charm!

  4. My big challenge now is to have two sets of fields, one for individual billing (Name, ID number, etc) and one for legal entity (Company, Trade Register Number, etc) and to be able to hide or show this fields but to disable the validation for the hidden fields.
    So far you have done a great job for me, a designer 🙂

  5. Another problem is that on the Orders Section (Admin) the billing and shipping info it does not get replaced with the new/updated fields.

  6. I been working with Magento for about a year now, and I can’t stand how unweildly it is to work with. Is it just me? What’s the future for Magento?

  7. This is Exactly what i am looking for, thanks for sharing this.

  8. When I add this as a plugin and activate it, I get this when I get to checkout:

    Fatal error: Call to undefined function array_replace() in /home/nmk/public_html/ghostswithshitjobs/wp-content/plugins/woocommerce-aschroder-customize-checkout/disable-checkout-fields.php on line 82

    It’s a virtual downloadable product, if that makes a difference.

  9. Hi, Jim

    Google that error, there are several solutions – it is to do with PHP version 5.3 vs 5.2. Here’s one of the top hits which describes the problem and a solution: https://github.com/fabpot/Twig/issues/420

    Also, you should check out this blog post, regarding for help and trying to solve it first:
    http://mattgemmell.com/2008/12/08/what-have-you-tried/

    Thanks,
    Ashley

  10. Thank you for the informative post, this is just what I am looking for but i am definitly green.

    You wrote:>>>
    To complete the plugin, at the end of your class definition, you can just instantiate your plugin class to kick things off.

    $woocommerce_disable_checkout_fields = new woocommerce_disable_checkout_fields();

    What does this mean? Where would I put this line of code?

  11. Thank you for the amazing post I didn’t understand that much cause Im a beginner .
    I was wondering what is the code for adding a delivery date option in the checkout page and where should I paste it ?

    Thank you SO MUCH !

  12. THANK YOU! I’m doing my best to use this, but I would love to see a plugin for modifying WooCommerce checkout fields.

  13. Thanks Bro! Works a treat. I had to change all array_replace to array_merge and, then updated the fields I wanted to show and everything does exactly what I wished for. Perfect!

  14. Hey Karl, by love do you mean you’d buy one?

  15. Hi, I’m wondering about the same thing as Sheila. I’ve installed your plugin but I do not know where I am supposed to paste

    $woocommerce_disable_checkout_fields = new woocommerce_disable_checkout_fields();

    There is no core file named woocommerce/classes/checkout.class.php in my woocommerce but there is a file called woocommerce/classes/class-wc-checkout.php

    Is that the same? If so, where in that file do I paste your code?

    I tried pasting it below the definition, i.e. around line 63 in the class-wc-checkout.php file but to no avail. The checkout page is still blank.

    Any help would be appreciated. Maybe some of the readers know where the code is to be pasted?

  16. Would be great if you turned this into a UI based drag/drop addon for Woocommerce. It would sell very well I bet. Thanks for tutorial.

  17. Anyone got any example code that allows you to add fields and other types of fields in to your billing fields?

    I’ve tried adding this to the array for a field, with no luck:

    ‘type’ => ‘select’,
    ‘options’ => array(
    ‘Option 1’ => ‘Option 1’,
    ‘Option 2’ => ‘Option 2’
    )

  18. It didnt work for me. After installing the plugin, the whole checkout page went blank. Its easier to just go to the countries class and // comment out the field you dont want to add in the check out at the bottom of the file.

  19. it’s worked for me but when i click the place order button i get a list of errors
    Notice: Undefined index: billing_country in D:\x\htdocs\wpwc\wp-content\plugins\woocommerce\classes\class-wc-checkout.php on line 176

    Notice: Undefined index: billing_state in D:\x\htdocs\wpwc\wp-content\plugins\woocommerce\classes\class-wc-checkout.php on line 177
    .
    .
    .

  20. Dude! just what i was looking for. But can I remove the phone number 🙂

  21. This plug is was working for me until the last woocommerce upgrade. I am experiencing the same as Lafanter where the screen is blank – just the header “Checkout” is displaying. I tried uninstalling and reinstalling the plugin again to no avail. Anyone know what might have changed that needs to be upgraded in the plug?

  22. yes Sheila you are right it is not working with lates woocommerce upgraded that is 1.5.2
    if you upgrade the woocommerece you have to find out the other option may be customize the latest plugin but i think if your work is fine then why you would update the plugin it is not good work if you updates the plugin with out taking the back up of all including files and DB

    THanks

    Best Regards
    Huma Naseem

  23. Hello Ashley !
    please help me what can i do now and please tell us all what sould we do with new updated version of woocommerece
    thanks a lot for this kindness

  24. This seems to work with the latest updated WooCommerce v1.5.2.1

    Thank you so much for posting this.

    I teach a class on how to build e-commerce sites for people on social assistance and I’m deep into the process of moving the curriculum from Magento to WordPress + WooCommerce since I first discovered it in October 2011.

    Your plug-in is great. If you’d like help to make the admin side of this plug-in I would love to offer my assistance.

  25. For those of you that are getting a blank page, do what user Endle wrote on Feb 23.
    Look for array_return and replace it with array_merge. There are 2

    function filter_shipping( $fields_array ) {
    $fields_array = array_merge($fields_array, $this->update_shipping);
    return array_diff_key($fields_array, array_flip($this->disabled_shipping));
    }

    function filter_billing( $fields_array ) {
    $fields_array = array_merge($fields_array, $this->update_billing);
    return array_diff_key($fields_array, array_flip($this->disabled_billing));
    }

  26. Is there a way to have an email me button as opposed to add to cart in WooCommerce? I just need to make a site where its possible to add products, but the customer doesnt want any online payments (but still wants to be able to easily add/remove products).So everytime the email me button is pressed I hook in some javascript or something to pop open a popup or something?

  27. Anyone know where to find the index.php file in woocomm? OR if you know how to add a class to the shop’s title – help us all suffering from the woocommingitis.

    Thanks.

  28. PLEASE make this a plugin! I’d certainly pay for it. I understand the logic of the code edits, but whenever I try to hook in I end up with errors, and to be honest I just don’t have the time to mess with it… that’s why good people like yourself make a living! 🙂

  29. Hey, awesome stuff maybe this would solve a little problem with layout in woocommerce I am having because field liek first and last name layout next to each other and i cannot figure how to drop to next line. Maybe i can just do this to shut off those fields from displaying.

    Thank You for spending the time to do this.

  30. Hi,
    I want to add two new field registration form. Using Woocommerce. I tried hard, I could not. Please help ..

  31. Perfect – thanks for this. Yes, I’d pay for this if it were on Code Canyon or something similar. Got to be worth $5 of anyone’s money to be able to customise the checkout forms. Good luck, and thanks again!

  32. I’m trying to use the plugin but I am getting this error:

    Fatal error: Call to undefined function array_replace() in /home/antonv/public_html/3dessentials.com/3dewp/wp-content/plugins/woocommerce-aschroder-customize-checkout/disable-checkout-fields.php on line 82

    Anybody has an idea to why is this happening?

  33. Ashley , this is time saver, adding/removing fields is no longer issue, thanks to your pretty cool class!

  34. This is my 2 minute hack for getting rid of all fields. It seems to work although I haven’t done much testing on it.

    Its not perfect but it might be useful to some.

    /* add to functions.php */

    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    unset($fields[‘billing’]);
    unset($fields[‘shipping’]);
    unset($fields[‘account’]);

    return $fields;
    }

    /* add to style.css ( to not display the warning that the array is empty */

    #customer_details .col-1{display:none}

  35. Ok, after a few days of pain it turns out this was a bad solution.

    I made some assumptions that turned out to be false. You need to keep billing in there because the shop is getting the email address from this form and not from paypal. No emails will be sent to the buyer if you use my above solution.

    I assume you can use the above solution to zero out shipping and account, then use some other solution to alter billing.

    Also, don’t use that css line from above.

    /* add to functions.php */

    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    unset($fields[‘shipping’]);
    unset($fields[‘account’]);

    return $fields;
    }

  36. What I did was changed the shipping to free… Made the payment to cash on delivery/manual payments

    Then disabled the functions that echos these info…

    Customer still have to go through checkout process and send “enquiry” to shop manager but it won’t get charged or anything like that… But still capture all the details necessary for shop manager to contact customer….

  37. I’m trying to use the plugin but I am getting this error:

    Fatal error: Call to undefined function array_replace() in /home/antonv/public_html/3dessentials.com/3dewp/wp-content/plugins/woocommerce-aschroder-customize-checkout/disable-checkout-fields.php on line 82

    Anybody has an idea to why is this happening?

  38. @ shailender
    It’s is to do with PHP version 5.3 vs 5.2.

    if you replace “array_replace” to “array_merge” in disable-checkout-fields.php file it should work.

  39. Hi Ashley, thanks for this tutorial – much appreciated.

    Although, I am using a different cart at the minute, I am whiling to look into this one.

    I have a question; if I simply want customers to pay via Paypal and NOT register at all on my website, is it possible with this plugin and your hack?

    Basically, I don’t need/want people’s information, the email is enough, AND I think that’s good to be able to purchase digital goods without having to register on the site. At the same time, I don’t want to “clog up” the database with passwords and what not…

  40. Hi Ashley,
    I’m a real novice when it comes to making a website and I wouldn’t have a clue about coding so I was wondering if you could point me in the right direction. Basically what I want to do is create an area in the My Account page in woocommerce for customer to submit there dogs name, breed and Birth date. I was wondering if this plugin would assist in doing this? Or is this only for the checkout options? This dog profile also needs to be dynamic so customers can add a second or third etc profiles just in case they have more than one dog.

    Any assistance would be much appreciated.

    Cheers,
    Saylesh

  41. Absolutely love this, exactly what I needed and so easy to modify! Thanks!

  42. Thanks a lot!
    Now I can customize shipping fields for different countries without modifying core Woocommerce code! Awesome!

  43. Thank you very much. Please start making good money with a plugin.

  44. This is exactly what I need too, but I am not a programmer, and can not figure this out on my own. A plugin for people like me would be great. In the mean time, wondering whether I could hire you to just fix this problem for me? Please send me a note and let me know?

    I am ready to switch shopping carts if I can’t fix this. There is no way people want to provide all that personal information just to buy a digital download. What are the woocommerce people thinking?

    Thanks for your help.

    Holly

  45. much easier way to do this with your functions.php file

    // Hook in
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    unset($fields[‘order’][‘order_comments’]);

    return $fields;
    }

  46. i.e to avoid matts problem above, just filter out all the billing fields (disable shipping in woocommerce control panel)

    // Hook in
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    // Our hooked in function – $fields is passed via the filter!
    function custom_override_checkout_fields( $fields ) {
    unset($fields[‘billing’][‘billing_phone’]);
    unset($fields[‘billing’][‘billing_company’]);
    unset($fields[‘billing’][‘billing_address_1’]);
    unset($fields[‘billing’][‘billing_address_2’]);
    unset($fields[‘billing’][‘billing_city’]);
    unset($fields[‘billing’][‘billing_country’]);
    unset($fields[‘billing’][‘billing_state’]);
    unset($fields[‘billing’][‘billing_postcode’]);
    return $fields;
    }

    Hope this helps

  47. Thank you so much for this!

    I knew it could be done with with hooks and filters, but it was really beyond my comfort zone. REALLY appreciate you’re sharing your work with everyone.

    Thanks again!

  48. Thanks for this! Worked great!

    I want a new field (web address), can I just change “Company” to the field name I want? Or do I need to create a new field?

  49. @Emily – you don’t need anyone’s permission, no one will die if it doesn’t work, and you can change it back later. Have fun!

    @Stevie – edit core files at your peril! Upgrading won’t be very fun if you do.

Comments are closed.