wordpress - 将 x 添加到 meta_value 并在 Woocommerce 中显示结果
问题描述
我创建了一个函数来向 woocommerce 产品管理屏幕添加一列,然后获取键 ae_cost_pkr 的元值并将 5 添加到它并在新创建的列中回显结果。它有效,但我遇到了一个问题,即原始元值附加到相邻列。我究竟做错了什么 ?
在这张图片中可以看到错误:ibb.co/n3y6n2n
我的功能是:
// add
add_filter( 'manage_edit-product_columns', 'my_total_sales_1', 20 );
// populate
add_action( 'manage_posts_custom_column', 'my_total_sales_2' );
function my_total_sales_1( $col_th ) {
// create colunm
return wp_parse_args( array( 'ae_cost_pkr' => 'AE Price' ), $col_th );
}
// add 5 to the meta value and display in column
function my_total_sales_2( $column_id ) {
if( $column_id == 'ae_cost_pkr' )
$re = get_post_meta( get_the_ID(), 'ae_cost_pkr', true );
echo $re + 5;
}
解决方案
试试下面的代码,如果你需要加 5,你需要更新 meta
// add
add_filter( 'manage_edit-product_columns', 'my_total_sales_1', 20 );
// populate
add_action( 'manage_posts_custom_column', 'my_total_sales_2' );
function my_total_sales_1( $col_th ) {
// create colunm
return wp_parse_args( array( 'ae_cost_pkr' => 'AE Price' ), $col_th );
}
// add 5 to the meta value and display in column
function my_total_sales_2( $column_id ) {
if( $column_id == 'ae_cost_pkr' )
$readd = '5';
$re = update_post_meta( get_the_ID() , 'ae_cost_pkr', $readd);
echo $re;
}
推荐阅读
- java - py4j 无法在 Docker 容器中运行
- python - 使用迭代器解包可变长度列表
- c# - “LinkAssemblies”任务意外失败。错误:XA2006
- arrays - 如果实例对象值和数组值相同,则删除数组索引
- numpy - 解释张量索引
- java - 无法让最简单的 JNI 示例在 Windows 10 上运行
- jenkins - jenkins 多次构建同一个项目
- python - 如何使用 JSON.stringify() 以 base 64 十六进制字符串发送图像?
- angular - Angular - 数据绑定变量是否应该在构造函数中初始化?
- azure - Azure 存储客户端加密:我们应该如何处理加密密钥续订/轮换?