Onclick event for to get orders in woocommerce dat

2019-07-26 01:40发布

the onclick event i would need it to appear in this code`to place order information in database. Also how do i display order id as four numbers.

this is javascript

// Provide values from the current cart order
var amount = <?php global $woocommerce; print WC()->cart->total; ?>;
var merchantOrderId = '<?php print time(); ?>';
var apiKey = 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t';

renderMMoneyPaymentButton(amount, merchantOrderId, apiKey)

1条回答
Explosion°爆炸
2楼-- · 2019-07-26 02:27

You can only get the Order ID after checkout when the order has been placed and paid in "Order received" page… The following will set in your javascript the order ID and the order total amount:

add_action('wp_head', 'render_mmoney_payment_button_script_js' );
function render_mmoney_payment_button_script_js(){
    // Only on Order received page
    if( is_wc_endpoint_url('order-received') ) :

    // get order ID
    $order_id = get_query_var('order-received');

    // Format order ID to 4 digits
    $order_id = str_pad($order_id, 4, '0', STR_PAD_LEFT); 

    // Get the order Object
    $order    = wc_get_order( $order_id );

    // Get order total amount
    $total    = $order->get_total()
    ?>
    <script type="text/javascript">
    var apiKey = 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t';
    renderMMoneyPaymentButton(<?php echo $total; ?>, <?php echo $order_id; ?>, apiKey)
    </script>
    <?php
    endif; ?>
}

Code goes in function.php file of your active child theme (or active theme). It should works.

If you want your script in the footer, you just have to replace 'wp_head' by 'wp_footer'… You can also use woocommerce_thankyou hook providing the order Id as an argument in the function.

查看更多
登录 后发表回答