繁体   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