Onclick event for to get orders in woocommerce database

问题: 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 // P...

问题:

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.

  • 发表于 2019-03-17 12:34
  • 阅读 ( 219 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除