체크아웃 후 주문 데이터 가져오기
WooCommerce에서는 고객님의 체크아웃이 완료되면 API로 요청을 보내고 싶습니다.기본적으로 온라인 코스(유드미 등)를 판매하고 있는 사이트입니다.
고객이 체크아웃할 때 API 요청을 보내서 해당 과정에 사용자를 등록하고 싶습니다.WooCommerce 훅을 여러 번 사용해 봤지만 아무 것도 작동하지 않았습니다.
사용하고 있는 코드는 다음과 같습니다.
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student($order_id)
{
echo $order_id;
echo "Hooked";
}
플러그인용 코드를 작성하고 있으며, 쉽게 하기 위해 현재 Cash on Delivery 방식을 사용하고 있습니다.
체크아웃할 때 인쇄 중인 메시지 또는 "hooked"가 표시되지 않기 때문에 잘못된 부분을 지적할 수 있는 사람이 있습니까?
성공 페이지로 이동하여 인쇄 중인 이 두 가지를 표시하지 않습니다.
업데이트 2 Woocommerce 3+ 전용(코드를 1회만 실행하도록 제한 추가)
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Allow code execution only once
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get the order key
$order_key = $order->get_order_key();
// Get the order number
$order_key = $order->get_order_number();
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object
$product = $item->get_product();
// Get the product Id
$product_id = $product->get_id();
// Get the product name
$product_id = $item->get_name();
}
// Output some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
// Flag the action as done (to avoid repetitions on reload for example)
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}
코드가 기능합니다.php 파일 또는 임의의 플러그인 파일에 있는 활성 자식 테마(또는 테마)입니다.
관련 스레드:
코드가 테스트되어 동작.
업데이트됨(코멘트 요청대로 주문 항목에서 제품 ID를 가져오기 위해)
대신 후크를 사용하면 다음과 같이 주문 접수 페이지에 에코 코드가 표시됩니다.
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
코드가 기능합니다.php 파일 또는 임의의 플러그인 파일에 있는 활성 자식 테마(또는 테마)입니다.
코드가 테스트되어 동작.
그런 다음 개체에 대한 모든 클래스 메서드를 사용할 수 있습니다.
관련:
'woocommerce_thank you' 후크가 아니라 'woocommerce_checkout_order_processed' 후크가 관련 후크입니다.'woocommerce_commerce_commer_order_commer' 후크는 한 번만 호출되며 각 제품에 메타를 추가하고 코드를 한 번만 실행하는지 확인하기 위해 추가 호출을 수행할 필요가 없습니다.woocommerce_thankout'은 감사 페이지가 로드될 때마다 여러 번 호출할 수 있습니다.교체하다add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
함께
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
메타 코드와 체크를 제거합니다.업데이트된 코드는
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
다음 방법으로 주문 항목을 얻을 수 있습니다.
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
언급URL : https://stackoverflow.com/questions/42530626/getting-order-data-after-successful-checkout-hook
'programing' 카테고리의 다른 글
포스트그레스 집약함수에서의 DISIGNITY ON (0) | 2023.03.19 |
---|---|
사용자 지정 포트에 Wordpress 다중 사이트(네트워크)를 설치할 수 없음 (0) | 2023.03.19 |
보내기 전에 $.ajax를 중지 (0) | 2023.03.19 |
콜 앵귤러문서 준비 완료 시 JS 함수 (0) | 2023.03.19 |
Angular.js는 컨트롤러를 앱 네임스페이스로 정리합니다. (0) | 2023.03.19 |