Display a custom product field before short descri

2019-04-13 17:38发布

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()
{

1条回答
男人必须洒脱
2楼-- · 2019-04-13 17:47

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:

enter image description here

enter image description here


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 ) );

}
查看更多
登录 后发表回答