Last active
July 17, 2024 15:06
-
-
Save nayemDevs/69ac7ea741404baabc5dc8a8aab46d32 to your computer and use it in GitHub Desktop.
Price and Image field 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
/** | |
* 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 ); |
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.
Thank you! Works great!
It just needs to be adapted for WC Bookings (Cost vs regular_price), throws an error.
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
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?