Skip to content

Instantly share code, notes, and snippets.

@wurwal
Created December 6, 2019 11:46
Show Gist options
  • Save wurwal/5ab153d58317f8cb8f89146b3ebbf2f2 to your computer and use it in GitHub Desktop.
Save wurwal/5ab153d58317f8cb8f89146b3ebbf2f2 to your computer and use it in GitHub Desktop.
Woo Product Entry Validation code
/* product entry validation STARTS */
// product entry validation - weight on simple products
add_action( 'admin_head', 'require_weight_field' );
function require_weight_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//dynamic product type variable added 14/5/19
producttype = $( "#product-type option:selected" ).val();
$('#product-type').change(function(){
producttype = $( "#product-type option:selected" ).val();
});
});
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('#_weight').prop('required',true); // Set weight field as required.
weight = $.trim($('#_weight').val());
if ( weight == '' || weight == 0 ) {
alert( 'Weight must be set in the Shipping tab on this simple product type.' );
$( '.shipping_tab > a' ).click(); // Click on 'Shipping' tab.
$( '#_weight' ).focus(); // Focus on Weight field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - SKU on simple products */
add_action( 'admin_head', 'require_sku_field' );
function require_sku_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('#_sku').prop('required',true); // Set weight field as required.
sku = $.trim($('#_sku').val());
if ( sku == '' || sku == 0 ) {
alert( 'SKU must be set in the inventory tab on this simple product type' );
$( '.inventory_tab > a' ).click(); // Click on 'Shipping' tab.
$( '#_sku' ).focus(); // Focus on Weight field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - dimensions - length on simple products */
add_action( 'admin_head', 'require_length_field' );
function require_length_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('#product_length').prop('required',true); // Set field as required.
length = $.trim($('#product_length').val());
if ( length == '' || length == 0 ) {
alert( 'Length must be set in the shipping tab on this simple product type' );
$( '.shipping_tab > a' ).click(); // Click on right tab.
$( '#product_length' ).focus(); // Focus on required field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - dimensions - width on simple products */
add_action( 'admin_head', 'require_width_field' );
function require_width_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('input[name="_width"]').prop('required',true); // Set field as required.
width = $.trim($('input[name="_width"]').val());
if ( width == '' || width == 0 ) {
alert( 'Width must be set in the shipping tab on this simple product type' );
$( '.shipping_tab > a' ).click(); // Click on right tab.
$( 'input[name="_width"]' ).focus(); // Focus on required field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - dimensions - height on simple products */
add_action( 'admin_head', 'require_height_field' );
function require_height_field() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('input[name="_height"]').prop('required',true); // Set field as required.
height = $.trim($('input[name="_height"]').val());
if ( height == '' || height == 0 ) {
alert( 'Height must be set in the shipping tab on this simple product type' );
$( '.shipping_tab > a' ).click(); // Click on right tab.
$( 'input[name="_height"]' ).focus(); // Focus on required field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - manage stock must be ticked on simple products */
add_action( 'admin_head', 'require_stock_control' );
function require_stock_control() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'simple') {
$('#_manage_stock').prop('required',true); // Set field as required.
ms = $.trim($('#_manage_stock').val());
if ( ms == '' || ms !== 'yes' ) {
alert( 'Manage stock must be set in the inventory tab on this simple product type' );
$( '.inventory_tab > a' ).click(); // Click on right tab.
$( '#_manage_stock' ).focus(); // Focus on required field.
return false;
}
}
});
});
</script>
<?php
}
}
/* product entry validation - SKU on Variable product */
add_action( 'admin_head', 'require_sku_field_var' );
function require_sku_field_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('#_sku').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function(e) {
//id starts with variable_sku
varsku = $.trim($('[id^="variable_sku"]').val());
//producttype = $( "#product-type option:selected" ).val();
//alert(varsku);
if (producttype == 'variable') {
$('[id^="variable_sku').each(function() {
$(this).prop('required',true); // Set fields to required
if(!$(this).val()){
alert('Some variable SKUs are empty');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - Weight on Variable product */
add_action( 'admin_head', 'require_weight_field_var' );
function require_weight_field_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('#_sku').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function(e) {
//id starts with variable_sku
varsku = $.trim($('[id^="variable_weight"]').val());
//producttype = $( "#product-type option:selected" ).val();
//alert(varsku);
if (producttype == 'variable') {
$('[id^="variable_weight').each(function() {
$(this).prop('required',true); // Set fields to required
if(!$(this).val()){
alert('Some variable Weights are empty');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - Length on variable product */
add_action( 'admin_head', 'require_length_field_var' );
function require_length_field_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('#_sku').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function(e) {
//id starts with variable_sku
varsku = $.trim($('[name^="variable_length"]').val());
//producttype = $( "#product-type option:selected" ).val();
//alert(varsku);
if (producttype == 'variable') {
$('[name^="variable_length').each(function() {
$(this).prop('required',true); // Set fields to required
if(!$(this).val()){
alert('Some variable Dimensions (lengths) are empty');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - width on variable product */
add_action( 'admin_head', 'require_width_field_var' );
function require_width_field_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('#_sku').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function(e) {
//id starts with variable_sku
varsku = $.trim($('[name^="variable_width"]').val());
//producttype = $( "#product-type option:selected" ).val();
//alert(varsku);
if (producttype == 'variable') {
$('[name^="variable_width').each(function() {
$(this).prop('required',true); // Set fields to required
if(!$(this).val()){
alert('Some variable Dimensions (widths) are empty');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - height on variable product */
add_action( 'admin_head', 'require_height_field_var' );
function require_height_field_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('#_sku').prop('required',true); // Set weight field as required.
$( '#publish' ).on( 'click', function(e) {
//id starts with variable_sku
varsku = $.trim($('[name^="variable_height"]').val());
//producttype = $( "#product-type option:selected" ).val();
//alert(varsku);
if (producttype == 'variable') {
$('[name^="variable_height').each(function() {
$(this).prop('required',true); // Set fields to required
if(!$(this).val()){
alert('Some variable Dimensions (heights) are empty');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - manage stock on variable product - */
add_action( 'admin_head', 'require_stock_control_var' );
function require_stock_control_var() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('.variable_manage_stock').prop('required',true); // Set field as required.
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'variable') {
$('[name^="variable_manage_stock').each(function() {
$(this).prop('required',true); // Set fields to required
if(($(this).prop('checked')==false)){
alert('All variations must have manage stock ticked');
$( '.variations_tab > a' ).click(); // Click on 'variations' tab.
return false;
event.preventDefault(e);
}
});
}
});
});
</script>
<?php
}
}
/* product entry validation - check atleast one variation exists */
add_action( 'admin_head', 'require_variation' );
function require_variation() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == 'product' ) {
?>
<script>
jQuery(document).ready(function(jQuery){
//$('.variable_manage_stock').prop('required',true); // Set field as required.
$( '#publish' ).on( 'click', function() {
//producttype = $( "#product-type option:selected" ).val();
if (producttype == 'variable') {
if ( $( ".woocommerce_variation" ).length ) {
// a variation exists
} else {
alert("make sure you have created at least one variation on this variable product");
return false;
event.preventDefault(e);
}
}
});
});
</script>
<?php
}
}
/* product entry validation ENDS */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment