Add a clikable dynamic payment request uri to on-hold email notification in WooCommerce

Viewed 62

I have a payment request uri that looks like this:

https://www.mypaymentsite.com/cgi-bin/webscr?&cmd=_xclick&business=89000&currency_code=USD&amount=9&item_name=ebook

I'm trying to create a dynamic clickable payment link that will capture WooCommerce order total as the amount.


The starting part of the url

https://www.mypaymentsite.com/cgi-bin/webscr?&cmd=_xclick&business=89000&currency_code=USD&amount=

should remain the same. Then the order total appended and finally the last url part &item_name=ebook

The payment link can be added to customer's order_on_hold woocommerce_email_before_order_table or be added to payment instructions.


This is my code so far

add_action( 'woocommerce_email_before_order_table', 'customer_note_email_before_order_table', 10, 4 );
function customer_note_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ){
    // Only on onhold email notifications
    if ( in_array( $email->id, array('customer_on_hold_order') ) ) :


    //first part of the url
    $first_url_part = "https://www.mypaymentsite.com/cgi-bin/webscr?&cmd=_xclick&business=89000&currency_code=USD&amount=";
    
    // Get customer Order total as the 2nd part
    $order_total = $order->get_order_total();

    //last part of the url
    $last_url_part = "&item_name=ebook";


    //Full url now. How do i combine those to make a url? 
    $payment_link = $first_url_part$order_total$last_url_part; 

    // lastly Display payment note "click here to send order payment directly" with "click here" as link text

    echo '<h2>' . __("Direct Payment Link", "woocommerce") . '</h2> //HOW DO I ECHO THAT?
    <div style="margin-bottom: 40px;">
    <table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
    <tr><td><p>' . $payment_link . '</p></td></tr>
    </table></div>';

    endif;
}

I have been able to add the link to payment instructions, but I'm not able to append order amount. When i try {order_total} it is not working. I need help please.

1 Answers

Your code contains some mistakes

  • Replace $order->get_order_total() with $order->get_total()
  • Put dots between your variables to concatinate them
  • The code to make the link work effectively is missing

So you get:

function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) { 
    // Only on onhold email notifications 
    if ( in_array( $email->id, array( 'customer_on_hold_order' ) ) ) {
        // First part of the url
        $first_url_part = 'https://www.mypaymentsite.com/cgi-bin/webscr?&cmd=_xclick&business=89000&currency_code=USD&amount=';
            
        // Get customer Order total as the 2nd part
        $order_total = $order->get_total();

        // Last part of the url
        $last_url_part = '&item_name=ebook';

        // Full url
        $payment_link = $first_url_part . $order_total . $last_url_part; 

        // Lastly display payment note "click here to send order payment directly" with "click here" as link text
        echo '<h2>' . __( 'Direct Payment Link', 'woocommerce') . '</h2>
            <div style="margin-bottom: 40px;">
            <table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
            <tr><td><span><a href="' . $payment_link . '">click here</a> to send order payment directly</span></td></tr>
            </table></div>';
    }
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
Related