正在更新管理元框字段中的产品 post 元数据
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>';
}
}
谢谢
我不知道错误在哪里,但我简化了代码。请试一试:
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;
}
}
这是您重新访问过的经过测试且功能齐全的代码,:
// 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 文件或任何插件文件中。
此代码已经过测试并且有效
我正在尝试使用 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>';
}
}
谢谢
我不知道错误在哪里,但我简化了代码。请试一试:
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;
}
}
这是您重新访问过的经过测试且功能齐全的代码,
// 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 文件或任何插件文件中。
此代码已经过测试并且有效