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

Ivi*_*pić 5 php wordpress cart woocommerce hook-woocommerce

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

我在一个插件上工作,连接woocommerce和支付API,我需要发送产品ID,名称,描述,数量和个人数量等产品详细信息.

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

我怎样才能获得这些数据?

谢谢

UPDATE

这里是基于anwers的更新,为每个需要它的人提供:

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);

    }
Run Code Online (Sandbox Code Playgroud)

感谢@loictheaztec和@ raunak-gupta

Rau*_*pta 6

\n

我认为您正在寻找woocommerce_checkout_process挂钩。 \n WC_Checkout::process_checkout()\xe2\x80\x93 在按下确认订单按钮后\n 处理结账。

\n
\n\n

这是代码:

\n\n
add_action(\'woocommerce_checkout_process\', \'wh_getCartItemBeforePayment\', 10);\n\nfunction wh_getCartItemBeforePayment()\n{\n    $items = WC()->cart->get_cart();\n\n    foreach ($items as $item => $values)\n    {\n        $_product = $values[\'data\']->post;\n        $product_title = $_product->post_title;\n        $qty = $values[\'quantity\'];\n        $price = get_post_meta($values[\'product_id\'], \'_price\', true);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

希望这可以帮助!

\n


Loi*_*tec 5

更新了 woocommerce 版本 3 及更高版本

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

由于 Woocommerce 3$cart_item['data'];不再是包含WP_Post对象的数组,而是包含WC_Product对象的数组。

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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,能在一个地方看到所有数据真是太棒了。 (2认同)