在WooCommerce中简短描述之前显示自定义产品字段 [英] Display a custom product field before short description in WooCommerce

查看:163
本文介绍了在WooCommerce中简短描述之前显示自定义产品字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在简短描述之前,我在WooCommerce中添加新字段时遇到问题。



我在 functions.php中使用了脚本和我的新自定义字段正确显示,但:




  • 使用脚本时,简短描述消失了,新字段显示正常

  • 我可以在产品页面上编辑字段的内容,但不能删除它。它始终是最后一个值。



如何在不删除产品简短说明的情况下显示此自定义字段?

如何重置该自定义字段内容?



这是我的代码:

  //添加一个自定义字段
add_action('woocommerce_product_options_general_product_data','woo_add_custom_general_fields');

函数woo_add_custom_general_fields(){

全球$ woocommerce,$ post;

woocommerce_wp_text_input(
array(
'id'=>'_text_field',
'label'=> __('Termin dostawy','woocommerce' ),
'占位符'>>'np:7 dni',
'desc_tip'=>'true',
'description'=> __('Wpisz przewidywany termin dostawy ,np:7 dni。','woocommerce')

);

}

//保存自定义字段
add_action('woocommerce_process_product_meta','woo_add_custom_general_fields_save');
函数woo_add_custom_general_fields_save($ post_id){
$ woocommerce_text_field = $ _POST [’_ text_field’];
if(!empty($ woocommerce_text_field))
update_post_meta($ post_id,'_text_field',esc_attr($ woocommerce_text_field)));

}

//显示自定义字段
add_action(‘woocommerce_short_description’,‘magik_custom_text’,10,1);
函数magik_custom_text()
{
全球$ product;

全球$ woocommerce,$ post;
echo Termin dostawy: .get_post_meta($ post-> ID,__text_field',true);
}

负责显示的功能:

  add_action('woocommerce_short_description','magik_custom_text',10,1); 
函数magik_custom_text()
{


解决方案

更新2:



1)您应该将其添加到简短描述中,而不是替换它……



正确的方法是在简短说明之前在 woocommerce_single_product_summary 阳离子挂钩中挂接您的自定义函数(优先级为11至19):

  add_action('woocommerce_single_product_summary','magik_custom_text',18); 
函数magik_custom_text()
{
global $ post;
$ field_value = get_post_meta($ post-> ID,‘_text_field’,true);
//仅在设置了
if(!empty($ field_value))
echo'< p>'时显示自定义字段。 __( Termin dostawy:, woocommerce)。 $ field_value。 ‘< / p>’;
}

代码包含在您活动的子主题的function.php文件中(或主题)或任何插件文件中。



经过测试并可以使用。因此,您将获得以下信息:








2)能够删除产品自定义字段的内容您需要替换:

  if(!empty($ woocommerce_text_field))

方式:

  if(isset($ woocommerce_text_field) )

在此挂钩函数中:

  add_action('woocommerce_process_product_meta','woo_add_custom_general_fields_save'); 
函数woo_add_custom_general_fields_save($ post_id){
$ woocommerce_text_field = $ _POST [’_ text_field’];
if(isset($ woocommerce_text_field))
update_post_meta($ post_id,'_text_field',esc_attr($ woocommerce_text_field)));

}


I've a problem with adding a new field in WooCommerce, before short description.

I used script in functions.php and my new custom field is displayed correctly but:

  • Short description disappear when use script, a new field is display fine.
  • I can edit the content of a field on a product page, but I can't delete it. It is always the last value.

How can I display this custom field without removing the product short description?
How can I reset this custom field content?

Here is my code:

// Add a custom Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field', 
            'label'       => __( 'Termin dostawy', 'woocommerce' ), 
            'placeholder' => 'np: 7 dni',
            'desc_tip'    => 'true',
            'description' => __( 'Wpisz przewidywany termin dostawy, np: 7 dni.', 'woocommerce' ) 
        )
    );

}

// Save the custom field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_text_field = $_POST['_text_field'];
    if( ! empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );

}

// Display the custom field
add_action('woocommerce_short_description', 'magik_custom_text', 10, 1);
function magik_custom_text()
{
     global $product;

     global $woocommerce, $post;
     echo "Termin dostawy: ".get_post_meta( $post->ID, '_text_field', true );
}

Function responsible for display:

add_action('woocommerce_short_description', 'magik_custom_text', 10, 1);
function magik_custom_text()
{

解决方案

Update 2:

1) You should add it to short description instead of replacing it…

The correct way is to hook your custom function in woocommerce_single_product_summary cation hook before the short description (using a priority between 11 to 19):

add_action('woocommerce_single_product_summary', 'magik_custom_text', 18);
function magik_custom_text()
{
    global $post;
    $field_value = get_post_meta( $post->ID, '_text_field', true );
    // Displaying the custom field only when is set with a value
    if( ! empty( $field_value ) )
        echo '<p>' . __('Termin dostawy: ', 'woocommerce') . $field_value . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works. So you will get this:


2) To be able to delete the content of your product custom field you need to replace:

 if( ! empty( $woocommerce_text_field ) ) 

by:

 if( isset( $woocommerce_text_field ) )

in this hooked function:

add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_text_field = $_POST['_text_field'];
    if( isset( $woocommerce_text_field ) )
        update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );

}

这篇关于在WooCommerce中简短描述之前显示自定义产品字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆