2016-12-23 13 views
3

私はWooCommerceをカスタマイズしており、商品ページにカスタムテキスト(条件とブランド)を追加して表示したいと考えています。単一の商品ページでSKUのカスタム項目値を表示

位置は「在庫あり」または「SKU」メタのいずれかです。私はカスタムフィールドを作成して保存することができましたが、これらのメタ値を製品ページに印刷する方法はありました。

助けてください!に、今

// Enabling and Displaying Fields in backend 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
function woo_add_custom_general_fields() { 
    global $woocommerce, $post; 

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

    woocommerce_wp_text_input(array(// Text Field type 
     'id'   => '_conditions', 
     'label'  => __('Conditions', 'woocommerce'), 
     'placeholder' => 'i.e: brand-new; refurbished; defected...', 
     'desc_tip' => 'true', 
     'description' => __('Enter the conditions of the products here.', 'woocommerce') 
    )); 

    woocommerce_wp_text_input(array(// Text Field type 
     'id'   => '_brands', // ===> NOT '_bands' 
     'label'  => __('Brands', 'woocommerce'), 
     'placeholder' => 'i.e: Lacoste; Hugo Boss...etc', 
     'desc_tip' => 'true', 
     'description' => __('Enter names of the Brands of the products if any.', 'woocommerce') 
    )); 

    echo '</div>'; // Closing </div> tag HERE 
} 

// Save Fields values to database when submitted (Backend) 
add_action('woocommerce_process_product_meta', 'woo_save_custom_general_fields'); 
function woo_save_custom_general_fields($post_id){ 

    // Saving "Conditions" field key/value 
    $conditions_field = $_POST['_conditions']; 
    if(!empty($conditions_field)) 
     update_post_meta($post_id, '_conditions', esc_attr($conditions_field)); 

    // Saving "Brands" field key/value 
    $brands_field = $_POST['_brands']; 
    if(!empty($brands_field)) 
     update_post_meta($post_id, '_brands', esc_attr($brands_field)); 
} 

:あなたの質問に提供されたコードが不完全であり、そのようなものでなければなりません

答えて

4

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
// Text Field 
function woo_add_custom_general_fields() { 
    global $woocommerce, $post; 
    echo '<div class="options_group">'; 
    woocommerce_wp_text_input( 
    array( 

     'id'   => '_conditions', 
     'label'  => __('Conditions', 'woocommerce'), 
     'placeholder' => 'i.e: brand-new; refurbished; defected...', 
     'desc_tip' => 'true', 
     'description' => __('Enter the conditions of the products here.', 'woocommerce') 
    ) 
); 
    echo '</div>'; 
    woocommerce_wp_text_input( 
    array( 

     'id'   => '_bands', 
     'label'  => __('Brands', 'woocommerce'), 
     'placeholder' => 'i.e: Lacoste; Hugo Boss...etc', 
     'desc_tip' => 'true', 
     'description' => __('Enter names of the Brands of the products if any.', 'woocommerce') 
    ) 
); 
} 
// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

ありがとう:ここ

は私の functions.phpのコードですこのメタデータの値を製品ページに表示すると、 get_post_meta()がフックドファンクションで機能しますに。ここでは以下はその優先順位(表示順)で woocommerce_single_product_summaryフックのすべてのフックテンプレートが表示されます。

/** 
    * 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 
*/ 

「在庫」や「SKU」のデータが優先権を持ってwoocommerce_template_single_metaで表示されますすぐに税関項目値を表示する必要があります。次に、優先度をにすることができます。ここで

「SKU」の中で、製品ページに出力するカスタムフィールドの値を起こっているコードです:

add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45); 
function woo_display_custom_general_fields_values() { 
    global $product; 

    echo '<p class="custom-conditions">Conditions: ' . get_post_meta($product->id, '_conditions', true) . '</p>'; 
    echo '<p class="custom-brands">Brands: ' . get_post_meta($product->id, '_brands', true) . '</p>'; 
} 

コードは(あなたのアクティブな子テーマのfunction.phpファイルに行きますアクティブなテーマまたは任意のプラグインファイル)。

このコードはテスト済みであり、動作します。

関連する問題