首页 > 解决方案 > 将 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; 

}

标签: wordpresswoocommerce

解决方案


试试下面的代码,如果你需要加 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;
}

推荐阅读