Updating product post meta data in admin meta box

2019-07-16 20:57发布

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

2条回答
SAY GOODBYE
2楼-- · 2019-07-16 21:36

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

查看更多
干净又极端
3楼-- · 2019-07-16 21:36

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

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