반응형
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 에게 감사드립니다.
갈고리를 찾으시는 것 같네요.
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);
}
}
코드가 작동합니다.활성 하위 테마(또는 테마)의 php 파일입니다.또는 플러그인 php 파일에서도 마찬가지입니다.
도움이 되길 바랍니다!
우커머스 버전 3 이상용으로 업데이트됨
카트 개체로 얻을 수 있는 모든 카트 항목 데이터는 다음과 같습니다.
1) 우커머스 버전 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
}
}
우커머스 3 이후로
$cart_item['data'];
더 이상은 더 이상의 배열이 아닙니다.WP_Post
객체, 그러나 객체.
2) 버전 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
}
}
언급URL : https://stackoverflow.com/questions/42784075/woocommerce-get-order-product-details-before-payment-in-plugin
반응형
'source' 카테고리의 다른 글
SQL에서 문자열을 비교하려면 '=' 또는 'LIKE'를 사용하시겠습니까? (0) | 2023.10.16 |
---|---|
var.replace가 함수가 아닙니다. (0) | 2023.10.16 |
두 개의 enum에는 몇 가지 공통 요소가 있는데, 왜 오류가 발생합니까? (0) | 2023.10.16 |
HTML5 History API 사용하기 좋은 튜토리얼 (Pushstate?) (0) | 2023.10.16 |
ASP에서 장시간 실행 중인 서버 호출의 진행률 표시줄.순 MVC (0) | 2023.10.16 |