問題描述
我需要在插件付款前顯示購物車中的訂單詳細(xì)信息.
I need to display order details from cart before payment in plugin.
我正在開發(fā)一個(gè)連接 woocommerce 和支付 API 的插件,在那里我需要發(fā)送一系列產(chǎn)品詳細(xì)信息,例如產(chǎn)品 ID、名稱、描述、數(shù)量和個(gè)人金額.
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.
我的問題是我找不到正確的鉤子來正確獲取所有數(shù)據(jù).
My problem is that I can't find right hook to get all data properly.
如何獲取這些數(shù)據(jù)?
謝謝
以下是針對(duì)需要它的每個(gè)人的答案的更新:
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);
}
感謝@loictheaztec 和@raunak-gupta
Thanks to @loictheaztec and @raunak-gupta
推薦答案
我認(rèn)為您正在尋找
woocommerce_checkout_process
掛鉤.WC_Checkout::process_checkout()
– 在按下確認(rèn)訂單按鈕.
I think you are looking for
woocommerce_checkout_process
hook.WC_Checkout::process_checkout()
– Process the checkout after the confirm order button is pressed.
代碼如下:
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);
}
}
代碼位于活動(dòng)子主題(或主題)的 function.php 文件中.或者也可以在任何插件 php 文件中.
希望這有幫助!
這篇關(guān)于WooCommerce 在插件付款前獲取訂單產(chǎn)品詳細(xì)信息的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!