I need to display order details from cart before payment in plugin.
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.
How can I get this data?
Thanks
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);
}
Thanks to @loictheaztec and @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.
Here is the code:
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);
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With