簡體   English   中英

在Woocommerce中的產品簡短描述后顯示自定義字段值

[英]Displaying custom field values after product short description in Woocommerce

我被要求在我們設計的woocommerce網站上添加一些自定義字段。 我已使用CSV導入工具中的meta:字段添加它們,並且已成功將信息作為自定義字段導入產品頁面。

困難的部分是,我希望能夠在產品頁面上顯示自定義字段,我已經檢查了文檔和在線論壇,沒有太多運氣。

如何在單個產品頁面的產品摘錄下顯示自定義字段值?

任何幫助表示贊賞

由於此自定義字段數據是產品發布元數據 (位於wp_postmeta數據庫表中) ,因此您有兩種方法可以使用以下方法獲取元數據:

以下代碼將在產品簡短描述后顯示您的自定義字段。 您需要在代碼_custom_meta_key替換真正的元鍵slug:

// Display a product custom field after product short description
add_action( 'woocommerce_single_product_summary', 'display_product_custom_field', 25 );
function display_product_custom_field(){
    global $product;

    $value = $product->get_meta('_custom_meta_key', true ); 
    // OR
    // $value = get_post_meta( $product->get_id(), '_custom_meta_key', true );

    if( ! empty( $value ) )
        echo '<p>'.__('My value', 'woocommerce') . ': ' . $value . '</p>';
}

代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

為什么不嘗試在woocommerce中編輯文件: single-product.php or content-single-product.php

然后使用高級自定義字段顯示您的內容 - https://www.advancedcustomfields.com/resources/text/

<h2><?php the_field('text'); ?></h2>

如果您不想將WooCommerce模板文件復制並自定義到您自己的主題中,可以在functions.php中使用WooCommerce掛鈎:

// Add ACF field data to Product page
add_action( 'woocommerce_after_single_product_summary', 'my_acf_product_content', 10 );
function my_acf_product_content() {

    $test_field = get_field( 'test_field' );

    if ( $test_field ) {
        echo '<h3>ACF Content Header</h3>';
        echo '<div>' . $test_field . '</div>';
    }
}

有關要在產品頁面上輸出字段數據的位置的參考(在示例中為“woocommerce_after_single_product_summary”),您可以參考此鏈接,我認為該鏈接仍應相關: https ://businessbloomer.com/woocommerce- 視覺鈎導向單品頁

暫無
暫無

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

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