更新管理元框字段中的产品发布元数据 [英] Updating product post meta data in admin meta box field
问题描述
我正在尝试使用 update_post_meta() 函数更新 WooCommerce 产品元数据,但它不起作用.
这是我的代码:
函数 woo_add_deal_general_fields_save( $post_id ){$post_id = (int)$post_id;//尝试转换为整数$woocommerce_textarea = $_POST['_deal_textarea'];if( !empty( $woocommerce_textarea ) )如果 ( get_post_meta($post_id, '_deal_textarea', FALSE) ) {$test= update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );} 别的 {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, &要么 get_the_ID();
?
这是我的代码部分,如函数调用:
//显示字段add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');//保存字段add_action('woocommerce_process_product_meta', 'woo_add_deal_general_fields_save');函数 woo_add_custom_general_fields() {全球 $woocommerce, $post;$feature_product=get_post_meta(get_the_ID(), '_featured', true );if($feature_product=='yes'){echo '';//自定义字段将在此处创建...//文本区域woocommerce_wp_textarea_input(大批('id' =>'_deal_textarea','标签' =>__( '交易说明', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处输入交易产品文本值.(将显示在主页上)', 'woocommerce' )));回声'</div>';}}谢谢
解决方案 这是您重新访问的经过测试且功能齐全的代码,基于这个答案:
//在常规标签产品页面中插入自定义管理字段add_action('woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field');函数 add_deal_custom_general_product_field() {全球 $post;$feature_product = get_post_meta( $post->ID, '_featured', true );如果( $feature_product == '是' ){echo '';woocommerce_wp_textarea_input(数组('id' =>'_deal_textarea','标签' =>__( '交易说明', 'woocommerce' ),'占位符' =>'','说明' =>__( '在此处输入交易产品文本值.(将显示在主页上)', 'woocommerce' )) );回声'</div>';}}//提交时在常规标签产品页面中保存自定义管理字段add_action('woocommerce_process_product_meta', 'save_deal_custom_general_product_field');函数 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 文件或任何插件文件中.
此代码已经过测试并有效
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:
// 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
这篇关于更新管理元框字段中的产品发布元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- 无法更新某些库中的元数据字段值;
- 共享点管理的元数据;
- 通过Shopify ScriptTag API读取产品元字段;
- 无法使用数组更新发布元;
- REST API - 无法更新托管元数据字段;
- 检索元数据列字段;
- 更新UIImage方向元数据?;
- 更新Chromecast Media元数据;
- 刷新发布的URL中的元数据;
- 使用Maven元数据发布Ivy SNAPSHOTS;
- 将元数据发布到Service Fabric;
- 元数据/非元数据int类型;
- 如何为WCF服务启用元数据发布;
- 元数据;
- 如何查询所有现有字段的元数据;
- 使用pyexiftool编辑/更新照片元数据的数据;
- 根据“管理元数据"列创建保留策略;
- 使用REST更新库项目元数据;
- 隐藏“缺货"Woocommerce 中带有自定义元数据的产品;
- jclouds:如何更新现有Blob的元数据?;
- 带有blobtrigger的Azure函数-更新元数据;
- 在R中管理元数据的最佳方法是什么?;
- 通过WooCommerce管理订单列表中的特定元字段过滤订单;
- Shopify中的元字段:尝试显示折扣60%的产品的最终销售消息;
- 如何从AWS Cognito JWT中获取元数据以将其用于MongoDB Stitch元数据字段?;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;