Skip to content

Instantly share code, notes, and snippets.

@nayemDevs
Last active July 17, 2024 15:06
Show Gist options
  • Save nayemDevs/69ac7ea741404baabc5dc8a8aab46d32 to your computer and use it in GitHub Desktop.
Save nayemDevs/69ac7ea741404baabc5dc8a8aab46d32 to your computer and use it in GitHub Desktop.
Price and Image field required
/**
* 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 ( isset( $postdata['feat_image_id'] ) && empty( $featured_image ) && ! in_array( 'Please upload a product cover image' , $errors ) ) {
$errors[] = 'Please upload a product cover image';
}
if ( isset( $postdata['_regular_price'] ) && 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 ( isset( $data['_regular_price'] ) && ! $data['_regular_price'] ) {
return new WP_Error( 'no-price', __( 'Please insert product price', 'dokan-lite' ) );
}
if ( isset( $data['feat_image_id'] ) && ! $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 );
@CContentFox
Copy link

CContentFox commented Jul 17, 2024

This didn't work for me, it crashed the whole site. Any ideas what might be going wrong? I'm fairly new to this world so it might be that I'm just overlooking at something simple.
Edit: not sure if it matters that I'm using Dokan Pro
Edit2: Nevermind, I just realized I need to change the "lite" to "pro". :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment