【问题标题】:WooCommerce Products Custom Fields (woocommerce)WooCommerce 产品自定义字段 (woocommerce)
【发布时间】:2016-02-08 12:39:13
【问题描述】:

我创建了一个 Textafield 字段类型,当我输入一些文本并点击发布时,它会显示在我的前端。但是,当我删除文本以清理前端并再次单击发布时,它会停留在 Textarea 字段和前端。 有什么帮助吗?

函数.php

    function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  woocommerce_wp_text_input( 
    array( 
        'id'          => '_text_field', 
        'label'       => __( 'titre produit', 'woocommerce' ), 
        'placeholder' => '',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the custom value here.', 'woocommerce' ) 
    )
);


function woo_add_custom_general_fields_save( $post_id ){

    // textfield
    $woocommerce_text_field = $_POST['_text_field'];
    if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, '_text_field', esc_html( $woocommerce_text_field ) );

【问题讨论】:

  • 您在清空文本字段时遇到问题?你的意思是它没有从字段中删除文本?
  • @Noman 是的,仍然显示在前端
  • 在下面查看我的答案。

标签: php wordpress woocommerce woothemes


【解决方案1】:

只需删除此条件。

if( !empty( $woocommerce_text_field ) )

当前您正在检查文本字段是否为空更新文本字段的内容。

当您删除上述条件时,无论您是否放置内容,它都会更新该字段。

【讨论】:

    【解决方案2】:

    是的,您可以删除条件

    function woo_add_custom_general_fields_save( $post_id ){
    
    // textfield
    $woocommerce_text_field = $_POST['_text_field'];
    if( !empty( $woocommerce_text_field ) )
     update_post_meta( $post_id, '_text_field', esc_html( $woocommerce_text_field ) );
    else
       update_post_meta( $post_id, '_text_field', '' );
    }
    
    **or** you can check like this also before updating or displaying: 
    
    if ( get_post_meta( $post->ID, '_text_field', true ) != '' ) {
     // your code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2019-06-25
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多