Forked from damiencarbery/add-required-attribute.js
Last active
April 9, 2020 16:30
-
-
Save danielcharrua/e897ec4adda0d9919d1d1250974094ac to your computer and use it in GitHub Desktop.
Make the 'Weight' field required in WooCommerce Edit Product page.
This file contains 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://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