WooCommerce : 관리 주문 페이지에 사용자 지정 메타박스 추가
현재 WooCommerce 제품 페이지에 다음 값을 표시하는 필드를 성공적으로 추가하고 있습니다.
- 카트(프런트 엔드),
- 체크 아웃 페이지(프런트 엔드),
- 주문 페이지(프런트 엔드),
- admin 개별 주문 페이지(백엔드)에 있습니다.
문제:관리 주문 "커스텀 필드" 메타박스에는 값이 포함된 커스텀 필드로 표시되지 않고 주문 페이지에 텍스트로 표시됩니다.
작업 코드는 다음과 같습니다.
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
echo '<label>fill in this field</label> <input type="text" name="my_field_name">';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['my_field_name'] ) ) {
$custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// This is what I think needs changing?
function subscription_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "My Field", $values['my_field_name'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'subscription_order_meta_handler', 1, 3 );
변경해야 할 것은 이 코드의 마지막 부분이라고 생각합니다.현재 주문 항목 아래에 텍스트가 표시되어 있기 때문에 조정이 필요할 수 있습니다.wc_add_order_item_meta
다른 무언가로요?
다 해봤는데 효과가 없는 것 같아요.체크 아웃 페이지에 내 필드가 있을 때는 작동시킬 수 있지만, 제품 페이지에서 꺼낼 때는 작동하지 않습니다.
체크 아웃 프로세스의 스니펫이 없는 것은 아닐까요?
업데이트 2017/11/02 (Woocommerce 3+에서 완벽하게 동작)
첫째, 기대했던 대로의 가치를 얻는 것 이외에는 모든 것이 잘 작동하고 있습니다.my_field_name
주문 페이지의 "사용자 지정 필드" 메타박스 백엔드에서 선택합니다.
그리고 정말 악몽을 꾼 후, 나는 이전보다 더 나은 꽤 괜찮은 해결책을 찾았다.백엔드에는 커스텀필드가 있는 커스텀 메타박스가 있습니다.my_field_name
다음 스크린샷과 같이 올바른 값을 표시합니다.
내 코드는 두 부분으로 나뉘어져 있다.
- [ Order ]페이지의 [Backend Metabox]페이지에는 제품 페이지(프런트 엔드)의 커스텀필드에서 올바른 값이 표시되는 편집 가능한 필드가 있습니다.
// Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
if ( ! function_exists( 'mv_add_meta_boxes' ) )
{
function mv_add_meta_boxes()
{
add_meta_box( 'mv_other_fields', __('My Field','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' );
}
}
// Adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'mv_add_other_fields_for_packaging' ) )
{
function mv_add_other_fields_for_packaging()
{
global $post;
$meta_field_data = get_post_meta( $post->ID, '_my_field_slug', true ) ? get_post_meta( $post->ID, '_my_field_slug', true ) : '';
echo '<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">
<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
<input type="text" style="width:250px;" name="my_field_name" placeholder="' . $meta_field_data . '" value="' . $meta_field_data . '"></p>';
}
}
// Save the data of the Meta field
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_save_wc_order_other_fields( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, '_my_field_slug', $_POST[ 'my_field_name' ] );
}
}
- 프론트 엔드/백 엔드:
• 제품 페이지 커스텀 필드(프런트 엔드)
• 이 데이터를 카트, 체크아웃 페이지 및 감사 주문(프런트 엔드)에 표시한다.
• 주문 페이지(백엔드)에 데이터 표시
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_product_field');
function my_custom_product_field() {
echo '<div id="my_custom_field">
<label>' . __( 'My Field') . ' </label>
<input type="text" name="my_field_name" value="">
</div><br>';
}
// Store custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'my_order_data', $_REQUEST['my_field_name'] );
}
return $cart_item_data;
}
// Add a hidden field with the correct value to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
$value = WC()->session->get( 'my_order_data' );
echo '<div id="my_custom_checkout_field">
<input type="hidden" class="input-hidden" name="my_field_name" id="my_field_name" value="' . $value . '">
</div>';
}
// Save the order meta with hidden field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, '_my_field_slug', $_POST['my_field_name'] );
}
}
// Display field value on the order edit page (not in custom fields metabox)
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
$my_custom_field = get_post_meta( $order->id, '_my_field_slug', true );
if ( ! empty( $my_custom_field ) ) {
echo '<p><strong>'. __("My Field", "woocommerce").':</strong> ' . get_post_meta( $order->id, '_my_field_slug', true ) . '</p>';
}
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
if( !empty( $cart_data ) ) $custom_items = $cart_data;
if( isset( $cart_item['my_field_name'] ) )
$custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
return $custom_items;
}
// Add the information as meta data so that it can be seen as part of the order
add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta', 10, 3 );
function add_values_to_order_item_meta( $item_id, $cart_item, $cart_item_key ) {
// lets add the meta data to the order (with a label as key slug)
if( ! empty( $cart_item['my_field_name'] ) )
wc_add_order_item_meta($item_id, __('My field label name'), $cart_item['my_field_name'], true);
}
이제 모든 것이 예상대로 작동하고 있다.
언급URL : https://stackoverflow.com/questions/37772912/woocommerce-add-custom-metabox-to-admin-order-page
'sourcetip' 카테고리의 다른 글
JSON을 문자열로 변환하는 방법 (0) | 2023.02.27 |
---|---|
모든 기능을 선택/선택 해제하고 값을 결정하는 angular.js 체크박스를 가져오려면 어떻게 해야 합니까? (0) | 2023.02.27 |
$190 get 파라미터가 동작하지 않는다 (0) | 2023.02.27 |
GSON - String에서 JSON 값을 가져옵니다. (0) | 2023.02.18 |
경로 "_id"에서 값 XXX에 대한 Mongoose 오류 ObjectId에 캐스트 실패했습니다. (0) | 2023.02.17 |