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

WooCommerce Get Order Product Details Before Payment in Plugin

我需要在插件中付款前显示购物车中的订单详情。

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

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

如何获取这些数据?

谢谢

更新

这里是根据所有需要它的人的答案更新:

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

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 文件中。

希望对您有所帮助!

Updated for woocommerce version 3 and above

这是您可以通过购物车对象获得的所有购物车商品数据:

1) 对于 woocommerce 版本 3 及以上:

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $product_id = $cart_item['product_id']; // Product ID
    $product_obj = $cart_item['data']; // Product Object
    $product_qty = $cart_item['quantity']; // Product quantity
    $product_price = $cart_item['data']->get_price(); // Product price
    $product_total_stock = $cart_item['data']->get_stock_quantity(); // Product stock quantity
    $product_type = $cart_item['data']->get_type(); // Product type
    $product_name = $cart_item['data']->get_name(); // Product Title (Name)
    $product_description = $cart_item['data']->get_description(); // Product description
    $product_excerpt = $cart_item['data']->get_short_description(); // Product short description


    $cart_line_subtotal = $cart_item['line_subtotal']; // Cart item line subtotal
    $cart_line_subtotal_tax = $cart_item['line_subtotal_tax']; // Cart item line tax subtotal
    $cart_line_total = $cart_item['line_total']; // Cart item line total
    $cart_line_tax = $cart_item['line_tax']; // Cart item line tax total

    // variable products
    $variation_id = $cart_item['variation_id']; // Product Variation ID
    if($variation_id != 0){
        $product_variation_obj = $cart_item['data']; // Product variation Object
        $variation_array = $cart_item['variation']; // variation attributes + values
    }
}

Since Woocommerce 3 $cart_item['data']; is not anymore an array with the WP_Post object, but the WC_Product object.

2) 对于版本 3 之前的 Woocommerce:

foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    $product_id = $cart_item['product_id']; // Product ID
    $product_obj = wc_get_product($product_id); // Product Object
    $product_qty = $cart_item['quantity']; // Product quantity
    $product_price = $cart_item['data']->price; // Product price
    $product_total_stock = $cart_item['data']->total_stock; // Product stock
    $product_type = $cart_item['data']->product_type; // Product type
    $product_name = $cart_item['data']->post->post_title; // Product Title (Name)
    $product_slug = $cart_item['data']->post->post_name; // Product Slug
    $product_description = $cart_item['data']->post->post_content; // Product description
    $product_excerpt = $cart_item['data']->post->post_excerpt; // Product short description
    $product_post_type = $cart_item['data']->post->post_type; // Product post type

    $cart_line_total = $cart_item['line_total']; // Cart item line total
    $cart_line_tax = $cart_item['line_tax']; // Cart item line tax total
    $cart_line_subtotal = $cart_item['line_subtotal']; // Cart item line subtotal
    $cart_line_subtotal_tax = $cart_item['line_subtotal_tax']; // Cart item line tax subtotal

    // variable products
    $variation_id = $cart_item['variation_id']; // Product Variation ID
    if($variation_id != 0){
        $product_variation_obj = wc_get_product($variation_id); // Product variation Object
        $variation_array = $cart_item['variation']; // variation attributes + values
    }
}