Hi, mosesclarke91, I’m another WooCommerce user.
It is possible to make optional fields mandatory by hooking into WooCommerce hooks that exist.
The WooCommerce documentation goes into good detail about how to do this:
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
This PHP code (add to your functions.php file or use a plugin like Code Snippets) should help you:
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$address_fields['postcode']['required'] = true;
$address_fields['company']['required'] = true;
$address_fields['company']['label'] = __('Pets name');
return $address_fields;
}
Hi,
Amazing. Thanks for so much this code. So I can just insert this code into the plugin ‘code snippets’ and it will insert this code into the PHP of WooCommerce? Then when WooCommerce updates it wont effect this code, that’s how code snippet works?
Hi @mosesclarke91,
So I can just insert this code into the plugin ‘code snippets’ and it will insert this code into the PHP of WooCommerce? Then when WooCommerce updates it wont effect this code, that’s how code snippet works?
That’s correct! If you’re using Code Snippets, updating WooCommerce will not have any effect on the custom code saved there.
Cheers.
Hi,
Amazing. Thank you. I have read on the code snippets plugin support that some sites have completely collapsed after using code snippets.
Is there anything I can do to try avoid this before inserting the snippet?
Thanks in advance!!
Hi guys,
It worked. Thank you so much!!! I was worried about the site crashing but it did not. Thank you @plantprogrammer for designing this code for me!!
The only addition I need to this code is that there are 2 shipping options, Ireland and the UK. Postcode is from the UK. But in Ireland this is known as the Eircode. Is there a way to add in an extra line to make the Eircode also mandatory?
Again, thank you so much for your help. I really appreciate it!
Hi @mosesclarke91,
Glad to hear that the code worked well to make fields mandatory.
I have read on the code snippets plugin support that some sites have completely collapsed after using code snippets. Is there anything I can do to try avoid this before inserting the snippet?
You can backup the site before adding the code and use a staging site to first test the code. For more info, kindly check this backup and staging section of one of our documentations .
The only addition I need to this code is that there are 2 shipping options, Ireland and the UK. Postcode is from the UK. But in Ireland this is known as the Eircode. Is there a way to add in an extra line to make the Eircode also mandatory?
Hoping that I understood this right, it seems that you want to have a limit on the countries to ship. If so, you can do it without custom code.
To make the available shipping countries to only be UK and Ireland in WooCommerce → Settings → General → Sell to specific countries:

Link to image: https://snipboard.io/cfxa76.jpg
Hope this helps.
Thanks.
Hi @abwaita ,
Thanks for this info. Interesting about the staging plug in. I will use this feature in the future.
I am already shipping to Ireland and to the UK.
My query is relating to the above php code for making the postcode mandatory. Postcode is in the UK. But Eircode is in Ireland. So at the moment when somebody buys my product in the UK their postcode is mandatory. But when somebody buys in Ireland the Eircode is not yet mandatory. Can I tweak the above code to include EIRCODE as well as POSTCODE to be both mandatory depending on which country they are shipping to. Hopefully this makes sense.
Thanks
Hi @mosesclarke91,
In this particular forum we’re focused on the default features of WooCommerce and, from time to time, you can get help from developers hanging out here willing to help.
@plantprogrammer could possibly help you tweak the code but I would definitely recommend reaching out to more development-oriented channels such as these ones:
Thanks.