제품 메타 데이터를 얻기 위해 주문 항목 ID를 가져오려면 어떻게 해야 합니까?
Woocommerce의 주문에서 아이템 메타값을 추출하려고 합니다.
$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );
단, 첫 번째 파라미터로 order_item_id를 취득하는 방법을 찾을 수 없습니다(get_items 사용).
global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['order_item_id']; //???
$data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
그리고 이 주문은 item_id(1과 2)입니다.
내가 어떻게 그럴 수 있지?
감사해요.
2018년 갱신:
- 가능한 2가지 사례를 사용하여 답을 명확히 하다
- woocommerce 3+ 호환성 추가
두 가지 경우가 있습니다.
1) 제품 메타 데이터 가져오기(순서 항목 메타 데이터 미설정):
Foreach 루프에서 제품 ID를 취득해야 합니다.WC_Order
이 제품의 메타데이터를 취득하려면 , 함수( )를 사용합니다.
코드는 다음과 같습니다.
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() => $item ) {
// Compatibility for woocommerce 3+
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();
// Here you get your data
$custom_field = get_post_meta( $product_id, '_tmcartepo_data', true);
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
2) 주문 항목 메타 데이터 가져오기(커스텀 필드 값):
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() as $item_id => $item ) {
// Here you get your data
$custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
커스텀 필드 데이터가 배열인 경우 Foreach 루프에서 데이터에 액세스할 수 있습니다.
// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
echo '<p>key: '.$key.' | value: '.$value.'</p>';
}
모든 코드가 테스트되어 동작한다.
주문 데이터 관련 참조:
- WooCommerce 주문 세부 정보 가져오는 방법 (woocommerce 3에도 해당)
- Woocommerce 3에서 주문 아이템과 WC_Order_Item_Product를 가져옵니다.
포어치 할 때$order->get_items()
키는 실제로는 주문라인 ID입니다.그래서:
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
}
파티에는 늦었지만 TM Extra Product Options 플러그인과 같은 점을 가지고 작업하고 있기 때문에 다음과 같은 질문에 답할 수 있다고 생각합니다.
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach( $items as $item ){
$data = unserialize($item['item_meta']['_tmcartepo_data'][0]);
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
내 경우 테스트되고 작동한다.
이것을 사용하다<pre><?php print_r($items); ?></pre>
$syslog 배열/개체의 모든 내용을 확인합니다.
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
이 솔루션은 나에게 가치가 있었습니다.meta_key에 대해 "_tmcartepo_data"를 변경해 주세요.
데이터베이스에서 주문 항목을 가져오는 간단한 방법
/**
* @param $order_id
*
* @return array|null|object
*/
function get_order_items( $order_id ) {
global $wpdb, $table_prefix;
$items = $wpdb->get_results( "SELECT * FROM `{$table_prefix}woocommerce_order_items` WHERE `order_id` = {$order_id}" );
$item_name = array();
foreach ( $items as $item ) {
$item_name[] = $item->order_item_name;
}
return $item_name;
}
언급URL : https://stackoverflow.com/questions/39390838/how-to-get-order-items-ids-to-get-some-product-meta-data
'it-source' 카테고리의 다른 글
수집되지 않은 참조 오류: ReactDOM이 정의되지 않았습니다. (0) | 2023.03.02 |
---|---|
BindingResult에서 컨트롤러 오류 텍스트를 가져오는 방법 (0) | 2023.03.02 |
Axios는 url에 들어가지만 두 번째 파라미터를 오브젝트로 하면 동작하지 않습니다. (0) | 2023.03.02 |
리액트를 사용하여 HTML5 비디오의 소스 URL 업데이트 (0) | 2023.03.02 |
Spring RestTemplate 요청에서 "Accept:" 헤더를 설정하려면 어떻게 해야 합니까? (0) | 2023.03.02 |