我如何要求 Dokan 供应商为其产品输入尺寸和重量?

问题描述

我试图要求 Dokan 供应商在通过其供应商仪表板添加或编辑产品时输入维度。这是因为我们会在后端自动计算运费,如果他们不提供上述信息,则销售将无法完成。

我尝试过的是:

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',[ 'Width','#product_width',[ 'Height','#product_height',];
    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>

    

但这仅适用于 WooCommerce 的 wp-admin 控制台。

应该适用于 Dokan vendor Dashboard 的代码,例如:

<?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',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',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.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)