Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active April 23, 2021 09:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save damiencarbery/2ac9d1651bd8f0a48f4d0434e0714bb2 to your computer and use it in GitHub Desktop.
Save damiencarbery/2ac9d1651bd8f0a48f4d0434e0714bb2 to your computer and use it in GitHub Desktop.
Make the 'Weight' field required in WooCommerce Edit Product page.
jQuery(document).ready(function($){
$('#_weight').prop('required',true); // Set weight field as required.
});
jQuery(document).ready(function($){
$( '#publish' ).on( 'click', function() {
weight = $.trim($('#_weight').val());
if ( weight == '' || weight == 0 ) {
alert( 'Weight must be set in the Shipping tab.' );
$( '.shipping_tab > a' ).click(); // Click on 'Shipping' tab.
$( '#_weight' ).focus(); // Focus on Weight field.
return false;
}
});
});
<?php
/*
Plugin Name: Require Weight field (WooCommerce)
Plugin URI: https://www.damiencarbery.com/2018/01/make-weight-a-required-field-in-woocommerce/
Description: Require that the Weight field have a value.
Author: Damien Carbery
Author URI: http://www.damiencarbery.com
Version: 0.2
WC tested up to: 5.2.2
*/
add_action( 'admin_head', 'dcwd_require_weight_field' );
function dcwd_require_weight_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function($){
$('#_weight').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function() {
weight = $.trim($('#_weight').val());
if ( weight == '' || weight == 0 ) {
alert( 'Weight must be set in the Shipping tab.' );
$( '.shipping_tab > a' ).click(); // Click on 'Shipping' tab.
$( '#_weight' ).focus(); // Focus on Weight field.
return false;
}
});
});
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment