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!
i am trying to develop a shipping plugin.. how can i grab this shipping address so i can use in my shipping plugin?
like for example i need to match the city field with the required city field for my api..
can you help?
Hi, I try to use your plugin with multilanguage translator for woocommerce WPML.
So when your plugin is loaded it doesn’t see current locale. It things that we use en-US locale.
What should I do to change order of a loading of plugins?
How can I make “friendship” beetwen your plugin and WPLM?
Or can you advice me another way to combime your plugin and WPLM.
Thank you!
Look forward to your reply!
Just wanted to say thanks for this post, been looking for this solution for a while 🙂
Im getting error like this: Fatal error: Call to undefined function array_replace() in /home/xxx/domains/xxx/public_html/wp-content/plugins/woocommerce-aschroder-customize-checkout/disable-checkout-fields.php on line 82
I have some trouble at woocommerce themes, when i’m edited my checkout class, some error be happen at woocommerce on checkout class, at line 12. Can you help me sir?
Can you please elaborate this?
——————————-
// 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;
}
————————–
I added the above code to my wp-content/themes/{theme-name}/function.php
I added on the last line and when I refresh my checkout page, nothing happens?
Do I need to install the plugin first?
Please help. Thanks
Hi,
Thanks for the effort! I vote for this to become a plugin.
There is this paid plugin http://61extensions.com/shop/woocommerce-checkout-field-manager
but 49 dollars is a bit too much for my taste.
Jozef
Agree with Josef. This should be either a plugin or you should be able to edit it from Options.
But until that happens there is actually a slightly cheaper plugin that handles these issues.
$28 instead of $49
http://terrytsang.com/shop/shop/woocommerce-custom-checkout-options/
I tried this plugin on my local dev server and it works like a charm. On my live server I get an error and had to disable the plugin: Call to undefined function array_replace() … any clue? I’d love to be able to have a bit more control over the checkout experience on my site.
Well…. looks like my host’s version of php doesn’t support that function. If there’s a less “smelly” (as you say) way to do it, please consider a patch.
Could you please give details how to get the download file work? where to put and what to add? I couldn’t find a clue, but I really need to disable the billing address, thanks.
Thanks for the article and gesture.
I’m needing orientation around
drop it [.php file] into your wordpress install
Where in my wordpress install?
I vote for this to be a free plugin 🙂
Hoping for a response soon
😐
@SJ – I think it’s array_flip that’s smelly, I could be wrong? Check php docs, array_replace might not be available in your version. If not, time for a new web host 🙂
@Ashley — Actually ended up just unsetting the fields through functions.php. Uses the same basic methodology as yours but without filtering the checkout form directly without needing to create the custom array. Woo has a tutorial here. Thanks for the science, though.
Hi @Ashley
silly question.. I used your plugin to clean up my checkout but I would like the title of the form to be only “Shipping details” instead of “Billing & Shipping” but I have no idea where is the line that controls that.. could you help me out ?
Thanks for this tutorial and code!
swap array_replace() for array_merge() if you are not using PHP 5.3
Sorry, I forgot to mention that this fix is for anyone getting this error:
Fatal error: Call to undefined function array_replace() in /home/xxx/domains/xxx/public_html/wp-content/plugins/woocommerce-aschroder-customize-checkout/disable-checkout-fields.php on line 82
Does anyone know how to overwrite the need for email as I keep getting ‘Invalid email address. Go to homepage →’ message when trying to download goods?
It seems as though the woocommerce login system is not functioning with the WP system
I just have this to say to you —
you’re awesome!!
🙂
Nice plugin! Passed a year and I have not seen any like this in WP Repository.
How do you install this plugin? I am not an expert at PHP but I know a bit.
Where should the downloaded file be placed, what needs to be edited, etc.?
Hi. Great plugin since i have having a hard time removing the billing address etc. However, the plugin works fine, but i would like to remove the Phone number as well. How can i do this? Thanks
Thank you very much for this contribution. It works perfect and does the heavy lifting for me. I am using it to give a client what they are requesting. However on a store I manage I pass the billing information to PayPal. Then they use it to authorize credit cards by billing address. Have you heard of this causing problems with PayPal or Google Wallet? I am going to do some testing and might have to turn off the option to pass the information to PayPal.
@AAA, I just copied the entire thing above and pasted it in the function.php of my theme. This is a custom theme so I don’t have to be concerned about an update overwriting the code. If you are then you can place the file disable checkout fields in your plugin folder on your server root/wp-content/plugins/disable-checkout-fields.php. Then go to the “installed plugins” screen of the WP UI and activate it.
@Rolly, on line 41 of the disable-checkout-fields.php file you can add ,’billing_phone’ right after ‘billing_state’. Don’t for get the comma then it will look like this: ‘billing_state’, ‘billing_phone’. Then on lines 62 through 67 delete:
‘billing_phone’ => array(
‘label’ => __(‘Phone’,’wc_disable_checkout_fields’),
‘placeholder’ => __(‘Phone number’,’wc_disable_checkout_fields’),
‘required’ => true,
‘class’ => array(‘form-row-first’)
)
Then you should be all set.
Thank you for this. It helped a lot! If you make a plugin, be sure to add a donate button! 😉
I have used this an received the “Call to undefined function array_replace()” error. Also, how can I integrate this with AWD Weight/Country shipping plugin?
Hi there, I am not a developer, can you just give me the copy to drop in
the document class-wc-checkout.php
It’s such a shame that they developed a wonderful system but left out this most simplest task, the custom checkout page would deter anyone who wants to buy an item. Mine is a digital store. So, i only would need name and email address. Thank you!
Great plugin job ! You just SAVED me $29.00 in purchasing a WooCommerce Extension Checkout Field Editor. Many thanks mate, and keep up the outstanding work !
Hey thanks for explaining this and laying it all out. You saved me a ton of time!