6

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

UPDATE

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

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78

2 Answers2

6

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!

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
  • I owe you a beer! THANKS! – Ivijan Stefan Stipić Mar 14 '17 at 11:34
  • ha ha, cheers, I haven't tested but read the doc you can also pass `$order_id` and can then get all the details from their, but as I have not tested so I cannot be sure. – Raunak Gupta Mar 14 '17 at 11:37
  • I will test and see how works. Tell me, to get total amount just need to use `WC()->cart->total` right? – Ivijan Stefan Stipić Mar 14 '17 at 11:42
  • 1
    yes @IvijanStefanStipić; `WC()->cart->total` will give you the cart total. – Raunak Gupta Mar 14 '17 at 11:44
  • 1
    So to resume: As nothing has been processed yet, there is no existing Order yet, so no `$order_id` available (and when looking at the source code where this hook is located, there is no available arguments for that hook. But you don't need any). – LoicTheAztec Mar 14 '17 at 12:02
  • @LoicTheAztec I'm new in woocommerce development, tell me, with this hook I'm able to get data before checkout right? I not need order ID yet because I need first to proccess this data to 3rd party and when there is payment successfull I will return data to woocommerce and made all processed – Ivijan Stefan Stipić Mar 14 '17 at 12:07
  • @IvijanStefanStipić **Yes this hook is convenient** (as most hooks in checkout page), as you are using the cart data. What is **not going to work is the `$user` object data** depending if user is logged in or not. You have to test this. With this hook you could try to get the `$_POST` user data (if not logged in). – LoicTheAztec Mar 14 '17 at 12:17
  • Yes user need to be logged to can pay. Thanks! – Ivijan Stefan Stipić Mar 14 '17 at 12:20
  • Just checked `class-wc-checkout.php` there is another action `woocommerce_checkout_order_processed` which is called just before payment and has `$order_id` as argument, you can try this. – Raunak Gupta Mar 14 '17 at 12:21
  • @IvijanStefanStipić In this case the user data will be defined then. – LoicTheAztec Mar 14 '17 at 12:21
5

Updated for woocommerce version 3 and above

Here is all the cart items data you can get with the cart object:

1) For woocommerce version 3 and above:

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) For Woocommerce before version 3:

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
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399