WooCommerce Get Order Product Details Before Payment in Plugin

前端 未结 2 1135
生来不讨喜
生来不讨喜 2020-12-17 02:42

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 o

相关标签:
2条回答
  • 2020-12-17 03:05

    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!

    0 讨论(0)
  • 2020-12-17 03:07

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题