Last active
April 23, 2021 09:19
-
-
Save damiencarbery/2ac9d1651bd8f0a48f4d0434e0714bb2 to your computer and use it in GitHub Desktop.
Make the 'Weight' field required in WooCommerce Edit Product page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($){ | |
$('#_weight').prop('required',true); // Set weight field as required. | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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