WooCommerce 在插件付款前获取订单产品详细信息

我需要在插件付款前显示购物车中的订单详细信息.

I need to display order details from cart before payment in plugin.

我正在开发一个连接 woocommerce 和支付 API 的插件,在那里我需要发送一系列产品详细信息,例如产品 ID、名称、描述、数量和个人金额.

I work on one plugin what connect woocommerce and an payment API and there I need to send array of product details like product ID, name, description, quantity and individual amount.

我的问题是我找不到正确的钩子来正确获取所有数据.

My problem is that I can't find right hook to get all data properly.

如何获取这些数据?

谢谢

以下是针对需要它的每个人的答案的更新:

Here is update based on anwers for everyone who need it:

add_action('woocommerce_checkout_process', 'woocommerce_get_data', 10);
function woocommerce_get_data(){

        $cart = array();
        $items = WC()->cart->get_cart();
        foreach($items as $i=>$fetch){
            $item = $fetch['data']->post;

            $cart[]=array(
                'code'        => $fetch['product_id'], 
                'name'        => $item->post_title, 
                'description' => $item->post_content, 
                'quantity'    => $fetch['quantity'], 
                'amount'      => get_post_meta($fetch['product_id'], '_price', true)
            );
        }

        $user = wp_get_current_user();

        $data = array(
            'total' => WC()->cart->total,
            'cart'  => $cart,
            'user'  => array(
                'id' => $user->ID,
                'name' => join(' ',array_filter(array($user->user_firstname, $user->user_lastname))),
                'mail' => $user->user_email,
            )
        );

        $_SESSION['woo_data']=json_encode($data);

    }

感谢@loictheaztec 和@raunak-gupta

Thanks to @loictheaztec and @raunak-gupta

推荐答案

我认为您正在寻找 woocommerce_checkout_process 挂钩.WC_Checkout::process_checkout() – 在按下确认订单按钮.

I think you are looking for woocommerce_checkout_process hook. WC_Checkout::process_checkout() – Process the checkout after the confirm order button is pressed.

代码如下:

add_action('woocommerce_checkout_process', 'wh_getCartItemBeforePayment', 10);

function wh_getCartItemBeforePayment()
{
    $items = WC()->cart->get_cart();

    foreach ($items as $item => $values)
    {
        $_product = $values['data']->post;
        $product_title = $_product->post_title;
        $qty = $values['quantity'];
        $price = get_post_meta($values['product_id'], '_price', true);
    }
}

代码位于活动子主题(或主题)的 function.php 文件中.或者也可以在任何插件 php 文件中.

希望这有帮助!

相关文章