Get the order ID in checkout page before payment process

落爺英雄遲暮 提交于 2020-04-28 08:17:40

问题


In WooCommerce, I need to get order ID right in checkout page of WooCoommerce, before payment, when the order is created.

I look at all sessions and tried to find out when order goes for payment in order_awaiting_payment session, but I need it before going for payment.
So I think about a solution that is make order when checkout page loaded (In fact make it ready for payment) and when checkout real complete update it.

How to get order ID in checkout page before order go for payment in WooCommerce?

I think there is some hook for this but I can't find it.


回答1:


You can use a custom function hooked in woocommerce_checkout_order_processed action hook.
Since woocommerce 3.0+ version, here Is the corresponding core code located in process_checkout() function.

// Since WooCommerce version 3.0+
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

and below WooCommerce 3.0 version:

// Since WooCommerce version 2.1+ (before version 3.0+)
do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted );

So there is 2 cases depending which version of woocommerce you are using:

Since WooCommerce 3.0+ you can use 2 additional arguments in your hooked function and you will not need to create an instance of the order object as you get $order already as an argument.
You will be able also to access the posted data directly through $posted_data argument.

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 3);
function action_checkout_order_processed( $order_id, $posted_data, $order ) {
   // Do something
}

Since WooCommerce 2.1+ (Before WooCommerce 3.0), you have only the $order_id as argument, so you might be need to get an instance of $order object with wc_get_order() function:

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 1);
function action_checkout_order_processed( $order_id ) {
   // get an instance of the order object
   $order = wc_get_order( $order_id );

   // Do something
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.




回答2:


so unfortunately you cannot access the order_id on the checkout page itself because the order has not been created yet. Though this is true, the cart data is temporarily saved as a 'session' as WooCommerce calls it. this may help you a bit with your issue with passing information to an iFrame if you can tweak it a bit.

The way you can do this is by getting the information from the WC_session:

$cart_data = WC()->session->get('cart');

Once you do that, you will be able to access the cart data hierarchy as stored by WooCommerce by using a key index such as 'product_id' :

$cart_data[array_keys($cart_data)[0]]['product_id'];

Here is the list of valid cart data keys:

key
product_id
variation_id
variation (Array)
quantity
data_hash
line_tax_data (Array)
line_subtotal
line_subtotal_tax
line_total
line_tax




回答3:


If you just look into class-wc-checkout.php file at line number 935 you will see that there is an action woocommerce_checkout_order_processed which pass $order_id as an argument.

So this should work for you:

add_action('woocommerce_checkout_order_processed', 'wh_pre_paymentcall');

function wh_pre_paymentcall($order_id) {

    //create an order instance
    $order = wc_get_order($order_id);
    //$payment_method = $order->payment_method_title;
    //$status = $order->get_status();

    // write your custom logic over here.
}

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.

Related Question: WooCommerce Get Order Product Details Before Payment in Plugin (also read the comments)

Hope this helps!



来源:https://stackoverflow.com/questions/43316644/get-the-order-id-in-checkout-page-before-payment-process

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