简体   繁体   English

通过钩子在 WooCommerce 单个产品页面中显示自定义字段值

[英]Display custom field value in WooCommerce single product pages via hook

I don't know much about PHP yet, but I'm trying to place some content in my single product pages by using the woocommerce_before_add_to_cart_form hook.我还不太了解 PHP,但我正在尝试使用woocommerce_before_add_to_cart_form挂钩在我的单个产品页面中放置一些内容。

I succeeded in making a code that prints my text and the Total sales of my products.我成功地制作了一个代码来打印我的文字和我的产品的总销售额。 The code looks like this:代码如下所示:

add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 );
  
function production_time() {
   global $product;
   $production_time = $product->get_total_sales();
   if ( $production_time ) echo '<p class="ri ri-clock">' . sprintf( __( ' Productietijd: %s', 'woocommerce' ), $production_time ) . '</p>';
}

But instead of product total sales I want my product custom field value to be displayed.但我希望显示我的产品自定义字段值,而不是产品总销售额。

This is the custom field I added fullfilment_production_time custom field:这是我添加的自定义字段fullfilment_production_time自定义字段:

自定义字段

I tried changing the get_total_sales() to get_fullfilment_production_time() but that didn't work.我尝试将get_total_sales()更改为get_fullfilment_production_time()但这没有用。

I also tried this one:我也试过这个:

add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 );
  
function production_time() {
   global $product;
   $production_time = $product->get_post_meta( get_the_ID(), 'fullfilment_production_time', true );
   if ( $production_time ) echo '<p class="ri ri-clock">' . sprintf( __( ' Productietijd: %s', 'woocommerce' ), $production_time ) . '</p>';
}

Any advice?有什么建议吗?

There is a mistake in your code when using get_post_meta() function in a wrong way.以错误的方式使用get_post_meta()函数时,您的代码中存在错误。 You can use different ways to get a product custom field value:您可以使用不同的方式获取产品自定义字段值:

  1. The WooCommerce way using WC_Data method get_meta() this way: WooCommerce 方式使用WC_Data方法get_meta()这种方式:
add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 ); 
function production_time() {
    global $product;

    $production_time = $product->get_meta( 'fullfilment_production_time' );

    if ( ! empty($production_time) ) {
        echo '<p class="ri ri-clock">' . sprintf( __( ' Productietijd: %s', 'woocommerce' ), $production_time ) . '</p>';
    }
}
  1. The old Wordpress way using get_post_meta() function this way:使用get_post_meta()函数的旧 Wordpress 方式如下:
add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 ); 
function production_time() {
    global $product;

    $production_time = get_post_meta( $product->get_id(), 'fullfilment_production_time', true ); 
   
    if ( ! empty($production_time) ) {
        echo '<p class="ri ri-clock">' . sprintf( __( ' Productietijd: %s', 'woocommerce' ), $production_time ) . '</p>';
    }
}
  1. For Advanced custom fields plugin using get_field() function this way:对于使用get_field()函数这样的高级自定义字段插件
add_action( 'woocommerce_before_add_to_cart_form', 'production_time', 11 ); 
function production_time() {
    global $product;

    $production_time = get_field( 'fullfilment_production_time', $product->get_id() ); 
   
    if ( ! empty($production_time) ) {
        echo '<p class="ri ri-clock">' . sprintf( __( ' Productietijd: %s', 'woocommerce' ), $production_time ) . '</p>';
    }
}

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

相关问题 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages 在WooCommerce单一产品和购物车页面中输出自定义字段值 - Output a custom field value in WooCommerce single product and cart pages 在 WooCommerce 管理订单页面中保留并显示产品自定义字段值 - Keep and display product custom field value in WooCommerce Admin Order pages 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 如何在 WooCommerce 中显示产品自定义字段值? - How display a product custom field value in WooCommerce? 在Woocommerce中查找并​​显示产品自定义字段值 - Find and display a product custom field value in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM