簡體   English   中英

在 Woocommerce 單個產品頁面的簡短描述下顯示自定義字段

[英]Display a custom field under short description in Woocommerce single product pages

在 woocommerce 中,我使用一些代碼在產品編輯頁面中添加一個帶有自定義字段的 Metabox。

如何在單個產品頁面的簡短描述下顯示此自定義字段的值?

這是我的代碼:

add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
    add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}


function info_meta_fields_output($post)
{
    $new_meta = get_post_meta($post->ID,'_new_meta',true);
    echo ('<label for="new_meta"> Custom Text </label>');
    echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}

add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
    $new_meta=sanitize_text_field($_POST['new_meta']);
    update_post_meta ($post_id,'_new_meta',$new_meta);
}


// Displaying the value on single product pages
function meta_product($product_id) {

    $new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
    echo ('<p id="value-on-single-product">' . $new_meta2 . '</p>');
}
add_action('woocommerce_single_product_summary', 'meta_product',30);

但它不顯示自定義字段值。

更新(在評論中添加了第二個自定義字段)

您應該嘗試以下操作,這將在產品常規選項卡 Metabox 中設置您的自定義字段,並將在產品簡短描述下顯示此自定義字段值:

// Add the custom field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_to_general_product_metabox' );
function add_custom_field_to_general_product_metabox() {
    global $post;

    // Get the selected value
    $value = get_post_meta( $post->ID, '_new_meta', true );
    if( empty( $value ) ) $value = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value, // Displaying the selected value
    ) );


    // Get the selected value
    $value2 = get_post_meta( $post->ID, '_new_meta2', true );
    if( empty( $value2 ) ) $value2 = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta2',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value2, // Displaying the selected value
    ) );
}

// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_to_general_product_metabox' );
function save_custom_field_to_general_product_metabox( $post_id ){

    if( isset( $_POST['new_meta'] ) )
        update_post_meta( $post_id, '_new_meta', esc_attr( $_POST['new_meta'] ) );

    if( isset( $_POST['new_meta2'] ) )
        update_post_meta( $post_id, '_new_meta2', esc_attr( $_POST['new_meta2'] ) );
}


// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_single_product_summary', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
    global $product;

    $custom_field = get_post_meta( $product->get_id(),'_new_meta', true );
    if( ! empty( $custom_field ) )
        echo  '<p id="value-on-single-product">' . $custom_field . '</p>';

    $custom_field2 = get_post_meta( $product->get_id(),'_new_meta2', true );
    if( ! empty( $custom_field2 ) )
        echo '<p id="value-on-single-product">' . $custom_field2 . '</p>';
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。

測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM