简体   繁体   English

在Woocommerce中的产品简短描述后显示自定义字段值

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

I've been asked to add some custom fields to a woocommerce site we designed. 我被要求在我们设计的woocommerce网站上添加一些自定义字段。 I've added them using the meta: field in the CSV import tool and it has successfully imported the info into the product page as a custom field. 我已使用CSV导入工具中的meta:字段添加它们,并且已成功将信息作为自定义字段导入产品页面。

The difficult part is, I want to be able to display the custom field on the product page, I've checked the documentation and online forums without much luck. 困难的部分是,我希望能够在产品页面上显示自定义字段,我已经检查了文档和在线论坛,没有太多运气。

How can I display the custom field value under the product excerpt in single product pages? 如何在单个产品页面的产品摘录下显示自定义字段值?

Any help is appreciated 任何帮助表示赞赏

As this custom fields data are product post meta data (located in wp_postmeta database table) , you have 2 alternatives to get the meta values using: 由于此自定义字段数据是产品发布元数据 (位于wp_postmeta数据库表中) ,因此您有两种方法可以使用以下方法获取元数据:

The following code will display your custom field after the product short description. 以下代码将在产品简短描述后显示您的自定义字段。 You will need to replace in the code _custom_meta_key by your real meta key slug: 您需要在代码_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>';
}

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

Why dont you try editing the file in woocommerce: single-product.php or content-single-product.php 为什么不尝试在woocommerce中编辑文件: single-product.php or content-single-product.php

Then use advanced custom fields to display your content - https://www.advancedcustomfields.com/resources/text/ 然后使用高级自定义字段显示您的内容 - https://www.advancedcustomfields.com/resources/text/

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

You can use a WooCommerce hook in your functions.php if you don't want to copy and customize a WooCommerce template file into your own theme: 如果您不想将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>';
    }
}

For reference on where you want to output the field data on the product page (in the example case "woocommerce_after_single_product_summary"), you can refer to this link, which I believe should still be relevant: https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page 有关要在产品页面上输出字段数据的位置的参考(在示例中为“woocommerce_after_single_product_summary”),您可以参考此链接,我认为该链接仍应相关: https ://businessbloomer.com/woocommerce- 视觉钩导向单品页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM