Forked from damiencarbery/add-required-attribute.js
Created
December 15, 2020 10:16
-
-
Save ljube/b24f5ae36f7bd79fd20b0c446b86a96a 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(jQuery){ | |
| $('#_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(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; | |
| } | |
| }); | |
| }); |
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.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