• Hello guys, I’m working on a portfolio page in which the user can buy a product and if the purchase was successful an email should be sent to him with the product attachment according to the product ID.

    I created two custom fields. One for setting the price and a second one for getting the file(or a link.)

    Custom Field for setting the price:

    
    // Price to buy product with Paypal
    function comprar_url($postID){
    	$comprar_url = 'Precio para comprar Archivo con Paypal';
    	$producto_url = get_post_meta($postID, $comprar_url, true);
    	if($producto_url==''){
    		delete_post_meta($postID,$comprar_url);
    		add_post_meta($postID,$comprar_url, '#');
    		return "#";
    		}
    	return $producto_url;
    	}
    

    Custom field to get the file:

    
    // URL de descarga de archivo
    function descargar_url($postID){
    	$descargar_url = 'URL para descargar Archivo';
    	$archivo_url = get_post_meta($postID, $descargar_url, true);
    	if($archivo_url==''){
    		delete_post_meta($postID,$descargar_url);
    		add_post_meta($postID,$descargar_url, '#');
    		return "#";
    		}
    	return $archivo_url;
    	}
    

    Having created those two custom fields I added a form to make the purchase of the product(according to the ID) in the Front End:

    
    <!-- Paypal Button -->
    <?php $producto_url = get_post_meta($post->ID, 'Precio para comprar Archivo con Paypal', true); ?>
    <?php if ($producto_url == '#') : ?>
    <?php else : ?>
    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
      <input type="hidden" name="cmd" value="_xclick">
      <!-- The PayPal account to pay -->
      <input type="hidden" name="business" id="business" value="<?php bloginfo('admin_email'); ?>" />
      <!-- Amount price -->
      <input type="hidden" name="amount" id="amount" value="<?php echo comprar_url(get_the_ID()); ?>" />
      <!-- It will be shown on PayPal checkout page -->
      <input type="hidden" name="item_name" id="item_name" value="<?php the_title(); ?>" /> 
      <!-- Specify your product ID here to process it later -->
      <input type="hidden" name="item_number" id="item_number" value="<?php the_id(); ?>" />
      <!-- Charset -->
      <input type="hidden" name="charset" value="utf-8">
      <!-- Currency -->
      <input type="hidden" name="currency_code" value="USD">
      <!-- Thank You page, the customer will be redirected after the payment -->
      <input type="hidden" name="return" value="<?php echo site_url(); ?>">
      <button type="submit" class="btn btn-success btn-sm"><i class="fa fa-paypal" aria-hidden="true"></i></button>
    </form>
    <?php endif; ?>
    

    Now in the code above I’m getting the proper values from the custom field to set the price.

    The problem that I have is in another document in which I handle whether the purchase was successful or not. Here it is:

    
    $r = array(
    'status' => $_POST['payment_status'],
    'payer_email'      => $_POST['payer_email']
    );
    if( isset( $_POST['item_number'] ) ) {
    $r['item_number'] = intval($_POST['item_number']);
    } elseif ( isset( $_POST['item_number1'] ) ) {
    $r['item_number'] = intval($_POST['item_number1']);
    }
    if( strtolower( $_POST['payment_status'] ) == 'completed' && $r['item_number'] ) {
    /*
     * On success - do some stuff here, you can:
     * send the product by email
     */
     
    // Download Button
    $archivo_url = get_post_meta($post->ID, 'URL para descargar Archivo', true);
    if ($archivo_url == '#') :
    echo 'Sorry, there's has been an error with the download. Please, contact the websmaster';
    else :
    
    $plugin_path = echo "" descargar_url(get_the_ID()); ""; // I can store this info from custom field
    $headers = 'From: Admin <no-reply@demo.com>' . "\r\n";
    wp_mail( $_POST['stripeEmail'], 'Thanks for buying with us', 'The product is attached to this email.', $headers, $attachments );
    
    endif;
     
    exit();
    }
    exit();
    

    Is it possible to get the values from the custom fiel in the email?… can you check my code, please?.

    Thanks in advanced for helping out.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    When you ask “Is it possible to get the values from the custom fiel[d] in the email?”, I’m unsure which direction you wish to go with the data. Either way the answer is yes. But do you really wish to get a value from the email? Or do you wish to place a value into the email? The first is literally what you asked, but the latter is a more likely question.

    There would be little reason to get a value from email since you created the email and the data used to compose it is available. Right now, the email body is “The product is attached to this email.” There is no useful data to get from that.

    To put a value into that message, you simply concatenate the value into message fragments using the concatenation operator: .. For example, to place the value of $archivo_url into a email message, the body argument for wp_mail() might be something like this:
    'The product is attached to this email. It may also be downloaded from this location: ' . $archivo_url . ' Thank you for your business.'

    I’m not going to fully check your code. If it works, it’s probably OK. I did notice that this line doesn’t look right:
    $plugin_path = echo "" descargar_url(get_the_ID()); "";

    I’m not sure what you are trying to do, maybe this:
    echo $plugin_path = descargar_url(get_the_ID());
    both assigns and echoes the return from descargar_url().

    Your use of a full sentence for meta tags is sub-optimal. While it is apparently just fine on your site, tags like that can have compatibility issues on various other sites, so is considered poor practice. Tags should generally be like variable names – all lowercase, no spaces. Instead of “Precio para comprar Archivo con Paypal”, consider something more like “paypal_archivo_precio”. Fully compatible, more concise, just as meaningful, and conforms to the coding style guide used by WP devs.

Viewing 1 replies (of 1 total)
  • The topic ‘How to properly send file from Custom field on Email(PayPal)’ is closed to new replies.