Onclick event for to get orders in woocommerce database

一个人想着一个人 提交于 2019-12-11 12:49:49

问题


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:


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.



来源:https://stackoverflow.com/questions/55188642/onclick-event-for-to-get-orders-in-woocommerce-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!