更新管理元框字段中的产品帖子元数据


Updating product post meta data in admin meta box field

我正在尝试使用update_post_meta()函数更新WooCommerce产品元数据,但它不起作用。

下面是我的代码:
 function woo_add_deal_general_fields_save( $post_id ){
     $post_id = (int)$post_id; // tried to convert into integer 
    $woocommerce_textarea = $_POST['_deal_textarea'];
    if( !empty( $woocommerce_textarea ) ) 
    if ( get_post_meta($post_id, '_deal_textarea', FALSE ) ) { 
     $test=   update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
    } else {     
        add_post_meta($post_id, '_deal_textarea',  $woocommerce_textarea  );
    }
         var_dump($test);exit;
}

如果我尝试使用固定的产品ID, 它可以工作:

$test=   update_post_meta(70, '_deal_textarea', $woocommerce_textarea );

为什么它不能与$post_id, (int)$post_id, & either get_the_ID();一起工作?

下面是我的部分代码,类似于函数调用:

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_deal_general_fields_save' );
function woo_add_custom_general_fields() {
  global $woocommerce, $post;
$feature_product=get_post_meta(get_the_ID(), '_featured', true );
 if($feature_product=='yes'){
  echo '<div class="options_group">';
  // Custom fields will be created here...
  // Textarea
woocommerce_wp_textarea_input( 
    array( 
        'id'          => '_deal_textarea', 
        'label'       => __( 'Deal Caption', 'woocommerce' ), 
        'placeholder' => '', 
        'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' ) 
    )
);
  echo '</div>';
 }
}

谢谢

根据这个答案,这里是您重新访问的经过测试的功能完整的代码:

// Inserting a Custom Admin Field in general tab products pages
add_action( 'woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field' );
function add_deal_custom_general_product_field() {
    global $post;
    $feature_product = get_post_meta( $post->ID, '_featured', true );
    if( $feature_product == 'yes' ){
        echo '<div class="options_group">';
        woocommerce_wp_textarea_input( array(
            'id'                => '_deal_textarea',
            'label'             => __( 'Deal Caption', 'woocommerce' ),
            'placeholder'       => '',
            'description'       => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
        ) );
        echo '</div>';
    }
}
// Saving the Custom Admin Field in general tab products pages when submitted
add_action( 'woocommerce_process_product_meta', 'save_deal_custom_general_product_field' );
function save_deal_custom_general_product_field( $post_id ){
$wc_field = $_POST['_deal_textarea'];
$feature_product = get_post_meta( $post_id, '_featured', true );
if( !empty($wc_field) && $feature_product == 'yes')
    update_post_meta( $post_id, '_deal_textarea', esc_attr( $wc_field ) );
}

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

我不知道哪里是错误,但我简化了代码一点。请试试:

function woo_add_deal_general_fields_save( $post_id ){
    $woocommerce_textarea = $_POST['_deal_textarea'];
    if( !empty( $woocommerce_textarea ) ) {          
         $test = update_post_meta( $post_id, '_deal_textarea', $woocommerce_textarea );
         var_dump( $test ); exit;           
    }            
}