• Resolved marcorm69

    (@marcorm69)


    Hi all.

    I have add a “File Upload” in my form, but this is visible for all type of payment, but I need only for offline payment, it is possible?
    Also, how to save the sent file (jpg or pdf)?

    Regards,
    Marco

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Ben Meredith

    (@benmeredithgmailcom)

    Hi @marcorm69,

    With only the free GiveWP plugin, there is not a way to add a file upload option. If you are using Form Field Manager to add a custom field for file upload, we’re happy to help you with support via our priority support channel at https://givewp.com/priority-support

    We are not allowed by forum rules to support or discuss premium products here.

    If you are adding that file upload field via some custom code that you created, I’m happy to take a look to see what I can understand about how to make what you are looking for happen. We’d need to see that custom code, though.

    Thanks!

    Thread Starter marcorm69

    (@marcorm69)

    Hi Ben.

    Thanks!
    For now I have try only the add a file upload button with a function

    
    function give_ma_custom_form_fields( $form_id ) {
    	// Only display for forms with the IDs "754" and "578";
    	// Remove "If" statement to display on all forms
    	// For a single form, use this instead:
    	if ( $form_id == 26958) {
    	//$forms = array( 754, 578 ); 
    	?>
    
        <div id="holder">
    	<i class="fas fa-cloud-upload-alt"></i>
            <input type="file" name="fileToUpload" id="fileToUpload">	
        </div> 
    
    	<?php
    	} //endif;
    }
    
    add_action( 'give_purchase_form_after_cc_form', 'give_ma_custom_form_fields', 10, 1 );
    

    The button is visible, but I have 2 questions:

    1. How do I show the button ONLY with bank payment?
    2. How to load the file when I click submit button?

    Regards,
    Marco

    Thread Starter marcorm69

    (@marcorm69)

    I add a bit.

    I am able to store the “namefile” in db and show it in donation detail, but not to upload the file.

    Thread Starter marcorm69

    (@marcorm69)

    I went a little further.
    This is my complete code, for now.
    But the problem persist.
    Without add “multipart/form-data” in form, the filename is stored in DB and show in donation detail but the file is not upload.
    With add “multipart/form-data” in form, the filemane is not stored in DB and NOT show in donation detail page and the file is NOT upload.
    ??

    <?php
    
    /**
     * Add enctype="multipart/form-data" to GiveWP form
     *
     * @param $final_output, $args
     */
    function give_add_multipart_tag_donation_form( $final_output, $args ) {    
    		$form_tag = "<form id=\"give-form-{$args['form_id']}-1\"";		
    		$final_output = str_replace( $form_tag, "{$form_tag} enctype=\"multipart/form-data\"", $final_output );
    		return $final_output;	
    }
    
    add_filter( 'give_donate_form', 'give_add_multipart_tag_donation_form', 10, 2 );
    
    /**
     * Add Custom Donation Form Fields
     *
     * @param $form_id
     */
    function give_mamafrica_custom_form_fields( $form_id ) {
    	// Only display for forms with the IDs "754" and "578";
    	// Remove "If" statement to display on all forms
    	// For a single form, use this instead:
    	if ( $form_id == 27021) {
    	//$forms = array( 754, 578 ); 
    	?>
    
    		
      <div id="holder">
    	<i class="fas fa-cloud-upload-alt"></i>
        <input type="file" name="fileToUpload" id="fileToUpload">
      </div> 
    
    	<?php
    	} //endif;
    }
    
    add_action( 'give_purchase_form_after_cc_form', 'give_mamafrica_custom_form_fields', 10, 1 );
    
    function give_mamafrica_donations_save_custom_fields( $payment_id ) {
    	if ( isset( $_POST['fileToUpload'] ) ) {
    		$message = wp_strip_all_tags( $_POST['fileToUpload'], true );
    		give_update_payment_meta( $payment_id, 'fileToUpload', $message );		
    	if(trim($_FILES["fileToUpload"]["name"]) == '')  
    		{  		
    			echo 'Non hai selezionato nessun file!';  
    		}  
    		if (!isset($_FILES['fileToUpload']) || !is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
    			echo 'Non hai inviato nessun file...';
    			exit;    
    		}
    		//percorso della cartella dove mettere i file caricati dagli utenti
    		$uploaddir = get_template_directory() || '/offline_donation/';
    
    		//Recupero il percorso temporaneo del file
    		$userfile_tmp = $_FILES['fileToUpload']['tmp_name'];
    
    		//recupero il nome originale del file caricato
    		$userfile_name = $_FILES['fileToUpload']['name'];
    
    		//copio il file dalla sua posizione temporanea alla mia cartella upload
    		if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
    		//Se l'operazione è andata a buon fine...
    			echo 'File inviato con successo.';
    		}else{
    			//Se l'operazione è fallta...
    			echo 'Upload NON valido!'; 
    		}
    		
    	}
    	
    	
    }
    
    add_action( 'give_insert_payment', 'give_mamafrica_donations_save_custom_fields' );
    
    /**
     * Show Data in Payment Details
     *
     * Show the custom field(s) on the payment details page in wp-admin within its own metabox.
     *
     * @param int $payment_id
     */
    function give_mamafrica_donation_details( $payment_id ) {
    
    	// Bounce out if no data for this transaction.
    	$fileToUpload = give_get_meta( $payment_id, 'fileToUpload', true );
    	if ( $fileToUpload ) : ?>
            <div class="referral-data postbox">
                <h3 class="hndle"><?php esc_html_e( 'Copia pagamento', 'give' ); ?></h3>
                <div class="inside" style="padding-bottom:10px;">			
    				<?php echo wpautop( $fileToUpload ); ?>
                </div>
            </div>
    	<?php endif;
    
    	
    	}
    
    add_action( 'give_view_donation_details_billing_after', 'give_mamafrica_donation_details' );
    
    ?>
    
    
    Thread Starter marcorm69

    (@marcorm69)

    Hi all,

    I have solved the upload files issue (at the end of develop I post the code)
    There is still the problem of viewing the file upload only for offline payment.
    Someone can you help me?

    Regards,
    Marco

    Thread Starter marcorm69

    (@marcorm69)

    I all.

    I have solved the second issue.
    I write the solution if it can be useful to someone.

    In add hook give_donation_form_required_fields in order to add a file upload field.
    Before add field code, I add check the radio button payment method.
    If the radio button is “paypal” I add “display:none” at the file upload div.
    If the radio button is “offline” I add “display:block” at the file upload div.
    At the first time, the payment-mode is empty, but the my default payment method is Paypal and the file upload field should not be displayed, so I added $answer==”” to check.

    The code working at each change of payment method.

    This is the code:

    <php?
      $answer = $_GET['payment-mode'];  
      if ($answer == "paypal" || $answer == "") {
    	$div_file_uploader = "style=\"display:none;\"";
    	$div_upload_filename = "style=\"display:none;\"";
      }
      else {
    	$div_file_uploader = "style=\"display:block;\"";
    	$div_upload_filename = "style=\"display:block;\"";
      } 
    ?>
    
    <div class="file-uploader" <?php echo $div_file_uploader; ?>>
        <input id="fileToUpload" name="fileToUpload" type="file" accept=".pdf,.jpg"/>
        <label for="file-upload" class="custom-file-upload">
           <i class="fas fa-cloud-upload-alt"></i>
           <p>Clicca per caricare la ricevuta di pagamento</p>
           <input type="hidden" id="filenamecheck" name="filenamecheck" value="">
         </label>
    </div>'

    Regards.
    Marco

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘File upload’ is closed to new replies.