Updating product post meta data in admin meta box field

若如初见. 提交于 2019-12-11 02:24:10

问题


I am trying to update WooCommerce product meta data using update_post_meta() function, but it does''t work.

Here is my code:

 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;

}

If I try it with a fixed product ID, it works:

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

Why its not working with $post_id, (int)$post_id, & either get_the_ID();?

Here is the part of my code like function calls:

// 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>';
 }

}

Thanks


回答1:


Here is your revisited tested and fully functional code, based on this answer:

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

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

This code is tested and works




回答2:


I don't know where is the bug, but i simplified the code a bit. Please try it:

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

}


来源:https://stackoverflow.com/questions/39850227/updating-product-post-meta-data-in-admin-meta-box-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!