Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielcharrua/e897ec4adda0d9919d1d1250974094ac to your computer and use it in GitHub Desktop.
Save danielcharrua/e897ec4adda0d9919d1d1250974094ac to your computer and use it in GitHub Desktop.
Make the 'Weight' field required in WooCommerce Edit Product page.
<?php
/*
Plugin Name: Require Weight field (WooCommerce)
Plugin URI: https://charrua.es
Description: Make weight field required when adding/editing product. Usefull when using weight shipping.
Author: Daniel Pereyra Costas
Author URI: https://charrua.es
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($){
$('#_weight').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function() {
weight = $('#_weight').val();
if ( weight == '' || weight == 0 ) {
alert( 'Debes completar el peso del artículo en la pestaña de envío' );
$( '.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