I am trying to require Dokan vendors to enter dimensions when adding or editing a product through their vendor dashboards. This is because we calculate shipping automatically on the backend and if they don't provide said information, the sale won't complete.
What I have tried is:
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){
required_fields_data = [
[ 'Weight', '#_weight', 'Shipping', '.shipping_tab > a' ],
[ 'Length', '#product_length', 'Shipping', '.shipping_tab > a' ],
[ 'Width', '#product_width', 'Shipping', '.shipping_tab > a' ],
[ 'Height', '#product_height', 'Shipping', '.shipping_tab > a' ],
];
num_required_fields = required_fields_data.length;
// Set the fields as required.
for ( i = 0; i < num_required_fields; i++ ) {
$( required_fields_data[ i ][ 1 ] ).prop('required',true);
}
// Verify that each required field has a non-zero value before allowing save.
$( '#publish' ).on( 'click', function() {
error_message = '';
target_field = null;
target_tab = null;
for ( i = 0; i < num_required_fields; i++ ) {
value = $.trim($( required_fields_data[ i ][ 1 ] ).val());
if ( value == '' || value == 0 ) {
error_message += required_fields_data[ i ][ 0 ] + " must be set in the "+ required_fields_data[ i ][ 2 ] +" tab.\n";
target_field = required_fields_data[ i ][ 1 ];
target_tab = required_fields_data[ i ][ 3 ];
}
}
if ( error_message != '' ) {
alert( error_message );
$( target_tab ).click(); // Click on appropriate tab.
$( target_field ).focus(); // Focus on the appropriate field.
return false;
}
});
});
</script>
But that only works on the wp-admin console for WooCommerce.
The code that should work for Dokan Vendor Dashboard, for example:
<?php
}
}
/**
* Validation add for product cover image
*
* @param array $errors
* @return array $errors
*/
function dokan_can_add_product_validation_customized( $errors ) {
$postdata = wp_unslash( $_POST );
$featured_image = absint( sanitize_text_field( $postdata['feat_image_id'] ) );
$_regular_price = absint( sanitize_text_field( $postdata['_regular_price'] ) );
if ( empty( $featured_image ) && ! in_array( 'Please upload a product cover image' , $errors ) ) {
$errors[] = 'Please upload a product cover image';
}
if ( empty( $_regular_price ) && ! in_array( 'Please insert product price' , $errors ) ) {
$errors[] = 'Please insert product price';
}
return $errors;
}
add_filter( 'dokan_can_add_product', 'dokan_can_add_product_validation_customized', 35, 1 );
add_filter( 'dokan_can_edit_product', 'dokan_can_add_product_validation_customized', 35, 1 );
function dokan_new_product_popup_validation_customized( $errors, $data ) {
if ( ! $data['_regular_price'] ) {
return new WP_Error( 'no-price', __( 'Please insert product price', 'dokan-lite' ) );
}
if ( ! $data['feat_image_id'] ) {
return new WP_Error( 'no-image', __( 'Please select AT LEAST ONE Picture', 'dokan-lite' ) );
}
}
add_filter( 'dokan_new_product_popup_args', 'dokan_new_product_popup_validation_customized', 35, 2 );
function dokan_add_product_errors($errors) {
//echo '<pre>'; print_r($_POST); echo '</pre>';
//echo '<pre>'; print_r($errors); echo '</pre>';
if(isset($_POST['item_condition'])){
$condition = $_POST['item_condition'];
if(empty($condition)){
$errors[] = 'Condition is required';
}
}
return $errors;
}
add_filter( 'dokan_can_add_product', 'dokan_add_product_errors', 121 );```
But I need to require dimensions and weight, not just the product image and price.
Thanks in advance.