getdokan / dokan

Multivendor marketplace platform
https://wordpress.org/plugins/dokan-lite/
259 stars 203 forks source link

Make Image, Price and Description fields required when adding a product #1258

Open MercanoGlobal opened 3 years ago

MercanoGlobal commented 3 years ago

At the moment, Dokan doesn't have any option for admins to make mandatory the adding of images, price and description, when a vendor adds a product. This is a big issue, as many vendors aren't web/computer experts, and many times they will just click on Create product, without adding any information.

To fix this, we have the following code that works well for Simple and External products, but the price part seems to break Variable and Group products (you get an error Please insert the product price. when trying to add those types of products).

So basically, we need 3 checkboxes in Dokan Settings - Selling Options to make these 3 fields mandatory or not.

/**
 * Validation for add product cover image, price and description
 *
 * @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'] ) );
  $post_excerpt   = wp_kses_post( $postdata['post_excerpt'] );

  if ( isset( $postdata['feat_image_id'] ) && empty( $featured_image ) && ! in_array( 'Please upload at least one product image.' , $errors ) ) {
      $errors[] = 'Please upload at least one product image.';
  }

  if ( isset( $postdata['_regular_price'] ) && empty( $_regular_price ) && ! in_array( 'Please insert the product price.' , $errors ) ) {
      $errors[] = 'Please insert the product price.';
  }

  if ( isset( $postdata['post_excerpt'] ) && empty( $post_excerpt ) && ! in_array( 'Please add a short description.' , $errors ) ) {
      $errors[] = 'Please add a short description.';
  }

  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['feat_image_id'] ) && ! $data['feat_image_id'] ) {
    return new WP_Error( 'no-image', __( 'Please upload at least one product image.', 'dokan-lite' ) );
  }
  if ( isset( $data['_regular_price'] ) && ! $data['_regular_price'] ) {
    return new WP_Error( 'no-price', __( 'Please insert the product price.', 'dokan-lite' ) );
  }
  if ( isset( $data['post_excerpt'] ) && ! $data['post_excerpt'] ) {
    return new WP_Error( 'no-desc', __( 'Please add a short description.', 'dokan-lite' ) );
  }
}

add_filter( 'dokan_new_product_popup_args', 'dokan_new_product_popup_validation_customized', 35, 2 );
Sophie-2e commented 5 months ago

Happy Friday @devAsadNur , @nurul-umbhiya, This is such a must-have feature guys! The issue with the current solution given in this Github issue is that it reloads the page so sellers will lose all the info entered if they forget one field which is bad for the UX and a waste of time for the sellers. What is the ETA on this feature? 🙂 it will also be nice to add a red '*' on the mandatory label field so sellers see directly that these fields are mandatory 😉

devAsadNur commented 5 months ago

@MercanoG @Sophie-2e Our upcoming Product Form Customize feature will contain this functionality, which is currently under development. Thanks for your cooperation.

Sophie-2e commented 3 months ago

Hello @devAsadNur, any news on when this new feature will be available?

Mohaiminulislam1989 commented 3 months ago

Hi @Sophie-2e, we hope to make this feature available in mid-September, InShaAllah.

Sophie-2e commented 2 weeks ago

Hello @Mohaiminulislam1989 hope you are well :) Will it be released in November 🤞?