Skip to content

Instantly share code, notes, and snippets.

@nayemDevs
Last active March 12, 2024 15:13
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 );
@queno18
Copy link

queno18 commented Nov 30, 2020

Thanks for this code, just a problem, it works perfectly for a simple product, but in variable product, even if I add price, the message ‘Please insert product price’ still appears. Any solution for product variable price attribute?

@renrax
Copy link

renrax commented Nov 11, 2021

I checked, it works for me and in variable goods.
But there is a problem with the translation. The dokan-lite translation template is missing all these lines:

  • 'Please upload a product cover image' ;
  • 'Please insert product price' ;
  • 'Please insert product price' ;
  • 'Please select AT LEAST ONE Picture' .

Thank you.

@JegerBlunt
Copy link

Thank you! Works great!
It just needs to be adapted for WC Bookings (Cost vs regular_price), throws an error.

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