2016-10-24 3 views
0

내가 만든 사용자 정의 텍스트 필드를 필드,하지만 난 액션 후크를 사용하여 하나의 제품 페이지에 출력 할 수 없습니다입니다 WooCommerce 사용자 정의 출력

사람이 솔루션을 제공 할 수 있는지 정말 thankfull 될 것

?

내 코드 (functions.php) :

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 

// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 


function woo_add_custom_general_fields() { 

    global $woocommerce, $post; 

    echo '<div class="options_group">'; 

    // Custom fields will be created here... 

    woocommerce_wp_text_input( 
    array( 
     'id'   => '_text_field', 
     'label'  => __('My Text Field', 'woocommerce'), 
     'placeholder' => 'http://', 
     'desc_tip' => 'true', 
     'description' => __('Enter the custom value here.', 'woocommerce') 
    ) 
); 

    echo '</div>'; 

} 

function woo_add_custom_general_fields_save($post_id){ 

// Text Field 
    $woocommerce_text_field = $_POST['_text_field']; 
    if(!empty($woocommerce_text_field)) 
     update_post_meta($post_id, '_text_field', esc_attr($woocommerce_text_field)); 

} 


add_action('woocommerce_single_product_summary', 'output_custom_fields'); 


function output_custom_fields() { 
    echo get_post_meta($post->ID, '_text_field', true); 
} 

감사합니다! Denis

+0

(19) 사이의 integrer에 우선 순위를 설정해야 전역 $ 게시물을 시도있다; echo get_post_meta ($ post-> ID, '_text_field', true); ? – MirzaP

답변

1

@MirzaP가 주석에서 말한 것처럼 $postoutput_custom_fields() 함수에 정의되어 있지 않습니다. 그래서 $post->ID가 작동하려면

작동하지 않을 수

function output_custom_fields() { 
    global $post; 

    echo get_post_meta($post->ID, '_text_field', true); 
} 

이 휘는 행동의 세 번째 매개 변수를 잊지 마세요 (기능의 포스트 객체를 얻을 수), 그 의지는 우선 순위를 설정합니다. 이러한 우선 순위는 Woocommerce 템플릿에서 찾을 수 있으며 메타 데이터를 배치 할 위치에 따라 우선 순위를 사용하십시오.

/** 
    * woocommerce_single_product_summary hook 
    * 
    * @hooked woocommerce_template_single_title - 5 
    * @hooked woocommerce_template_single_price - 10 
    * @hooked woocommerce_template_single_excerpt - 20 
    * @hooked woocommerce_template_single_add_to_cart - 30 
    * @hooked woocommerce_template_single_meta - 40 
    * @hooked woocommerce_template_single_sharing - 50 
*/ 

그래서 당신은 사용자 정의 데이터가 바로 가격 한 후 표시하려는 경우, 당신은 11

add_action('woocommerce_single_product_summary', 'output_custom_fields', 15); 
관련 문제