我们正在使用名为 Klarna Checkout Version 2 的第 3 方插件来处理 WooCommerce 的付款。
在将产品添加到购物车后,Klarna Checkout 会显示从他们的域托管的 iframe
。
由于我无法编写 jQuery 来获取从另一个域托管的 iframe 表单
的数据,我正在考虑使用 woocommerce hook 来获取数据。
我尝试了下面的代码,但没有得到任何数据:
add_action('woocommerce_checkout_update_order_review', 'show_klarna_data');
function show_klarna_data($posted_data) {
global $woocommerce;
print_r($posted_data);
}
您知道如何从 woocommerce 返回数据,例如在提交前查看订单吗?
非常感谢您的帮助。谢谢
最佳答案
您好,您需要调用 woocommerce 钩子(Hook) 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
如果您使用低于 3.0 的 woocommerce 版本,您也可以使用 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
}
}
关于javascript - 如何在提交前获取 WooCommerce 订单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53735871/