我需要在插件付款之前显示购物车中的订单详情。
我在一个插件上工作,连接woocommerce和支付API,我需要发送产品ID,名称,描述,数量和个人数量等产品详细信息。
我的问题是我找不到正确的钩子来正确获取所有数据。
如何获取此数据?
由于
以下是基于每个人的需求的更新:
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);
}
感谢@loictheaztec和@ raunak-gupta
答案 0 :(得分:3)
针对woocommerce版本3及更高版本
进行了更新
以下是购物车对象可以获得的所有购物车商品数据:
1)对于woocommerce 3及以上版本:
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
}
}
由于Woocommerce 3
$cart_item['data'];
不再是包含WP_Post
对象的数组,而是WC_Product
对象。
2)对于版本3之前的Woocommerce:
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
}
}
答案 1 :(得分:2)
我认为你正在寻找
woocommerce_checkout_process
钩子。WC_Checkout::process_checkout()
- 在结束后处理结帐 按下确认订单按钮。
以下是代码:
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);
}
}
代码进入活动子主题(或主题)的function.php文件。或者也可以在任何插件php文件中。
希望这有帮助!