提交前如何获取WooCommerce订单数据?

时间:2018-12-12 04:01:28

标签: javascript php jquery css wordpress

我们正在使用第三方插件处理名为Klarna Checkout版本2的WooCommerce付款。

将商品添加到购物车后,Klarna Checkout会显示从其域托管的iframe

enter image description here

由于我无法编写jQuery来获取来自另一个域的iframe form的数据,因此我想到了 woocommerce挂钩来获取数据。

我尝试了以下代码,但没有数据:

add_action('woocommerce_checkout_update_order_review', 'show_klarna_data');
function show_klarna_data($posted_data) {
    global $woocommerce;
    print_r($posted_data);
}

您知道如何从woocommerce中返回数据,例如在提交前检查订单吗?

非常感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:1)

您好,您需要致电woocommerce钩子woocommerce_checkout_process来获取数据,然后才能在结帐时进行付款。使用下面的代码即可解决问题。

add_action('woocommerce_checkout_process', 'get_woo_cart_detail', 10);

    function get_woo_cart_detail()
    {
        $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

如果您使用的woocommerce版本低于3.0,则也可以使用cart object来获取类似的数据

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
    }
}