WooCommerce在购买插件之前获取订单产品详细信息 [英] WooCommerce Get Order Product Details Before Payment in Plugin

查看:325
本文介绍了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文件中.

希望这会有所帮助!

这篇关于WooCommerce在购买插件之前获取订单产品详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆