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
Here is your revisited tested and fully functional code, based on this answer:
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
I don't know where is the bug, but i simplified the code a bit. Please try it: