Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make the 'Weight' field required in WooCommerce Edit Product page.
jQuery(document).ready(function(jQuery){
$('#_weight').prop('required',true); // Set weight field as required.
});
jQuery(document).ready(function(jQuery){
$( '#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.1
*/
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(jQuery){
$('#_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
You can’t perform that action at this time.